Pest PHP Testing: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Pest and Why Use It?

Pest is a modern, elegant testing framework for PHP, created by Nuno Maduro in 2021. Unlike traditional solutions like PHPUnit, Pest offers a more concise and expressive syntax inspired by Jest (JavaScript) and RSpec (Ruby). If PHPUnit is a "Swiss Army knife" for testing, Pest is a "scalpel" that lets you write tests faster, cleaner, and with less boilerplate code.

Pest's main goal is to make writing tests as enjoyable and intuitive as possible. Instead of classes and methods with long names, you use closures and a fluent interface. This is especially useful for projects where development speed and code readability matter — like startups, API services, or teams where both developers and QA engineers write tests.

Key advantages of Pest over PHPUnit:

  • Conciseness: Less code to write the same test.
  • Readability: Tests read like natural language (e.g., expect($result)->toBeTrue()).
  • Built-in helpers: beforeEach(), afterEach(), todo(), and more.
  • Architecture: No mandatory inheritance from TestCase.
  • Ecosystem: Plugins for Laravel, Livewire, Mockery, and other popular tools.

Installing Pest

Pest is installed via Composer, the standard package manager for PHP. Make sure you have PHP 8.0 or higher installed.

Create a new project or navigate to your existing project's root directory:

mkdir my-pest-project
cd my-pest-project
composer init --yes

Install Pest as a dev dependency:

composer require pestphp/pest --dev

After installation, initialize Pest in your project:

./vendor/bin/pest --init

This command creates the basic structure:

  • tests/ — directory for your tests.
  • tests/Pest.php — file for global settings (e.g., uses()).
  • phpunit.xml — configuration file (Pest runs on top of PHPUnit).

Now you can run your tests:

./vendor/bin/pest

If you're using Laravel, install the additional package for better integration:

composer require pestphp/pest-plugin-laravel --dev

Quick Start with Code Example

Create a file tests/ExampleTest.php:

<?php

test('basic assertion', function () {
    $result = 1 + 1;
    expect($result)->toBe(2);
});

Run the tests:

./vendor/bin/pest

You'll see a green output with PASS Tests\ExampleTest. Notice: we didn't use classes, annotation methods, or inheritance. Just the test() function and a closure. That's "modern testing" in action.

Alternative syntax using the it keyword (more readable for describing behavior):

it('performs basic addition', function () {
    $sum = 2 + 3;
    expect($sum)->toEqual(5);
});

Key Pest Methods and Functions

Below are 10 essential Pest methods and functions with detailed descriptions and examples.

1. test() — Defining a Test

What it does: Creates a new test. This is the primary function for defining a test scenario.

Parameters:

  • string $description — test description (displayed in the console).
  • callable $closure — closure containing the test logic.

Recommendations