Can I re-generate random values in AutoFixture using a seed?
Asked Answered
W

1

12

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.

Wherry answered 6/5, 2015 at 13:41 Comment(6)
AutoFixture doesn't have such a feature, as it doesn't use a global Random object to generate values. In all the years I've used it, I've never needed such a feature either. If a test fails, you can write them in such a way that the test report will always include which values were used. For example, if you use AutoFixture.Xunit, this happens automatically.Engineering
BTW, I do realize that FsCheck does have such a feature, but I don't use it there either...Engineering
Writing out all test data that took part in a certain test doesn't scale really much. Assuming that there are many of them, you will end up with a huge big list. Also, in case of a failed test, you would have to adapt all test data in the test to the printed values. Even more cumbersome is that you'll have to write custom code in each and every test, as long as the test data is not contained in some instance fields. I really think that a printed seed is the way to go here.Wherry
Are you arguing from experience? Is this something you've tried for a long period and found to be lacking?Engineering
I find fixing the seed very useful too, especially for reproducing failing cases. That said, to actually record the found problems and avoid regression, I typically do make a separate unit test with the literal failing values. The reason for that is that generators may change or be tweaked slightly (either in FsCheck or your custom generator) and so the generated values are generally not expected to be stable for all time.Jacobinism
@MarkSeemann: I think this highly depends on your domain. I had several projects - some of them targeting code generation, others were more business stuff. I don't know what kind of projects you're working on, but I'm sure there are also some which doesn't need this feature. That said, in my opinion there is a benefit for some test domains, because of the coverage and repeatability. I don't know about your plans with AutoFixture, but for me it sounds like a good feature. On the other hand it might need some big work; so your opinion counts :)Wherry
O
6

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.

Ora answered 6/5, 2015 at 14:17 Comment(1)
I'm more interested into something that is type-wide. I will update my question to make this more clear.Wherry

© 2022 - 2024 — McMap. All rights reserved.