Search code examples
phpunitlaravel-5.8faker

Reproduce random tests in Laravel (using Faker)


In one of our projects we are experiencing random failures of tests, when re-running them more often than not they succeed again.

We're using Laravel 5.8, with phpunit for tests, and in our tests we often seed the database using factories and Faker.

So I was wondering, is it possible to, at the end of the tests (especially when they fail), to print the seed used by Faker to generate the values? So I could then set this seed before running the tests, so I can reproduce the errors?


Solution

  • After some more checking I found that faker uses mt_rand internally.

    So what I did, was set the seed at the start of each test in setUp and then print it in tearDown:

    abstract class TestCase extends BaseTestCase
    {
        protected $seed;
    
        protected function tearDown(): void
        {
            // Print seed so we know which one was used for this failing test
            if ($this->hasFailed()) {
                echo $this->seed;
            }
        }
    
        protected function setUp(): void
        {
            // Seed the random seed for this test, so we can easily reproduce the same result, used seed will be printed when test fails
            $this->seed = rand();
            mt_srand($this->seed);
        }
    }
    

    Then it will print the seed of each failed test, and to reproduce while debugging simply replace $this->seed = rand(); with $this->seed = the seed;.