The Wiki says这些数字是随机的,但它也告诉我们这一点
The first numbers are generated within the range of [1,255],as this
is a set of values that are valid for all numeric data types. The
smallest numeric data type in .NET is System.Byte,which fits in this
range.When the first 255 integers have been used,numbers are subsequently
picked from the range [256,32767],which corresponds to the remaining
positive numbers available for 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 }
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 }
我还没有找到一个声明,说明生成的数字是唯一的,只有那些可能暗示它的信息,但我可能错了.
解决方法
一旦初始设置耗尽,AutoFixture将很乐意重用值;另一种方法是抛出异常.
如果对于测试用例而言数字是唯一的很重要,我建议在测试用例本身中明确这一点.您可以组合Generator< T>与众不同:
var uniqueIntegers = new Generator<int>(new Fixture()).Distinct().Take(10);
如果您使用的是AutoFixture.Xunit2,则可以请求生成器< T>通过测试方法参数:
[Theory,AutoData] public void MyTest(Generator<int> g,string foo) { var uniqueIntegers = g.Distinct().Take(10); // more test code goes here... }