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

Rretzko's avatar
Level 15

Pest Test wipes MySql database

Hi folks - I'm just starting with using Pest for testing and its working except that it is wiping my local MySql database with each test. I thought I followed the instructions and found some addition help in this forum, but I haven't figured it out yet. I've set up my phpunit.xml for sqlite:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory>tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory>tests/Feature</directory>
        </testsuite>
    </testsuites>
    <source>
        <include>
            <directory>app</directory>
        </include>
    </source>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <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>
</phpunit>

My database.config has a sqlite driver

 'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 
...

and I've created a .env.testing file

APP_NAME=TDR<dev2025>
APP_ENV=testing
APP_KEY=base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
APP_DEBUG=true
APP_URL=http://localhost

DEBUGBAR_ENABLED=false

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_DEFAULT=sqlite_testing
...

And I'm using the RefreshDatabase method in my tests.

<?php

use function Pest\Laravel\get;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('returns a successful response for welcome page', function () {

    // Act & Assert
    get(route('welcome'))
        ->assertOk();
});

it('shows cards for dashboard page navigation', function () {

    // Arrange
    $user = \App\Models\User::factory()->create();

    $this->actingAs($user);


    // Act & Assert
    get(route('dashboard'))
        ->assertOk()
        ->assertSeeText([
            'schools', //students,events,profile
            ]);
});
...

Any help in figuring out what I've missed (or mistyped) is appreciated! Best - Rick

0 likes
2 replies
LaryAI's avatar
Level 58

It looks like you've set up most of the configuration correctly for using SQLite as your testing database. However, there's a small discrepancy in your .env.testing file that might be causing the issue. You've set DB_DEFAULT instead of DB_CONNECTION. The DB_CONNECTION environment variable is what Laravel uses to determine which database connection to use.

Here's how you can fix it:

  1. Update your .env.testing file to use the correct environment variable for specifying the database connection. Change DB_DEFAULT to DB_CONNECTION:
DB_CONNECTION=sqlite
DB_DATABASE=:memory:
  1. Make sure that your .env.testing file is being used when running Pest tests. You can specify the environment file to use by setting the --env option when running Pest, or by configuring your testing environment to automatically pick up the .env.testing file.

  2. Ensure that your PHPUnit configuration in phpunit.xml is not being overridden somewhere else. Pest uses this file, so the settings here should be respected.

  3. If you're using Laravel, you can also set the configuration programmatically in your test setup method to ensure that the SQLite database is being used:

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

    config()->set('database.default', 'sqlite');
    config()->set('database.connections.sqlite.database', ':memory:');
}
  1. Finally, make sure to clear your configuration cache before running tests, as cached configuration might still be pointing to the MySQL database. You can do this by running:
php artisan config:clear

Or, if you're using Pest, you can add a script in your composer.json to clear the config before running tests:

"scripts": {
    "test": [
        "php artisan config:clear",
        "pest"
    ]
}

Then you can run your tests with composer test.

After making these changes, your tests should run using the SQLite database in memory, and your local MySQL database should remain untouched.

Please or to participate in this conversation.