Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? I.e., 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.

like image 949
Matthias Avatar asked May 06 '15 13:05

Matthias


1 Answers

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.

like image 126
Enrico Campidoglio Avatar answered Oct 05 '22 22:10

Enrico Campidoglio