Skip to main content

Testing for Humans: Laravel Pest

Published 3 min readMarkdown
Part of seriesThe Art of Laravel
Testing for Humans: Laravel Pest

If you’ve been around Laravel long enough, you know the pain of opening a test file and feeling like you just walked into a courtroom, too many words, too much formality, and definitely not enough joy. PHPUnit works… but let’s be honest, it’s not the most fun part of development.

Then we get the improved companion and it’s called Pest. It took the whole Laravel testing experience and said, “Alright, let’s chill.” And honestly, that’s what I love most about it, cleaner syntax, human-friendly structure, and the freedom to write tests the way we actually think.

In this article, I’ll walk you through how I use Pest in my everyday Laravel projects, why it feels so natural, and some examples you can steal for your own codebase.


Why Pest Feels More Human

Most testing frameworks are powerful, but they want you to write like a robot. Pest, on the other hand, reads almost like sentences. It doesn’t try too hard, it’s simple, elegant, and gets out of your way.

Here’s what immediately stood out to me:

  • No class boilerplate

  • Cleaner syntax (no public function test_something_weird_here())

  • Expect API is 🔥

  • Perfect integration with Laravel

  • You write just the test, nothing else

If you like code that feels good to write, Pest is your framework.


Setup: Two Minutes and You’re In

If you’re using Laravel 9+, Pest support is basically native. Just run:

composer require pestphp/pest --dev
php artisan pest:install

Your tests/ folder gets cleaned up, Pest config is added, and your old PHPUnit tests still work. Zero stress.


A Simple Example (That Actually Feels Nice)

Let’s say you want to test if your homepage loads.

PHPUnit way:

public function test_home_page_loads_successfully()
{
    $response = $this->get('/');
    $response->assertStatus(200);
}

Pest way:

it('loads the home page', function () {
    $this->get('/')->assertOk();
});

Notice the vibe? The Pest one reads like a sentence. You’re just expressing an idea, not drowning in structure.


Testing Validation Like a Human

If you’re doing form submissions or API endpoints, Pest makes these tests feel natural.

Example: Testing a required field

it('requires a name for creating a user', function () {
    $this->post('/users', [
        'name' => '',
    ])->assertSessionHasErrors('name');
});

Using the Expect API (My Personal Favourite)

The expect() syntax is honestly the best part of Pest. It turns tests into tiny readable checks.

Example:

// Case 01
expect(2 + 3)->toBe(5);

// Case 02
$user = User::factory()->create();

expect($user->isActive())->toBeTrue();

It’s small things like this that make Pest feel good.


Database Tests That Don’t Feel Like Work

Laravel + Pest makes database testing incredibly smooth.

Example: Testing user creation

it('creates a user', function () {
    $response = $this->post('/users', [
        'name' => 'Amindu',
        'email' => 'amindu@test.com',
        'password' => 'secret123',
    ]);

    $response->assertRedirect();

    expect(User::where('email', 'amindu@test.com')->exists())->toBeTrue();
});

Grouping Tests With “describe”

When your feature grows, grouping tests under a “describe” block keeps things neat.

describe('User Management', function () {

    it('lists all users', function () {
        $this->get('/users')->assertOk();
    });

    it('can update a user', function () {
        $user = User::factory()->create();

        $this->put("/users/{$user->id}", [
            'name' => 'New Name'
        ]);

        expect($user->fresh()->name)->toBe('New Name');
    });

});

This style feels like writing documentation, but it runs as tests.


A Quick Tip: Use Higher-Order Tests

When you skip the function wrapper, Pest becomes even more magical.

it('returns 200', function () {
    $this->get('/')->assertOk();
});

can simply be:

it('returns 200')->get('/')->assertOk();

Final Thoughts

Testing shouldn’t make you feel like you’re filing legal documents. Pest proves that tests can be readable, simple, and even enjoyable.

If you’re building Laravel apps today, honestly, give Pest a try. It’s the closest thing to “testing for humans” that we’ve ever had.

Whether you’re working on a small feature or building a massive platform, Pest keeps the testing layer clean and expressive, so you can focus on writing better code instead of fighting syntax.

Happy testing! 🚀

About the author

  • Seasoned IT professional with 7+ years of Program & Project Management, Enterprise and Information Architecture, Technical Delivery (IT Manager), IT Service Management, Team Building, Vendor Management, and Technology experience. Solid track record of leading and managing teams and delivering solutions of business value, use of diverse technologies in the fields of Business Intelligence and Enterprise Data Warehouse, Applications, Databases, Web Portals, Infrastructure, Business Products, and various IT solutions.

No comments yet. Be the first to comment.

The Art of Laravel

Part 4 of 12

Laravel isn’t just a framework — it’s a craft. The Art of Laravel captures that spirit, blending technical depth with developer wisdom to help you master queues, authentication, Octane, and more — building apps as elegant as the framework itself.

Up nextLaravel's Big JourneyWhat Really Happens From Request to Response