Skip to main content

Why write tests?

Tests verify that your code behaves as expected and alert you when changes break existing functionality. In team projects, a good test suite lets everyone refactor and ship with confidence. Laravel has first-class support for testing built in. Every new project ships with a phpunit.xml configuration file and a tests/ directory, and supports both Pest and PHPUnit.
Pest is built on top of PHPUnit and offers a more concise, readable syntax. If you’re just getting started with testing, Pest is the recommended choice.

The tests/ directory

  • Feature/ — Tests that cover larger units of functionality, including HTTP requests. Most of your tests will live here.
  • Unit/ — Tests for individual classes or methods in isolation. The Laravel application is not booted, so these tests cannot use the database or other framework features.

Creating tests

Use the make:test Artisan command to generate a new test file:

Running tests

Run all tests with:
You can also run vendor/bin/pest or vendor/bin/phpunit directly, but php artisan test produces more readable output. Useful options:

Writing tests

Basic assertions

Common assertions

HTTP tests

Laravel’s HTTP testing tools let you simulate requests to your application without running a real HTTP server. This is done inside Feature/ tests.

Testing a page response

Testing CRUD operations

Using a Post model as an example:

Useful response assertions

The RefreshDatabase trait resets the database after each test, preventing data from one test leaking into another.

Testing authenticated routes

Use actingAs() to authenticate a user for a test:

Test environment configuration

phpunit.xml

The phpunit.xml file at the project root configures the test environment. By default, the session and cache use the array driver so data is not persisted between requests:
For database tests, use an in-memory SQLite database to keep tests fast and self-contained:

.env.testing

Create a .env.testing file at the project root to override environment variables specifically for the test environment. Laravel loads this file instead of .env when running tests:
If you have cached your configuration, run php artisan config:clear before running tests. Stale cache can cause your test environment variables to be ignored.

Running tests in parallel

As your test suite grows, execution time increases. The --parallel flag runs tests across multiple processes simultaneously. First install the brianium/paratest package, then use the flag:
By default, Laravel creates one process per CPU core. Control this with --processes:
Parallel tests require each process to have its own isolated database. Using RefreshDatabase with in-memory SQLite (configured in phpunit.xml) satisfies this requirement automatically.

Next steps

HTTP testing

Explore advanced HTTP testing — authenticated requests, JSON assertions, file uploads, and more.
Last modified on April 25, 2026