Web Development 5-8 minutes

Pest 3: The Elegant PHP Testing Framework Laravel Uses by Default

Diego Cortés
Diego Cortés
Full Stack Developer & SEO Specialist
Share:
Pest 3: The Elegant PHP Testing Framework Laravel Uses by Default

Pest 3, released in September 2024, has been Laravel's default testing framework since v11 and now adds Playwright browser testing. Migrating from PHPUnit is usually a one-line change: this guide shows you how, plus mutation testing, arch testing, and browser tests.

What Pest Is and Why It's the Laravel Standard

Pest is a testing framework for PHP built on top of PHPUnit. It doesn't replace the testing engine: it wraps it in a cleaner, more human-friendly syntax, so your tests read like sentences instead of methods with repeated prefixes. That readability, combined with tools PHPUnit doesn't ship with, made it the default choice of the Laravel ecosystem since version 11.

An Expressive Syntax Built on Top of PHPUnit

The key to Pest is that any PHPUnit test keeps working, but you can write them in a much more direct way. Instead of classes with methods starting with test, you define functions with descriptive phrases and helpers like it, test, or expect. The same test takes half the lines and reads like a specification, which lowers the friction of writing and maintaining your suite. Pest 3 requires PHP 8.2 or higher and is built on PHPUnit 11, so you lose none of the power you already know.

Adoption: From Laravel 11 to Laravel 13

Adoption wasn't anecdotal: Pest has been Laravel's default testing framework since v11 and already surpassed 18 million downloads when Pest 3 was announced at Laracon US 2024. It has kept growing ever since, and today it's common to find it in Laravel 12 and 13 projects alongside PHPStan for static analysis and Pint for formatting, forming the standard quality stack of the ecosystem.

Installation and Migration from PHPUnit

Starting from scratch takes two commands. You add Pest as a dev dependency and initialize the configuration:

composer require pestphp/pest --dev
./vendor/bin/pest --init

The init command generates the Pest.php file with the required usings and leaves your suite ready. If you already have tests written with PHPUnit, you don't need to rewrite them: Pest runs them as they are. And if you want to convert your suite to the new style, the upgrade command does it for you in a single step for most projects, updating the syntax of your existing tests automatically.

Mutation Testing: Find Untested Code

Mutation testing is one of Pest 3's biggest selling points. The idea is simple but powerful: the framework modifies your source code in a controlled way, introducing small changes (mutations) such as inverting a condition or swapping an operator, and runs the suite against each mutated version. If your tests catch the mutation, the mutant dies and that part of the code is properly covered; if the tests still pass, you've found code that isn't actually being verified. Running it is as easy as adding a flag:

./vendor/bin/pest --mutate

The resulting report tells you which mutants survived, meaning exactly which lines of your code can break without your suite noticing. It's the most direct way to measure the real quality of your tests, going beyond the traditional coverage percentage.

Arch Testing: Architecture Rules in Your Suite

Arch testing turns architecture conventions into executable tests. Instead of relying on team discipline, you write rules that verify, for example, that all controllers end with Controller or that services don't depend directly on Eloquent. If someone breaks the rule, the suite fails and the error points to the exact file that violates it.

Arch Presets and Custom Architectural Tests

Pest 3 ships ready-to-use presets, like the Laravel preset, which apply the framework's most common conventions in one go. On top of them you can add your own rules with a declarative syntax:

arch()->preset()->laravel();

arch('controllers')
    ->expect('App\\Http\\Controllers')
    ->toHaveSuffix('Controller');

These rules run like any other test and are ideal for keeping architecture under control in large teams, long-lived projects, or codebases that pass through many hands.

Browser Testing with Playwright

Pest's browser tests are regular Pest tests in which Playwright drives a real browser. That means you can visit pages, fill in forms, click, and verify the result in a real Chrome or Firefox browser while keeping the same expressive syntax as the rest of your suite. A minimal example:

it('logs the user in', function () {
    $this->browse(function (Browser $browser) {
        $browser->visit('/login')
            ->type('email', 'diego@example.com')
            ->type('password', 'secret')
            ->press('Login')
            ->assertPathIs('/dashboard');
    });
});

The advantage over traditional HTTP tests is obvious: you test the JavaScript, the redirects, and the real behavior of the interface, not just the server response. For Laravel apps with Livewire components or heavy frontends, this level of verification is what actually prevents regressions in production.

AI Evals and the Tia Engine: Testing for Agents

Pest's official website now presents it as the testing framework "for developers and AI agents," and it's not empty marketing. The suite includes AI evals to evaluate model responses and an agent browser that verifies the work of coding agents, plus the Tia Engine, designed for near-instant execution. The release is described as the biggest Pest release to date, with a one-line upgrade for most suites. If you work with agents that generate or modify code, having a framework that puts them to the test is a qualitative leap.

Hands-On: A Complete Suite in Minutes

To see it all together, imagine a fresh Laravel project. After installing Pest, you write a unit test for a domain class, a feature test that verifies an authenticated route, an arch testing rule, and a browser test. Everything coexists in the same suite, runs with a single command, and reads like a specification of the expected behavior. The quality flow is complete by adding PHPStan for static analysis and Pint for style, and you can hook Pest into your CI pipeline so every pull request checks tests, mutations, and architecture automatically.

Conclusion

Pest 3 has made testing in PHP a much more enjoyable experience, without reinventing the wheel: it runs on PHPUnit, migrates in one line, and adds mutation testing, arch testing, and Playwright browser tests that cover what traditional coverage can't see. If you work with Laravel, it's the standard the ecosystem has already chosen. To round out your quality stack, check out our guide on SQLite in production with Laravel and our look at PHP 8.5, nine months later.

Categories