If you start a fresh Laravel project today, your tests will be written in Pest. It has been the framework’s default testing tool since Laravel 11, and with Pest 5 landing in July 2026 it has grown from a prettier PHPUnit wrapper into a full testing platform: browser tests, architecture tests, mutation testing, and a test-impact engine that can shrink a three-minute suite to seconds. This guide covers Laravel Pest testing from a standing start — installation, the test types that actually earn their keep, and the Pest 5 features worth upgrading for.
Verified against the official Pest and Laravel documentation on 16 November 2026. Commands and versions below reflect Pest 5 and Laravel 13.
What Pest actually is (and what it isn’t)
Pest is a testing framework for PHP built on top of PHPUnit. That last part matters more than the marketing: Pest does not replace PHPUnit, it sits on it. Every PHPUnit assertion, every Laravel testing helper, and every factory still works. What changes is the surface you write against.
Instead of a class with a method name that encodes the behaviour being tested, you write a closure with a plain-English description:
test('it adds two numbers', function () {
expect(1 + 2)->toBe(3);
});
it('returns a successful response', function () {
$response = $this->get('/');
$response->assertOk();
});
The it() form is the same as test(), except Pest prefixes the description with “it” when it prints results — so it('returns a successful response') reads as a sentence in your terminal.
Here is how Pest compares with writing PHPUnit directly:
| Pest | PHPUnit alone | |
|---|---|---|
| Test syntax | Closures: test() / it() |
Classes and test_* methods |
| Assertions | expect()->toBe() chains, plus all PHPUnit assertions |
$this->assertEquals() and friends |
| Data-driven tests | Datasets, built in | Data providers, more ceremony |
| Architecture tests | Built in (arch()) |
Needs deptrac or phpat |
| Browser tests | First-party plugin (Playwright) | Needs Dusk or a separate tool |
| Mutation testing | First-party plugin | Needs Infection |
| Engine underneath | PHPUnit 13 (Pest 5) | PHPUnit directly |
The practical consequence: choosing Pest is not a rewrite decision. A suite can hold class-based PHPUnit tests and closure-based Pest tests side by side, so an existing codebase can migrate one file at a time — or never migrate at all and just write new tests in Pest.
Install Pest in Laravel 13
A fresh Laravel 13 app already has it
Laravel 13 shipped in March 2026, and a new application created with laravel new asks which test framework you want — Pest or PHPUnit — with Pest as the default choice. The skeleton installs Pest 4, not Pest 5, for a deliberate reason: Pest 5 requires PHP 8.4 or newer, while Laravel 13 still supports PHP 8.3. If your machine runs PHP 8.4+, upgrade to Pest 5 (covered below); if it runs 8.3, Pest 4 is the correct version for you and everything in this guide except the Pest 5 section still applies.
Your new app already contains a tests directory with Feature and Unit subdirectories and an example test in each. Run the suite with any of these:
./vendor/bin/pest
php artisan test
An existing Laravel app
If your project was scaffolded with PHPUnit, switching takes three commands. First remove PHPUnit, then require Pest with the Laravel plugin, then initialise it:
composer remove phpunit/phpunit
composer require pestphp/pest pestphp/pest-plugin-laravel --dev --with-all-dependencies
./vendor/bin/pest --init
The --with-all-dependencies flag tells Composer to update packages that conflict with Pest’s requirements instead of failing. The --init step creates tests/Pest.php, the file where you configure the suite — which test case class feature tests use, and which traits apply where:
<?php
uses(Tests\TestCase::class, Illuminate\Foundation\Testing\RefreshDatabase::class)
->in('Feature');
That one line is doing real work: every test in tests/Feature now boots the Laravel application and gets a fresh database for each test, with no boilerplate in the test files themselves. The Laravel plugin also adds the php artisan pest:install command, which wires the plugin into an existing project.
Generate new test files with Artisan, using the --pest flag for Pest-style files:
php artisan make:test PostControllerTest --pest
Upgrading to Pest 5
Pest 5 was announced at Laracon US 2026 on 29 July 2026, with Nuno Maduro tagging v5.0.0 live on stage. It requires PHP 8.4 or newer and runs on PHPUnit 13, so check your PHP version before upgrading:
php -v
composer require pestphp/pest:^5.0 --dev --with-all-dependencies
The headline additions are the Tia engine (test impact analysis), an Agent plugin for AI coding assistants, an Evals plugin for testing LLM output, PHPStan and Rector plugins, and new format expectations for emails, ULIDs, IP addresses and domains. The current 5.3.x line is what you will get from a fresh install today. If your app must stay on PHP 8.3, stay on Pest 4 — it is still supported and still the version Laravel 13 scaffolds.
Feature tests: where Pest earns its keep
Laravel’s own documentation gives the advice plainly: most of your tests should be feature tests, because they give the most confidence that the system as a whole works. A feature test boots the framework, hits a real route, and runs real queries against a test database — routing, middleware, validation, authorisation and persistence in one pass.
Here is a pair of tests for a simple post-publishing flow. The first checks that guests are turned away; the second checks that a signed-in author can publish:
<?php
use App\Models\Post;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
test('guests cannot create posts', function () {
$this->post('/posts', ['title' => 'Hello'])
->assertRedirect('/login');
});
test('authors can publish a post', function () {
$user = User::factory()->create();
$this->actingAs($user)
->post('/posts', [
'title' => 'Hello world',
'body' => 'First post.',
])
->assertRedirect()
->assertSessionHasNoErrors();
expect(Post::first()->title)->toBe('Hello world');
});
Three things to notice. First, RefreshDatabase migrates a fresh database before each test and rolls it back afterwards, so tests never interfere with each other or with your real data — and with SQLite in memory (the Laravel 13 default for testing) it is fast. Second, factories give you realistic rows in one line instead of hand-built fixtures. Third, the final expect() checks the database state, not just the HTTP response: the response being a redirect does not prove the post was saved.
For anything involving external services, fake them. Laravel’s fakes — Mail::fake(), Queue::fake(), Notification::fake(), Http::fake() — let you assert that a mail was queued or an API call was attempted without ever sending one. A test that hits a real payment gateway is not a test, it is a liability.
Datasets: one test, twenty inputs
Validation logic is where test suites usually balloon: the same test copied five times with different bad input. Pest datasets collapse that into one test that runs once per input:
it('rejects invalid email addresses', function (string $email) {
$response = $this->post('/register', [
'name' => 'Ada',
'email' => $email,
'password' => 'password',
'password_confirmation' => 'password',
]);
$response->assertSessionHasErrors('email');
})->with([
'not-an-email',
'missing-domain@',
'@no-local-part.com',
]);
Each entry in the with() array becomes a separate test run with its own name in the output, so a failure tells you exactly which input broke. Named datasets (defined once with dataset() in tests/Pest.php and reused across files with ->with('roles')) are worth the extra step the moment a second test file needs the same inputs. Datasets also accept closures, so you can generate inputs lazily — useful for factories and Faker data that should not be built until the test runs.
Architecture tests: automated code review
This is the Pest feature most tutorials skip, and the one that pays for itself fastest on a growing codebase. Architecture tests do not check what your code does; they check how it is written. You define structural rules once, and Pest enforces them on every run — the kind of thing code review keeps missing.
<?php
arch('no debugging statements ship')
->expect(['dd', 'dump', 'ray', 'var_dump'])
->not->toBeUsed();
arch('the app uses strict types')
->expect('App')
->toUseStrictTypes();
arch('controllers do not touch Eloquent directly')
->expect('App\Http\Controllers')
->not->toUse('Illuminate\Database\Eloquent\Builder');
arch('models extend Eloquent')
->expect('App\Models')
->toExtend('Illuminate\Database\Eloquent\Model');
Every expectation can be negated with ->not->, and the vocabulary is wide: toBeFinal(), toBeReadonly(), toBeInterfaces(), toOnlyUse(), toOnlyBeUsedIn(). If you do not want to write rules from scratch, presets cover common conventions in one line:
arch()->preset()->laravel();
arch()->preset()->php();
arch()->preset()->security();
The laravel() preset enforces Laravel conventions; php() and security() cover general PHP and security best practices. These tests run in milliseconds and live in tests/Arch, next to the rest of the suite — no separate tool, no separate CI step.
Browser tests without leaving Pest
Since Pest 4, browser testing is a first-party plugin that drives a real browser through Playwright. Your browser tests live in tests/Browser, run with the same ./vendor/bin/pest command, and — crucially — still have access to Laravel’s testing tools: factories, authentication, database refreshes, and fakes. That combination is what makes them more useful than a standalone Playwright script, which can see the page but cannot check whether the job was queued or the row was written.
Install the plugin and Playwright:
composer require pestphp/pest-plugin-browser --dev
npm install playwright@latest
npx playwright install
The last command downloads the browser binaries Playwright drives. Add tests/Browser/Screenshots and tests/Browser/Traces to your .gitignore so test-run artefacts are never committed. Then write the test:
it('shows the login page in a real browser', function () {
$page = visit('/login');
$page->assertSee('Log in');
});
The visit() helper returns a page object you chain assertions and interactions onto — clicking, typing, selecting, and asserting against the rendered page, JavaScript included. Two rules keep browser tests from ruining your suite: use them only for flows where the browser is the thing being verified (login, checkout, JavaScript-heavy interactions), and keep them few. Browser tests are an order of magnitude slower and flakier than feature tests, so a suite with hundreds of them is a suite nobody runs.
Mutation testing: prove your tests actually test
Code coverage tells you which lines your tests execute. It does not tell you whether your tests check anything. Mutation testing closes that gap: Pest makes small changes to your code — flipping a > to >=, changing a true to false — and checks whether your tests notice. If a test still passes after the change, that part of the code is not really tested, however green the coverage report looks.
composer require pestphp/pest-plugin-mutate --dev
./vendor/bin/pest --mutate --parallel
./vendor/bin/pest --mutate --min=80
The --min flag sets a mutation score threshold the run must clear, so you can enforce it in CI the same way teams enforce coverage minimums. Scope a run with covers() or mutates() in the test file when you only care about one class. Mutation testing needs a coverage driver (PCOV or Xdebug) and it is slow — it runs your suite once per mutant — so treat it as a periodic audit, not something that runs on every save. Start with the code that handles money, permissions, and data loss; that is where an unnoticed mutant hurts.
Make the suite fast: parallel runs and Tia
A slow suite is a suite developers stop running, so speed is a feature, not a luxury. Two switches cover most of it. First, parallel execution, which splits the suite across processes:
./vendor/bin/pest --parallel
php artisan test --parallel
Parallel runs need tests that do not share state — another reason RefreshDatabase and fakes matter. If two tests write to the same row, parallel execution will find that out for you, loudly.
Second, Pest 5’s Tia engine — test impact analysis. The first run records which tests touch which files; every run after that executes only the tests affected by what you changed, replaying cached results for the rest. The headline number comes from Laravel Cloud: a suite of more than 19,000 tests went from three minutes to five seconds. Tia detects Laravel, Symfony, Livewire, Inertia and browser assets automatically through Composer, so there is nothing to configure:
./vendor/bin/pest --parallel --tia
One rule: Tia is for local development. In CI, always run the whole suite — the point of CI is to catch the thing your local cache assumed was fine.
Run it in CI
A test suite that only runs on your laptop is a hobby. The minimal honest CI step runs the suite in parallel with a coverage floor:
- name: Run tests
run: php artisan test --parallel --coverage --min=80
If you use browser tests, add one step before the suite runs — npx playwright install --with-deps — so the runner has the browser binaries. And if your suite uses Tia locally, do not pass --tia here: CI runs everything, every time.
7 mistakes that make Pest suites miserable
1. Testing the framework instead of your code. A test that asserts Laravel’s router returns 404 for a missing route proves nothing about your app. Test your validation, your authorisation, your business rules.
2. One giant feature test per page. A test that creates a user, posts, comments, likes, and checks the feed is five tests wearing a trench coat. When it fails, you will not know which part broke.
3. Shared state between tests. Tests that depend on running order, or on a row another test created, will fail in parallel and flake in CI. RefreshDatabase exists precisely so each test starts from zero.
4. Hitting real external services. Real mail, real APIs, real payment sandboxes in tests mean slow suites and failures that have nothing to do with your code. Fake them, always.
5. Browser-testing everything. Browser tests are the most expensive tier. A handful guarding login and checkout; everything else belongs in feature tests.
6. Chasing 100% coverage. Coverage measures execution, not correctness — that is what mutation testing is for. A suite at 80% with meaningful assertions beats a suite at 100% full of tests that assert nothing.
7. Ignoring the slow tests. Run ./vendor/bin/pest --profile and look at the slowest ten. One test that sleeps, hits the network, or rebuilds the database will dominate your suite’s runtime — and your team’s patience.
Pest 5 extras worth knowing about
Two Pest 5 plugins deserve a mention even if you do not need them today. The Agent plugin gives an AI coding agent a single command to verify a change inside your real test suite — factories, fakes and all — and with the browser plugin installed it can drive a real browser and assert the backend side effects in the same probe:
composer require pestphp/pest-plugin-agent --dev
./vendor/bin/pest --agent='visit("/")->assertSee("Welcome");'
The Evals plugin scores LLM output with deterministic checks and AI-powered scorers through the same expect() API, so a feature that leans on an LLM can be tested like any other feature. And if your tests are written by an agent, the Rector plugin ships 60 rules that rewrite raw PHP assertions into idiomatic Pest, while the PHPStan plugin teaches PHPStan about it(), test(), expect() and the $this inside your test closures.
The short version
Start with feature tests — they give the most confidence per test written. Add datasets the first time you catch yourself copying a test to try another input. Add three architecture tests before your next pull request; they cost minutes and catch real mistakes. Add browser tests only for the two or three flows where the browser is the point. Run mutation testing once a month on the code that handles money and permissions. And if you are on PHP 8.4, upgrade to Pest 5 and let Tia make running the suite a reflex instead of a chore.
Further Reading & References
- Pest 5 Now Available — pestphp.com (official announcement: Tia, Agent plugin, Evals, PHP 8.4 / PHPUnit 13 baseline)
- Installation — Pest docs (official install and
--initflow) - Writing Tests — Pest docs (official
test()/it()and expectation API reference) - Browser Testing — Pest docs (official Playwright plugin setup and
visit()API) - Testing: Getting Started — Laravel 13.x docs (official Laravel testing guide: feature vs unit, environment, parallel runs)
- Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals — Laravel News (29 July 2026; the Laravel Cloud 19,000-test / 3-minutes-to-5-seconds Tia figure)
- Pest: PHP Testing That Reads Like Plain English — jump.bg (independent walkthrough run on Pest 5.3.4 and PHP 8.5, with real terminal output)
- Laravel Pest 4 Testing: The Complete Guide for Laravel 13 Developers — DEV (feature tests, datasets, architecture tests and parallel runs in one guide)
Internal reading on HowToShipIt: Laravel Sanctum Setup: Complete Auth Guide (2026) and Laravel 13.42: Typed UUID Input, Failed-Migration Alerts, and the Security Fix (2026).



