Introduction
Laravel provides first-class support for testing database-driven applications. You can combine database reset traits, model factories, and test-specific assertions to write fast and reliable feature tests.
In most cases, start with RefreshDatabase. It keeps tests isolated while remaining faster than fully rebuilding the database every time.
Resetting the database after each test
To prevent data from one test affecting another test, use one of Laravel’s database reset traits.
RefreshDatabase checks whether the database schema is already up to date. If it is, Laravel runs each test in a transaction. If it is not, Laravel runs migrations first.
If you need a full reset strategy instead of transaction-based isolation, use the following traits:
Model factories
Before asserting behavior, you often need test data. Laravel model factories provide expressive defaults for creating Eloquent models.
For full factory definitions and advanced states / relationships, see Eloquent Factories.
Running seeders
Use the seed() method to populate data during tests. Without arguments, it runs DatabaseSeeder. You can also run a specific seeder class or an array of classes.
You may also seed automatically for tests using RefreshDatabase by using attributes:
To run a specific seeder automatically, use #[Seeder(...)]:
Available assertions
Laravel provides dedicated database assertions for both Pest and PHPUnit feature tests.
assertDatabaseCount
Assert that a table contains the expected number of records.
assertDatabaseEmpty
Assert that a table contains no records.
assertDatabaseHas
Assert that a table contains records matching the given attributes.
assertDatabaseMissing
Assert that a table does not contain records matching the given attributes.
assertSoftDeleted
Assert that the given model has been soft deleted.
assertNotSoftDeleted
Assert that the given model has not been soft deleted.
assertModelExists
Assert that the given model (or collection of models) exists in the database.
assertModelMissing
Assert that the given model (or collection of models) does not exist in the database.
expectsDatabaseQueryCount
Call this at the beginning of a test to assert the exact total number of database queries executed.
Last modified on May 29, 2026