drewdan's avatar
Level 15

Test starts before seeding finishes

Hey Guys,

How does seeding in tests work?

protected function setUp() {
        parent::setUp();
        $this->artisan('migrate');
        (new \DatabaseSeeder())->call(\DatabaseSeeder::class);
    }

I have this in my parent TestClass which migrates and seeds the db before every test. But the echo output always appears after the tests have finished. Does this mean the seeding hasnt finished before the test starts to run. Coincidentally the tests fail because my account doesnt have the right permissions, which is what the seeding is for.

Cheers for your help.

Andrew

0 likes
2 replies
bobbybouwmann's avatar

The setUp method is run before each test, I can't think of a way how this would work asynchronous.

Also one small improvement, why don't you run your seeder as artisan command as well?

$this->artisan('migrate --seed');

// Or
$this->artisan('migrate');
$this->artisan('db:seed', ['class' => 'DatabaseSeeder']);
drewdan's avatar
Level 15

So I believe the issue was to do with me not specifying which database to seed. So the test database never got seeded. So the tests failed. However, from a performance stand point, because of the sheer amount of data that needs to be seeded. It became more practical to create a file based sqlite database and then seed that first, and then run all of the tests on that using db transactions.

Cheers for all of your assistance. As always, you are fantastic :)

Please or to participate in this conversation.