Matthias
Matthias

Reputation: 16209

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.

Upvotes: 12

Views: 2879

Answers (1)

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59983

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.

Upvotes: 6

Related Questions