简体   繁体   English

我可以使用种子在AutoFixture中重新生成随机值吗?

[英]Can I re-generate random values in AutoFixture using a seed?

Is there any way in AutoFixture so that fixture.Create<string>() will yield the same result? 在AutoFixture中是否有任何方法使fixture.Create fixture.Create<string>()产生相同的结果? Ie, can I initialize the fixture with a seed? 即,我可以用种子初始化夹具吗?

Update 更新

To be more precise, I'm looking for a random value generator that is initialised with some random seed, which is also outputted if a test fails. 更确切地说,我正在寻找一个随机值生成器 ,它使用一些随机种子初始化,如果测试失败也会输出。 Thus, I can take the seed for that particular test run, and run the test with the fixed seed again. 因此,我可以为特定的测试运行取种子,并再次使用固定种子运行测试。 The seed should apply to all instances, regardless of their types. 种子应该适用于所有实例,无论其类型如何。 I think this is the most powerful way to use random values in tests, because it has a huge coverage, and is also reproducible. 我认为这是在测试中使用随机值的最有效方法,因为它具有巨大的覆盖范围,并且也是可重现的。

You're looking at a feature called freezing : 您正在查看名为freeze的功能:

var alwaysTheSameString = fixture.Freeze<string>();

If you want, you can also freeze a string based on a seed value of yours: 如果需要,您还可以根据您的种子值冻结string

var alwaysTheSameFooString = fixture.Freeze<string>("foo");

Keep in mind that AutoFixture only uses the provided seed value when asked to create strings. 请记住,当要求创建字符串时,AutoFixture仅使用提供的种子值。 If you want to use a seed value for any other type you'll have to customize it yourself . 如果要将种子值用于任何其他类型,则必须自己进行自定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM