Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I specify the random seed for Bogus?

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);
like image 308
b_levitt Avatar asked Dec 10 '25 17:12

b_levitt


2 Answers

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);
like image 197
Julian Avatar answered Dec 12 '25 07:12

Julian


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.

like image 20
D Stanley Avatar answered Dec 12 '25 05:12

D Stanley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!