简体   繁体   中英

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? 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 :

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

If you want, you can also freeze a string based on a seed value of yours:

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

Keep in mind that AutoFixture only uses the provided seed value when asked to create strings. If you want to use a seed value for any other type you'll have to customize it yourself .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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