vincent15000's avatar

How to seed the database once for all tests

Hello,

Instead of seeding the database once for each test, I'd like to seed it only once for all tests.

Where can I write the code to seed the database ?

Thanks for your help.

V

0 likes
5 replies
vincent15000's avatar

@Tray2 Thank you ... but what about seeding the database once for all tests ? Is it a good idea ? Is it only possible ?

Scooby's avatar

In our TestCase we do the following which gets the job done.

protected static $initialized = false;

protected function setUp(): void
{
    parent::setUp();

    if (self::$initialized === false) {
        Artisan::call('migrate');

        $this->seed();

        self::$initialized = true;
    }
}

Perhaps that would work well enough for your testing purposes..

1 like
nasirkhan's avatar

I was searching for the same solution but unfortunately, I could not solve it.

I added the code mentioned by @scooby on TestCase.php but when I try to get a user from a test case it shows the users table does not exist. what could be the solution?

Please or to participate in this conversation.