3rgo's avatar
Level 1

Parallel testing with multiple database connections

Hi !

I'm currently working on an API that needs to have multiple database connections. At the moment it uses : 1 MySQL + 1 PostgreSQL + 1 MongoDB. MySQL and PostgreSQL may be merged in the future, but it will at least keep a MongoDB in parallel of the PostgreSQL. Connection to MongoDB is handled by the jenssegers/mongodb package

This API is tested with Pest, but the current ~90 tests take around 2 minutes to run, and this is about 5% of the total features that will be added. So I wanted to setup parallel testing, but only the MySQL (main database connection) has the testing_X databases created.

Does anyone know what I can override to handle the other connections so that it also creates the testing_X databases on PostgreSQL and MongoDB ?

I've already altered the default TestCase to call a custom migrate:fresh when using the DatabaseMigrations trait, so that it can reset the databases for all three connections (which I know is the main performance issue at the moment in my tests), but I have not been able to find how to intercept the parallel testing setup procedure.

1 like
7 replies
cwhite's avatar

I've had the same issue in the past, check out the Parallel Testing Hooks---specifically setUpTestDatabase().

Note that the hooks will be called for every process, so if you only want to run something once then you'll need to do something like:

        // Executed when a test database is created...
        ParallelTesting::setUpTestDatabase(function ($database, $token) {
            if ($token === 1) {
                // create databases
            }
        });
2 likes
3rgo's avatar
Level 1

@cwhite Thanks, I'm gonna check it out ! In the meantime I found the ParallelTestingServiceProvider in the framework code, so I'll try to replicate the database creation script for multiple connections...

netdjw's avatar

@3rgo do you found any solution for the problem? I have the same issue.

tisuchi's avatar

@3rgo IIUC, you want parallel testing for the PostgreSQL and MongoDB connections, where you will need to override the default database connection settings for your tests. This can be done by creating a new database configuration file specifically for your tests and using the DB_CONNECTION environment variable to specify which connection to use.

For example, you could create a database.testing.php configuration file in your config directory and set up the PostgreSQL and MongoDB connections like this:


'default' => env('DB_CONNECTION', 'mysql'),

'mysql' => [
    // MySQL connection settings
],

'pgsql' => [
    // PostgreSQL connection settings
],

'mongodb' => [
    // MongoDB connection settings
],

Then, in your phpunit.xml file, you can specify the DB_CONNECTION environment variable for each of your test processes:


<phpunit>
    <!-- Other configuration options -->

    <php>
        <!-- Other PHP options -->

        <env name="DB_CONNECTION" value="mysql"/>
    </php>

    <php processIsolation="true" timeout="36

3rgo's avatar
Level 1

@tisuchi Thanks for the reply, but you did not understand my request. I'm already using multiple database connections in my app. What I want is to be able to use the Pest parallel testing plugin with multiple database connections. The thing is this plugin works properly with the default database connection but does not create the multiple databases on the secondary connections.

Čamo's avatar

Did you solve this problem? I have the same problem. Only default database connection separate databases. I tried to solve it in config/database.php but can not access the parallel test token.

Please or to participate in this conversation.