Are integer numbers generated with IFixture.Create<int>()
unique?
The Wiki says that numbers are random, but it also tells us this
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.
Two related things at GitHub:
https://github.com/AutoFixture/AutoFixture/issues/2
https://github.com/AutoFixture/AutoFixture/pull/7
And what about those unit tests?
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
}
I have not found a statement that says the generated numbers are unique, only those bits of information that would suggest it, but I may be wrong.
Here we are creating 10 test data for Employee Object. The RepeatCount property sets a number that controls how many objects are created when AutoFixture.Fixture creates more than one anonymous objects.The default value is 3.
There are multiple ways to customize how AutoFixture generates its values. A simple approach to customize the string generation: One piece of dummy data that always takes time is lists.
Remember, three is the default number for AutoFixture. Assume that you have a child class inside your parent class: Generating a list of MyParentClass: Would give me a list of 3 MyParentClass, with dummy string ParentName and a dummy MyChildClass, with a dummy string ChildName.
The RepeatCount property sets a number that controls how many objects are created when AutoFixture.Fixture creates more than one anonymous objects.The default value is 3. The Repeat method belongs to the FixtureRepeater static class which contains extension methods for repeating a function in AutoFixture.IFixture instances.
Currently, AutoFixture endeavours to create unique numbers, but it doesn't guarantee it. For instance, you can exhaust the range, which is most likely to happen for byte
values. If, for instance, you request 300 byte
values, you will get duplicates, because there's only 256 values to choose from.
AutoFixture will happily reuse values once the initial set has been depleted; the alternative would be to throw an exception.
If it's important for a test case that numbers are unique, I would recommend making this explicit in the test case itself. You can combine Generator<T>
with Distinct
for this:
var uniqueIntegers = new Generator<int>(new Fixture()).Distinct().Take(10);
If you're using AutoFixture.Xunit2, you can request a Generator<T>
via a test method argument:
[Theory, AutoData]
public void MyTest(Generator<int> g, string foo)
{
var uniqueIntegers = g.Distinct().Take(10);
// more test code goes here...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With