Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

netdjw's avatar
Level 15

Pest parallel testing with Spatie's multitenancy package

I use "spatie/laravel-multitenancy": "3.0.2" package in my project. I use Pest for testing and now when I use php artisan test command to testing, it works fine all test PASS. But I have 2600+ tests and it runs over 25 minutes.

When I want to run php artisan test --parallel tests are fail because of migration issues, duplicate database entries and similar things.

I found out what's happenging (and what's not):

  1. The parallel testing create databases for each testing threads (in my case 12 different databases). But only for landlord. I defined in the config/database.php the test_landlord connection with test_landlord database name. So I have now test_landlord_test_1, test_landlord_test_2 ... test_landlord_test_12 databases.
  2. Tenant databases are not created in this way. Instead of way from step 1st, the tenant database is used the default what I defined in the config/database.php. Connection name: test_tenant, database name: test_tenant.
  3. All parallel testing thread using the default "test_tenant" database for running test.
  4. When I try to migrate tenant databases (because of parallel working) the first migration running, then db:wipe drop tables in half of the first process running. And it leads to a mess.

The migration command is this:

Artisan::call('tenants:artisan "migrate --database=test_tenant"');

But it can't catch the database name from Config::get('database.connections.test_tenant.database').

Is there any way to give the exact database name to the artisan command instead of connection name?

Or is there any way to set up parallel testing to use multiple database connections?

1 like
0 replies

Please or to participate in this conversation.