netdjw's avatar
Level 15

Drop and recreate sqlite database files between tests

I'm using Spatie's multitenancy package with separated databases per each tenant. Now I want to start e2e testing with Dusk. I want to use sqlite database for testing with a landlord.sqlite and tenant.sqlite database file.

My issue is when I start tests I need to use special artisan commands for migrating (because of spatie's package). I want to put this command somewhere general place into my test codes and I don't want to call in each tests.

setUp is not a good way, because it runs before each tests. I want to migrate only at once, before all tests.

And if it's possible I want to clean up after tests, so it'll be nice a function running after all tests to drop the test database files.

What is the best solution for this?

0 likes
1 reply
Sinnbeck's avatar

The way laravel does it is to indeed to it in setIUp on the base TestCase file. But to only run it once, it adds a static property to check if it has been run already

private static $migrated = false;

//and in setUp
if (! self::$migrated) {
     //handle migration and database setup
    self::$migrated = true;
}

Please or to participate in this conversation.