加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用AutoFixture 3生成的整数是唯一的吗?

发布时间:2020-12-15 08:08:47 所属栏目:百科 来源:网络整理
导读:使用IFixture.Create int()生成的整数是唯一的吗? 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 smalle
使用IFixture.Create< int>()生成的整数是唯一的吗?

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努力创造独特的数字,但它并不能保证.例如,您可以耗尽范围,这最有可能发生在字节值上.例如,如果您请求300字节值,您将获得重复项,因为只有256个值可供选择.

一旦初始设置耗尽,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...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读