Testing Laravel Apps with Pest: A Practical Setup

Pest gives Laravel tests a cleaner syntax than PHPUnit without losing any power. Here's a practical setup for HTTP, database, and feature tests.

Richard GamoraRichard GamoraFullstack developer·4 min read
LaravelPestTesting

I switched a Laravel codebase from PHPUnit to Pest a couple of years ago. The team's reaction surprised me — every developer wrote more tests after the migration. Same coverage targets, same project, much cleaner syntax. Here is the setup that actually works.

Installing alongside PHPUnit

Pest is built on top of PHPUnit, so you can install it without removing PHPUnit. Run composer require pestphp/pest --dev --with-all-dependencies and php artisan pest:install. Existing PHPUnit tests keep working while you migrate.

Writing HTTP tests

phpit('lists active products', function () {
    Product::factory()->count(3)->create(['active' => true]);
    Product::factory()->create(['active' => false]);

    $this->getJson('/api/products')
        ->assertOk()
        ->assertJsonCount(3, 'data');
});

Compare that to the equivalent PHPUnit class with setUp() boilerplate, public function methods, and $this in every assertion. Pest is the same test, two-thirds the lines.

Database setup

Use the RefreshDatabase trait globally in tests/Pest.php so every test runs in a transaction that rolls back at the end. Then your factories, seeders, and assertions run against a clean state without managing it manually.

Where Pest does not help

Pest's syntactic sugar is great for tests with simple, sequential setup. For complex scenarios with many model relationships and stateful setup, the it() blocks can get nested and confusing. In those cases, fall back to a class-style test or use Pest's beforeEach() hooks.

The honest pitch for Pest is not that it is more powerful — it is that the lower friction makes the team write more tests. Coverage rises without anyone working harder, which is the most you can ask of a tooling change.

About the author

Richard Gamora

Richard Gamora

Fullstack developer based in the Philippines, working mostly with Laravel and Vue.js, with eight years of production experience across web and mobile.

me@richardgamora.comUpwork ↗

More on Laravel