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

seanmccabe's avatar

Using seperate database for testing

I'm trying to use a seperate MySQL database for testing but it appears to just be ignoring any of the setup I've done as it keeps going to my local database and wiping everything in there.

So how do I do this? I've got a .env.testing file setup with the following:

APP_NAME="Laravel Test"
APP_ENV=testing
APP_KEY=base64:iSgW1vFSRF9ieXJpLps9+IvET2U8vcG7iw0NLE2yRvM=
APP_DEBUG=true
APP_URL=http://localhost

DB_TEST_CONNECTION=testing
DB_TEST_HOST=127.0.0.1
DB_TEST_PORT=3306
DB_TEST_DATABASE=test
DB_TEST_USERNAME=root

PHP Unit has the following:

<php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <!-- <env name="DB_CONNECTION" value="testing"/> -->
        <!-- <env name="DB_DATABASE" value=":memory:"/> -->
        <env name="MAIL_MAILER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
    </php>

If I uncomment the DB_CONNECTION line, then it does attempt to use the testing connection I have the database config file, but it attempts to connect with the default forge credentials, apparently completely overlooking the .env.testing file.

Ater making any changes I also do php artisan config:cache --env=testing

I've searched and searched on this and as far as I can tell I'm doing everything right, but I must be missing something.

0 likes
12 replies
tykus's avatar

Why are your .env.testing keys starting DB_TEST_ and not just DB_ (like they would be in .env)?

DB_CONNECTION=testing
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
seanmccabe's avatar

@tykus trying to nail down exactly where the issue was, to ensure it's pulling the right values from the env file.

tykus's avatar

@seanmccabe but I assume the config/database.php file is getting the variables named DB_CONNECTION, DB_HOST etc. and you don't have them in the .env.testing file, so it falls back to .env

lbecket's avatar

If you copy the contents of your .env file to a new file named .env.testing you can then modify the values for the database connection in the .env.testing file to use the test database. Then, you can use the following code in your tests/TestCase.php file to load the .env.testing file before running the tests:

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    public function setUp(): void
    {
        parent::setUp();

        $this->loadEnvironmentFrom('.env.testing');
    }
}

By doing this, Laravel will use the .env.testing file when running your tests, and should connect to the database specified in that file.

seanmccabe's avatar

@tykus this did work, though it'd be nice not to have to remember the env flag each time I run them. Anyway to set that automatically?

tykus's avatar

@seanmccabe you can just use the phpunit.xml file to set environment variables if you prefer 🤷‍♂️

lararara's avatar

If you're using Laravel 11. Just add the env.testing with the database details. Running PEST tests via PHPStorm will require no extra config. Laravel/Pest will auto load the database in the .env.testing

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=my_pgsql_testing_db
DB_USERNAME=root
DB_PASSWORD=

This way when your tests have the RefreshDatabase trait, which is the default for all files under the Feature folder, it won't reset your main DB every single time. Do note you'll need to run an initial migration on the test db.

1 like
baumgars's avatar

@lararara do be more clear about .env.testing here is the snippet from the official docs

"The .env.testing Environment File In addition, you may create a .env.testing file in the root of your project. This file will be used instead of the .env file when running Pest and PHPUnit tests or executing Artisan commands with the --env=testing option."

Can be found here

https://laravel.com/docs/12.x/testing

lara372116's avatar

You do not have to use separate database for testing you can just use these code and testing code will not affect your data in database, this is for the pest

use Illuminate\Foundation\Testing\DatabaseTransactions;
uses(DatabaseTransactions::class); 

1 like

Please or to participate in this conversation.