I am trying to run laravel feature tests in parallel mode as it is in document. My phpunit.xml
is configed as below :
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="mysql"/>
</php>
and I am running the tests with this command :
php artisan test --parallel
The result is all tests fail with this error message :
PDOException: SQLSTATE[HY000] [1049] Unknown database 'mydbname_test_11'
should I create this database before starting test or laravel creates that automatically ?
3
Answers
Yes, the database must exist before you run your test. another thing to note is that parallel unlike PHP artisan test, will not migrate your database for you. So what I found works best is to migrate my database before running the test in parallel.
When running tests in parallel mode with Laravel, it is necessary to create a separate database for each parallel test process. Laravel will not create these databases automatically, so it is up to you to ensure that they exist before running your tests. You can either create the databases manually, or you can use a tool like PHPUnit’s
@database
annotations to automatically create and tear down the necessary databases before and after each test run.In your
phpunit.xml
file, you can specify theDB_DATABASE
andDB_USERNAME
environment variables to control which database Laravel should use for testing. For example:The
%s
in theDB_DATABASE
value will be replaced with the current test process number, so each test process will use a different database. You will need to make sure that a database with the appropriate name exists for each test process before running your tests.You can then use the
@database
annotation in your test classes to control the creation and destruction of these databases.You do not need to manually create databases to run tests in parallel. Laravel will create these databases for you automatically and tear them down when your tests have finished.
This is true also when running in Bitbucket Pipelines, for the commenter that asked.
From the documentation on parallel testing in Laravel (9.x):
Your issue is likely to be a composer dependency that is preventing Laravel from creating these databases. In my case, it was the
grimzby/laravel-mysql-spatial
package. Link to the relevant issue here.