What is Pest?
Pest is a testing framework built on top of PHPUnit and is the default test runner for new Laravel 11+ projects. It lets you write tests as closures with concise, expressive syntax while still having full access to PHPUnit’s assertion library.Pest tests run on PHPUnit under the hood, so they can coexist with existing PHPUnit test classes. Run them with
php artisan test or vendor/bin/pest.describe / it / test
test()
The simplest form. The first argument becomes the test description.it()
Reads naturally in English as “it should …“.describe()
Groups related tests and allows sharingbeforeEach() state within the group.
The Expectation API
expect() is Pest’s fluent assertion syntax. Chain matchers to build readable assertions.
Basic assertions
Assertions on models
Chaining with and()
Asserting over every element with each()
Datasets — parameterised tests
Inline datasets
Named datasets
Define reusable datasets in thetests/Datasets directory.
Testing with multiple values per row
Mocking with Mockery
Mocking a service
Spies
Spies call the real implementation and record what was called.Partial mocks
Stub specific methods while calling through to the real implementation for everything else.Faking HTTP requests
Http::fake() stubs HTTP responses without making real network calls.
Basic fake
Error responses
Simulating connection failures
Faking events, mail, and notifications
Event::fake()
Mail::fake()
Notification::fake()
Testing Artisan commands
Interactive commands
Database helpers
RefreshDatabase
Runs all migrations before the test suite and wraps each test in a database transaction that is rolled back afterward.LazilyRefreshDatabase
Defers migration until the first test that actually writes to the database. Faster when many tests in your suite do not touch the database.Architecture tests
Pest’s architecture testing API lets you enforce structural rules across your codebase. These run as part of your normal test suite.Code coverage
Xdebug or PCOV must be installed.Related page
Testing (basics)
Getting started with testing in Laravel and the
php artisan test command.