生成的整数是IFixture.Create<int>()唯一的吗?
维基说这些数字是随机的,但它也告诉我们这一点
第一个数字是在[1,255]范围内生成的,因为这是一组对所有数值数据类型有效的值..NET中最小的数字数据类型是System.Byte,它适合此范围.
当使用前255个整数时,随后从范围[256,32767]中选择数字,这对应于System.Int16可用的剩余正数.
GitHub上的两件相关事情:
https://github.com/AutoFixture/AutoFixture/issues/2
https://github.com/AutoFixture/AutoFixture/pull/7
那些单元测试怎么样?
https://github.com/AutoFixture/AutoFixture/blob/master/Src/AutoFixtureUnitTest/GeneratorTest.cs#L33
[Theory, ClassData(typeof(CountTestCases))]
public void StronglyTypedEnumerationYieldsUniqueValues(int count)
{
// Fixture setup
var sut = new Generator<T>(new Fixture());
// Exercise system
var actual = sut.Take(count);
// Verify outcome
Assert.Equal(count, actual.Distinct().Count());
// Teardown
}
Run Code Online (Sandbox Code Playgroud)
https://github.com/AutoFixture/AutoFixture/blob/master/Src/AutoFixtureUnitTest/GeneratorTest.cs#L57
[Theory, ClassData(typeof(CountTestCases))]
public void WeaklyTypedEnumerationYieldsUniqueValues(int count)
{
// Fixture setup
IEnumerable sut = new Generator<T>(new Fixture());
// Exercise system
var actual = sut.OfType<T>().Take(count);
// Verify outcome
Assert.Equal(count, actual.Distinct().Count());
// Teardown
}
Run Code Online (Sandbox Code Playgroud)
我还没有找到一个声明,说明生成的数字是唯一的,只有那些可能暗示它的信息,但我可能错了.
Mar*_*ann 12
目前,AutoFixture努力创建唯一的数字,但它并不能保证.例如,您可以耗尽范围,这最有可能发生在byte值上.例如,如果您请求300个byte值,您将获得重复项,因为只有256个值可供选择.
一旦初始设置耗尽,AutoFixture将很乐意重用值; 替代方案是抛出异常.
如果对于测试用例而言数字是唯一的很重要,我建议在测试用例本身中明确这一点.您可以结合Generator<T> 使用Distinct此:
var uniqueIntegers = new Generator<int>(new Fixture()).Distinct().Take(10);
Run Code Online (Sandbox Code Playgroud)
如果您正在使用AutoFixture.Xunit2,则可以Generator<T>通过测试方法参数请求:
[Theory, AutoData]
public void MyTest(Generator<int> g, string foo)
{
var uniqueIntegers = g.Distinct().Take(10);
// more test code goes here...
}
Run Code Online (Sandbox Code Playgroud)