0
0
Laravelframework~10 mins

Database testing (RefreshDatabase) in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Database testing (RefreshDatabase)
Start Test
RefreshDatabase Trait
Run Database Migrations
Execute Test Code
Rollback or Reset DB
Test Ends
This flow shows how the RefreshDatabase trait resets the database before each test by running migrations, ensuring a clean state.
Execution Sample
Laravel
<?php
use IlluminateFoundationTestingRefreshDatabase;

class UserTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_creation()
    {
        User::factory()->create();
        $this->assertDatabaseCount('users', 1);
    }
}
This test uses RefreshDatabase to reset the DB, creates a user, then checks the users table has exactly one record.
Execution Table
StepActionDatabase StateTest AssertionResult
1Start test methodEmpty databaseNo assertion yetContinue
2RefreshDatabase runs migrationsTables created, emptyNo assertion yetContinue
3User factory creates 1 userUsers table has 1 recordNo assertion yetContinue
4Assert users count is 1Users table has 1 recordAssertion passesContinue
5Test ends, database resetDatabase cleanedTest completeExit
💡 Test ends after assertion passes and database is reset for next test
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Database StateEmptyMigrations run, empty tablesUsers table: 1 userUsers table: 1 userCleaned/reset
Key Moments - 2 Insights
Why does the database get reset before each test?
The RefreshDatabase trait runs migrations fresh before each test (see execution_table step 2), so tests start with a clean database to avoid leftover data affecting results.
What happens if I create multiple users in one test?
Each user creation adds records to the database (like step 3). The database keeps these until the test ends and resets, so assertions can check exact counts during the test.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the database state after migrations run?
ADatabase is full of test data
BUsers table has 1 user
CEmpty tables created
DDatabase is not changed
💡 Hint
Check execution_table row 2 under Database State
At which step does the test check if the users table has exactly one record?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at execution_table row 4 under Test Assertion
If you remove RefreshDatabase trait, what would likely happen?
ATest might fail due to leftover data
BDatabase resets automatically anyway
CTest runs faster
DMigrations run twice
💡 Hint
Recall why RefreshDatabase resets DB before each test (see key_moments)
Concept Snapshot
Use RefreshDatabase trait in Laravel tests to reset the database before each test.
It runs migrations fresh, so tests start with a clean state.
Create test data inside tests, then assert database contents.
Database resets after test ends to avoid data leaks.
This ensures reliable, isolated database tests.
Full Transcript
In Laravel testing, the RefreshDatabase trait resets the database before each test by running migrations fresh. This means each test starts with an empty database with all tables created. When the test runs, you can create data like users using factories. Then you assert the database state, for example checking the number of users. After the test finishes, the database is cleaned again to prepare for the next test. This process avoids leftover data from one test affecting another, making tests reliable and isolated.