How to seed database migrations for laravel tests?

How to seed database migrations for laravel tests?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to seed database migrations for Laravel tests

Laravel's testing suite provides a convenient mechanism to handle database state through the DatabaseMigrations trait. When you apply this trait to your test class, Laravel automatically runs the migration stack before each test and rolls it back afterward. This ensures that every test starts with a fresh, clean slate populated only by your defined schema.

However, this automatic rollback behavior creates a specific friction point when dealing with seed data. If you rely on php artisan migrate --seed to populate your database with initial fixtures, the standard workflow breaks down in testing environments. The trait executes the migrations first, which populates the tables. Immediately after your test completes, the trait rolls back those migrations. When the next test begins, it re-runs the migration file again. Since the rollback process clears any data inserted by seeders during that previous run, the subsequent migration execution—which typically only contains the schema definition—leaves your database empty.

This results in a scenario where the first test passes with data, but every following test fails due to missing records. The root cause is that the DatabaseMigrations trait does not invoke seeder classes automatically; it strictly handles the migration logic. To resolve this without abandoning the benefits of automatic cleanup, you must explicitly trigger the seeders within the context of the migration process.

The most robust solution involves creating a custom trait or modifying your test class to call the seeder after the migrations run but before the rollback occurs. You can achieve this by defining a custom trait that extends DatabaseMigrations. In this custom implementation, you override the methods responsible for resetting the database to ensure the seeder is invoked during the migration step.

Alternatively, if you prefer not to write custom traits, you can manually call your seeder class within the test setup or directly in the test method before performing assertions. For instance:

use Database\Seeders\MySeeder;

public function setUp(): void
{
    parent::setUp();
    
    MySeeder::class; // This approach depends on specific Laravel versions and structure
}

A more reliable pattern, often preferred in modern Laravel versions where the DatabaseSeeder is a dedicated class, is to instantiate and call it explicitly. You would access the seeder via the database connection or import the seeder class directly:

use Database\Seeders\DatabaseSeeder;

public function setUp(): void
{
    parent::setUp();
    
    $this->loadDatabaseFixtures();
}

protected function loadDatabaseFixtures()
{
    $this->artisan('db:seed', ['--class' => 'DatabaseSeeder'])->assertExitCode(0);
}

However, using php artisan commands inside tests can sometimes be slower and less predictable than direct class instantiation. A cleaner approach is to import your specific seeder class and call it directly in the test method or a dedicated setup hook.

use App\DatabaseSeeders\MySeeder;

public function testBasicExample()
{
    // Manually seed before assertions
    MySeeder::class; 
    
    $response = $this->get('/');
    // ...
}

It is crucial to note that simply calling the seeder in setUp might conflict with how the trait handles the rollback. The ideal integration point is ensuring the seeder runs before the migration data is rolled back at the end of the test lifecycle, or re-seeding immediately after the rollback if your architecture demands it.

If you are using a dedicated DatabaseSeeder class located in database/seeders/DatabaseSeeder.php, you can call its call() method to trigger all associated seeders:

use Database\Seeders\DatabaseSeeder;

public function testBasicExample()
{
    DatabaseSeeder::class;
    
    $response = $this->get('/');
}

This explicit seeding strategy ensures that your tests receive the necessary data regardless of the automatic rollback mechanism triggered by the DatabaseMigrations trait. By separating the concern of schema setup from data population, you maintain test isolation while guaranteeing consistent state for every assertion.

Our network