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

bryanhoffman's avatar

Running `php artisan test` deletes my database

Howdy!

I am following along with "Pest from Scratch" and when I run php artisan test my database is cleared almost entirely. I think I need to specify an alternative test database but it is unclear to me how I would do that yet. I appreciate any and all help.

Thanks!

0 likes
5 replies
LaryAI's avatar
Level 58

It sounds like your tests are running against your main database instead of a separate test database. This is a common issue but can be resolved by configuring Laravel to use a different database when running tests.

Here’s how you can set up a separate test database:

  1. Create a Test Database: First, create a new database that will be used exclusively for testing. You can do this through your database management tool (like phpMyAdmin, MySQL Workbench, etc.).

  2. Configure Environment: Open your .env.testing file in the root of your Laravel project. If it doesn't exist, create it by copying your .env file and renaming it to .env.testing. In this file, change the database configuration to point to your new test database. For example:

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=laravel_test
    DB_USERNAME=root
    DB_PASSWORD=
    

    Make sure to replace laravel_test, root, and the password with the appropriate values for your test database.

  3. Configure PHPUnit: Open your phpunit.xml file in the root of your Laravel project. Ensure that the environment variables for the database connection are set to use your test environment settings. It should look something like this:

    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="mysql"/>
        <env name="DB_DATABASE" value="laravel_test"/>
        <env name="DB_USERNAME" value="root"/>
        <env name="DB_PASSWORD" value=""/>
    </php>
    
  4. Run Migrations for Test Database: Before running your tests, make sure your test database has all the necessary tables. You can run migrations specifically for your test database by using:

    php artisan migrate --env=testing
    
  5. Run Tests: Now, when you run php artisan test, it should use your test database instead of your main database.

By following these steps, you can ensure that your main database remains unaffected when running tests, as all testing operations will be performed on the separate test database.

1 like
puklipo's avatar

In Laravel 11, the DB is deleted during testing in the initial state.

Let's change phpunit.xml to use in-memory SQLite.

// phpunit.xml

<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
1 like
Kortez12's avatar

@bryanhoffman Why tables are deleted is because in the Pest.php, there are trait RefreshDatabase, which deletes all tables from the database. You need to remove this trait if you do not want to delete tables from DB. Or like Larry suggested, create a new .env.testing file

mattsches's avatar

I was upgrading vom 10 to 11, and I already had an .env.testing file in place. I also have multiple DB connections:

# default
DB_DRIVER=sqlite
DB_DATABASE=":memory:"
#second
SECOND_DB_DRIVER=sqlite
SECOND_DB_DATABASE=":memory:"

This does not work with Laravel 11. I also tried setting the environment variables in phpunit.xml, but that did not work either. Removing the RefreshDatabase trait could be an option, but I'm using it in a lot of tests and also for custom DB commands, so I'd rather keep it.

For the time being, I'm stuck on Laravel 10, although the actual application seems to work just fine. But I cannot ship it without green tests ¯\(ツ)

Please or to participate in this conversation.