3

I'm using Bogus to generate test data.

Is there a way to set the seed it uses so it generates the same test data, in the same order?

For example, this test would fail:

var person1 = new Bogus.Person();
var person2 = new Bogus.Person();
Assert.AreEqual(person1.FullName, person2.FullName);

However, is there a way to reset the seed so that it wouldn't, ie:

Bogus.Config.SetSeed(1);
var person1 = new Bogus.Person();

Bogus.Config.SetSeed(1);
var person2 = new Bogus.Person();

Assert.AreEqual(person1.FullName, person2.FullName);
b_levitt
  • 7,059
  • 2
  • 41
  • 56

2 Answers2

5

The readme has an example of this:

//Set the randomzier seed if you wish to generate repeatable data sets.
Randomizer.Seed = new Random(8675309);

However, setting the seed means that the results of the random generator are consistent. To do what you want, you'll need to reset the seed before each call to get the same results.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Thank you. I knew I saw it somewhere. I do only need it done once per test run, but I wrote the example to indicate I wanted control of it, just to make sure i didn't get an answer that required restarting the app domain. – b_levitt Oct 15 '18 at 14:10
3

The other answer will change the global seed, which isn't really nice:

  • There is a change you can't run tests in parallel
  • There is a change that test code is dependent of each other

The recommend way is to change the seed of the Faker itself:

For the non-generic Faker:

var seed = 8675309;
var faker = new Faker()
faker.Random = new Randomizer(seed );

The generic Faker has a helper, UseSeed:

var seed = 8675309;
var faker = new Faker<MyClass>().UseSeed(seed);
Julian
  • 33,915
  • 22
  • 119
  • 174