Laravel Testing: PHPUnit vs Pest PHP Compared
Laravel ships with PHPUnit out of the box, and for years it was the only serious option. Then Pest arrived in 2021, and the Laravel community split. Some teams migrated immediately. Others stayed with PHPUnit. Both are still active choices in 2026, and both have real trade-offs worth understanding before you commit either way.
This post compares the two side by side — syntax, architecture tests, expectations, and what migration actually looks like.
The Fundamental Difference
PHPUnit is a class-based testing framework. Every test file is a class that extends TestCase, and every test is a public method prefixed with test or annotated with @test.
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class OrderTotalTest extends TestCase
{
public function test_calculates_total_with_tax(): void
{
$order = new Order(subtotal: 100, taxRate: 0.1);
$this->assertEquals(110.0, $order->total());
}
/** @test */
public function it_applies_discount_before_tax(): void
{
$order = new Order(subtotal: 100, taxRate: 0.1, discount: 10);
$this->assertEquals(99.0, $order->total());
}
}Pest takes a different approach. Tests are top-level functions, not class methods. The file reads more like a spec than a class definition.
<?php
test('calculates total with tax', function () {
$order = new Order(subtotal: 100, taxRate: 0.1);
expect($order->total())->toBe(110.0);
});
it('applies discount before tax', function () {
$order = new Order(subtotal: 100, taxRate: 0.1, discount: 10);
expect($order->total())->toBe(99.0);
});Both test() and it() in Pest do the same thing — it() just reads better when the test description starts with a verb.
The Expectations API
PHPUnit uses assertion methods: assertEquals, assertTrue, assertInstanceOf, and so on. They work, but the parameter order is always a small cognitive load (assertEquals($expected, $actual) — expected first, always).
Pest's expectations API chains off the value under test, which reads more naturally:
// PHPUnit
$this->assertEquals(200, $response->status());
$this->assertStringContainsString('Laravel', $response->content());
$this->assertCount(3, $user->orders);
$this->assertInstanceOf(Collection::class, $result);
// Pest
expect($response->status())->toBe(200);
expect($response->content())->toContain('Laravel');
expect($user->orders)->toHaveCount(3);
expect($result)->toBeInstanceOf(Collection::class);Pest also supports chaining multiple expectations on one value:
expect($user)
->toBeInstanceOf(User::class)
->and($user->email)->toContain('@')
->and($user->created_at)->not->toBeNull();The not modifier works throughout — not->toBe(), not->toBeNull(), not->toHaveCount().
Datasets (Parametrized Tests)
PHPUnit handles parametrized tests through data providers:
/** @dataProvider orderAmounts */
public function test_tax_calculation(float $subtotal, float $expected): void
{
$order = new Order(subtotal: $subtotal, taxRate: 0.1);
$this->assertEquals($expected, $order->total());
}
public static function orderAmounts(): array
{
return [
'small order' => [100.0, 110.0],
'large order' => [500.0, 550.0],
'zero order' => [0.0, 0.0],
];
}Pest handles this with with():
it('calculates tax correctly', function (float $subtotal, float $expected) {
$order = new Order(subtotal: $subtotal, taxRate: 0.1);
expect($order->total())->toBe($expected);
})->with([
'small order' => [100.0, 110.0],
'large order' => [500.0, 550.0],
'zero order' => [0.0, 0.0],
]);Both work well. Pest's version keeps the test and its data in one place.
Architecture Tests
This is where Pest has a clear advantage over plain PHPUnit. Pest's architecture testing lets you write assertions about the structure of your codebase — not just behavior.
arch('models extend Eloquent Model')
->expect('App\Models')
->toExtend('Illuminate\Database\Eloquent\Model');
arch('controllers are not invokable')
->expect('App\Http\Controllers')
->not->toBeInvokable();
arch('no debugging functions in production code')
->expect('App')
->not->toUse(['dd', 'dump', 'var_dump', 'die']);
arch('service classes have no public state')
->expect('App\Services')
->toHavePropertiesOfType('private', 'string')
->or->not->toHavePublicProperties();These tests run as part of your normal test suite and catch architectural drift before it compounds. PHPUnit has no equivalent without third-party packages.
Setup and Teardown
PHPUnit uses setUp() and tearDown() methods:
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
}
protected function tearDown(): void
{
parent::tearDown();
// cleanup
}Pest uses beforeEach() and afterEach() (and beforeAll() / afterAll() for class-level setup):
beforeEach(function () {
$this->user = User::factory()->create();
});
afterEach(function () {
// cleanup
});The $this context in Pest tests is available and works as expected — it's bound to a test case instance internally.
Test Groups and Skipping
PHPUnit uses annotations:
/** @group slow */
public function test_large_import(): void { ... }
public function test_legacy_feature(): void
{
$this->markTestSkipped('Legacy code, remove in v3');
}Pest uses method chaining:
it('imports large datasets', function () {
// ...
})->group('slow');
it('processes legacy format', function () {
// ...
})->skip('Legacy code, remove in v3');
it('only runs on CI', function () {
// ...
})->skipOnWindows();Higher-Order Tests
Pest has a feature called higher-order tests that lets you skip the closure entirely for simple assertions:
it('has verified email')
->expect(fn() => User::factory()->create(['email_verified_at' => now()]))
->toHaveProperty('email_verified_at');For very simple cases this reduces boilerplate, though it can be harder to read for complex scenarios.
Migrating from PHPUnit to Pest
Pest is built on top of PHPUnit — it's not a replacement at the runtime level, it's a different syntax layer. That means you can migrate incrementally. Old PHPUnit test classes continue to work alongside new Pest files.
Install Pest in a Laravel project:
composer require pestphp/pest pestphp/pest-plugin-laravel --dev
php artisan pest:installThe pest:install command creates Pest.php in your tests directory. This file is where you configure global setup:
<?php
uses(Tests\TestCase::class)->in('Feature');
uses(Tests\TestCase::class, RefreshDatabase::class)->in('Feature/Database');The uses() call replaces the class inheritance model. Instead of every test class extending TestCase, you declare once at the top of Pest.php which test directories use which base classes and traits.
To convert an existing PHPUnit test file to Pest:
./vendor/bin/pest --initOr convert manually — a PHPUnit class method like:
public function test_user_can_register(): void
{
$this->postJson('/api/register', [...])
->assertStatus(201);
}Becomes:
it('allows user registration', function () {
$this->postJson('/api/register', [...])
->assertStatus(201);
});The Laravel HTTP testing methods ($this->get(), $this->postJson(), etc.) work identically in both.
Which One Should You Use?
Use PHPUnit if:
- Your team is already comfortable with it and sees no pain points
- You're maintaining a legacy codebase where consistency matters more than syntax
- You need maximum compatibility with CI tooling that parses PHPUnit XML output
Use Pest if:
- You're starting a new project
- You want architecture tests without additional packages
- Your team finds the class-based syntax noisy and prefers spec-style tests
- You want a better
--coverageoutput and Pest's built-in profiling
Both frameworks output the same JUnit XML format, integrate with the same CI systems, and support the same coverage tools. The choice is mostly about syntax preference and whether architecture tests matter to your team.
Running Tests
Both use the same command pattern in Laravel:
php artisan test # runs all tests
php artisan test --filter OrderTest # filter by class or method
php artisan test --group slow # run by group
php artisan test --parallel # parallel execution (requires brianium/paratest)With Pest directly:
./vendor/bin/pest
./vendor/bin/pest --filter "calculates total"
./vendor/bin/pest --group slow
./vendor/bin/pest --parallel
./vendor/bin/pest --coverageClosing Note
Neither PHPUnit nor Pest solves end-to-end coverage — they both stop at your application boundary. For monitoring production flows and catching regressions that unit tests miss, tools like HelpMeTest run continuous end-to-end checks against your live application, giving you a complete picture of what's actually working.