Home » Php » php – Laravel 5 : Use different database for testing and local

php – Laravel 5 : Use different database for testing and local

Posted by: admin April 23, 2020 Leave a comment


How does one change database for development and testing on local system without editing the .env file each time?

I have found it quite inconvenient to practice TDD because of this.

Is it possible for a Laravel application to differentiate between normal development and testing so that it can choose the appropriate database?

How to&Answers:

Create a testing database configuration in Laravel

Edit the config\database.php file and add a testing – array into the connections array:

'connections' => [
    'testing' => [
        'driver' => env('DB_TEST_DRIVER'),
        // more details on your testing database

Then add the necessary variables to your .env-file.

Edit PHPUnit configuration

Open your phpunit.xml-file and add the following within your <php>-tag:

<env name="DB_CONNECTION" value="testing"/>

Now PHPUnit will run with the tests on the database you defined in the testing – array.


For Laravel 5.5, the proper way to do this is create a testing environment file called .env.testing. Here, you can define your testing environment, including the database you want to use for testing…

DB_USERNAME=test DB user
DB_PASSWORD=test DB password

Then run this command…

php artisan config:cache --env=testing

This will configure the cache to the .env.testing file parameters.

Here is a link to the documentation.


If your testing database uses the same configuration but only it’s name is different, you could only change the selected database name by adding

<env name="DB_DATABASE" value="testing_db_name"/>

to the file phpunit.xml in the <php> node


if you need a specific database for one of your unit tests, add this code at the start of your unit test

    $connections                                    = config('database.connections');
    $connections['single_test_database_connection'] = [
        'driver'      => 'mysql',
        'host'        => env('DB_HOST', ''),
        'port'        => env('DB_PORT', '3306'),
        'database'    => 'single_test_database',
        'username'    => env('DB_USERNAME', 'forge'),
        'password'    => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset'     => 'utf8mb4',
        'collation'   => 'utf8mb4_unicode_ci',
        'prefix'      => '',
        'strict'      => true,
        'engine'      => null,
    config()->set('database.connections', $connections);
    config()->set('database.default', 'single_test_database_connection');


You can use a different .env file for each type of test. You can modify your tests/functional.suite.yml and tests/unit.suite.yml something like this:

class_name: FunctionalTester
    enabled: [Laravel5, FunctionalHelper, Asserts]
            environment_file: .env.testing

And this:

class_name: UnitTester
    enabled: [Asserts, UnitHelper, Laravel5]
            environment_file: .env.unittesting

Or you can simply modify your phpunit.xml and add environment vars like @Tijmen did above.