vincent15000's avatar

Testing with DB is sometimes done on the main DB instead of the test DB

Hello,

Something strange : when I run sail artisan test, the test is done on the main DB instead of the test DB. Yet the phpunit configuration seems to be ok.

Do you have any idea why a test could be done on the bad DB ?

What do you suggest me to check ?

Thanks for your help.

V

0 likes
7 replies
LaryAI's avatar
Level 58

It sounds like your tests are not using the correct database configuration. Here are a few steps to troubleshoot and ensure that your tests are using the test database:

  1. Check the .env.testing file: Ensure that you have a .env.testing file in your project root. This file should contain the database configuration for your test environment. For example:

    DB_CONNECTION=mysql
    DB_DATABASE=testing
    DB_USERNAME=root
    DB_PASSWORD=
    
  2. Ensure the phpunit.xml file is correctly configured: Your phpunit.xml file seems to be mostly correct, but double-check that the DB_DATABASE environment variable is set to testing:

    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="mysql"/>
        <env name="DB_DATABASE" value="testing"/>
        <env name="DB_USERNAME" value="root"/>
        <env name="DB_PASSWORD" value=""/>
        <!-- other environment variables -->
    </php>
    
  3. Check the config/database.php file: Ensure that your config/database.php file is set up to use the correct database connection when the environment is testing. It should look something like this:

    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            '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,
        ],
    ],
    
  4. Clear the configuration cache: Sometimes, Laravel caches the configuration, and changes might not take effect immediately. Run the following commands to clear the cache:

    sail artisan config:cache
    sail artisan config:clear
    
  5. Verify the environment during tests: Add a simple test to verify that the environment is set to testing and the database is correct:

    public function testEnvironmentIsTesting()
    {
        $this->assertEquals('testing', app()->environment());
        $this->assertEquals('testing', env('DB_DATABASE'));
    }
    
  6. Check for hardcoded database connections: Ensure that none of your tests or application code is hardcoding the database connection. All database connections should be retrieved from the environment configuration.

By following these steps, you should be able to ensure that your tests are using the correct test database. If the issue persists, double-check for any typos or misconfigurations in your environment files and configuration settings.

tisuchi's avatar

@vincent15000 what database system are you using? MySql or Sqlite?

Can you also make sure DB_CONNECTION?

    <env name="DB_CONNECTION" value="mysql"/> <!-- or :memory: for in-memory SQLite -->
1 like
tisuchi's avatar

@vincent15000 is just an alternative solution!

In production, if you use MySql, for the test use Sqlite (if possible). Solve the issue 🤷🏻.

1 like
Talinon's avatar

@vincent15000 The word "sometimes" is what stuck out to me about your post. Make sure you don't have your configuration cached.

I haven't tried that in a long time, so perhaps that is no longer an issue. But I know at least at one time tests would honour the cached config. I suspect they still do. Which would mean it would run the tests against your dev database instead of the connection configuration in phpunit.xml. In other words, never cache your dev enviornment.

1 like

Please or to participate in this conversation.