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

lemmon's avatar
Level 28

PHPUnit RefreshDatabase not clearing database

Laravel 10 Breeze/Blade starter kit. I am using a seprate database to run tests RefreshDatabase is not clearing out the database. When I run all the tests one fails but when I run it by itself it fails? Please help

0 likes
3 replies
LaryAI's avatar
Level 58

It sounds like you're encountering an issue where your database is not being reset between tests when using the RefreshDatabase trait in your PHPUnit tests with Laravel. This trait is supposed to roll back the database to its original state after each test, ensuring that each test runs in a fresh state.

Here are a few steps you can take to troubleshoot and resolve this issue:

  1. Check Database Configuration: Ensure that you have a separate database configured for testing in your phpunit.xml file or in your .env.testing file. It should look something like this in your phpunit.xml:
<php>
    <env name="DB_CONNECTION" value="sqlite"/>
    <env name="DB_DATABASE" value=":memory:"/>
    <!-- Or if you're using a different database -->
    <env name="DB_CONNECTION" value="mysql"/>
    <env name="DB_DATABASE" value="your_test_database_name"/>
</php>
  1. Use Transactions: The RefreshDatabase trait uses transactions to reset the database state. Make sure that the database you are using supports transactions and that you are not using any features that could prevent transactions from working correctly, such as DB::statement('SET FOREIGN_KEY_CHECKS=0;').

  2. Migrations: Ensure that your migrations are set up correctly and can be rolled back. The RefreshDatabase trait runs migrations before each test, so any issues with migrations could cause problems.

  3. Test Isolation: If a test is failing when run with others but passes when run alone, it could be a sign of tests not being properly isolated. Make sure that each test is independent and does not rely on the state created by another test.

  4. Artisan Commands: You can try running the following Artisan commands to clear the test database and rerun migrations:

php artisan migrate:fresh --env=testing
  1. Debugging: Add debugging statements to your tests to ensure that the RefreshDatabase trait is being used. You can also check the database directly after a test runs to see if the data is being persisted when it shouldn't be.

Here's an example of a test class using the RefreshDatabase trait:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    use RefreshDatabase;

    public function testBasicTest()
    {
        // Your test code here
    }
}

If you've gone through these steps and are still facing issues, it might be helpful to provide more specific information about the test that is failing and the exact error message you're receiving. This will help in diagnosing the problem more accurately.

lemmon's avatar
Level 28

All that sounds good but it is one of the test that comes with breeze and I have not wrote any tests myself and it is just this one and when you run it by itself it all passes but when you run the whole bundle just that one test fails.

lemmon's avatar
lemmon
OP
Best Answer
Level 28

This was the error I was getting: Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes so I tried:

use Illuminate\Support\Facades\Schema;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
}

And I guess phpunit did not like it so I removed that and changed the database engine to 'InnoDB' and that solved it. I hope it does not cause other problems.

Please or to participate in this conversation.