catsedawk's avatar

testing: RefreshDatabase trait with seeder (for every single test)

I am trying to use the 'RefreshDatabase' trait.

What I'm looking for is that for every test the db is migrated fresh and then seeded with some vital rows.

If i do this with a single test function it all works but if i have several test functions it seems the seed isnt executed anymore..

i tried:

class MyTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed('TestDataSeeder');
    }

    public function testCase1(){
        //
    }

    public function testCase2(){
        //
    }
}

aswell as:

class MyTest extends TestCase
{
    use RefreshDatabase;

    public function testCase1(){
        $this->seed('TestDataSeeder');
        //
    }

    public function testCase2(){
        $this->seed('TestDataSeeder');
        //
    }
}

So the migrate:fresh seems to be run everytime but the seed only the first time.

so if i have:

  • only testCase1 function all is well
  • only testCase2 function all is well
  • have both, then the second function fails because the database is empty (but tables are created correctly)

any ideas how to make this work?

0 likes
2 replies
catsedawk's avatar

never mind, after some digging around, Im suspecting the two tests bite each other since it only happens when both tests are run and the trait is just working as it is supposed to work.

catsedawk's avatar

there was something in two functions biting each other but after fixing it it still didnt work.

Now I'm using this and also gets the job done:

    protected function setUp(): void
    {
        parent::setUp();
        $this->artisan('migrate:fresh');
        $this->seed('TestDataSeeder');
    }

Please or to participate in this conversation.