Skip to main content

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 sharing beforeEach() state within the group.
describe() blocks can be nested, but keep the depth to two levels to keep tests readable.

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 the tests/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()

Fake only specific events and let others propagate normally:

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.
For most projects, RefreshDatabase is the safer choice. Consider LazilyRefreshDatabase when you have a large number of tests that do not interact with 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.
Coverage measurement significantly increases test run time. In CI, run coverage in a dedicated job or only on pull requests to avoid slowing down your main pipeline.

Testing (basics)

Getting started with testing in Laravel and the php artisan test command.
Last modified on March 29, 2026