How to seed database migrations for laravel tests?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Seeding Database Migrations for Laravel Tests with Comprehensive Guide Introduction: Laravel is an excellent PHP framework that provides powerful tools to handle your application's data persistence. In some cases, you might need to use specific seed data in your tests. This blog post will walk you through the process of ensuring your database seeds are applied consistently between tests using the DatabaseMigrations trait and a few simple yet effective methods. 1. Define Seed Data and Tests: To understand this topic better, let's first define the concepts of seed data, migrations, and tests. Seeds help establish initial data in your application while migrations are responsible for applying schema changes to the database. Lastly, tests allow you to verify that the system functions as intended through automated scripts. 2. The Limitations with DatabaseMigrations: Laravel's documentation on testing recommends using the DatabaseMigrations trait for managing data persistence during your test execution. However, this method only migrates and rolls back the database after each test. This means that subsequent tests may fail if they rely on previously seeded data. 3. Solution 1: Manual Seeding Before Each Test Case: To solve this issue, you can manually invoke the seeding command before executing your tests. Make sure to use a wrapper script or shell command for this purpose. For example: - Create an additional command in your application's config/app.php file using: 'Illuminate\Foundation\Testing\DatabaseMigrations'; - Run `php artisan migrate --seed` before running the tests. 4. Solution 2: Custom Test Environment with Seeding: You can also create a custom test environment that automatically runs the seeding command prior to each test execution. To do this, follow these steps: - Create a new file named 'custom_test_environment.php' in your project's tests/ directory; - Set the appropriate configuration for migrations and seeds in this file (refer to Laravel documentation); - Modify your TestCase class by extending the DatabaseMigrationsTrait instead of just DatabaseMigrations: 'use Illuminate\Foundation\Testing\DatabaseMigrationsTrait'; - Override the getEnvironmentSetUp method to call the custom test environment: public function getEnvironmentSetUp() { parent::getEnvironmentSetUp(); $this->loadEnvironmentFrom(file_exists('custom_test_environment.php') ? 'custom_test_environment' : null); } 5. Solution 3: Laravel-Enhanced DatabaseMigrations Trait: A third solution would be to use a custom trait that extends the DatabaseMigrations trait from Laravel. Make sure to include this new trait in your tests and modify it to ensure proper seeding before each test execution. 6. Conclusion: Seeding database migrations for Laravel tests can be challenging, but with careful planning and methodical approaches, you can have consistent data persistence across all your tests. Utilize the recommended solutions above, maintain a clear structure in your application's configuration files, and keep an eye out for new updates from the Laravel community to ensure seamless integration of these features. Remember that using proper naming conventions for seeds and migrations will make it easier to manage and organize them within your project. And always keep track of any database changes in your version control system to maintain consistency across team members.