testing php applications
Pest Installation And Syntax
Pest is a PHP testing framework with a concise functional syntax and tooling built around developer feedback. It runs on top of the PHPUnit ecosystem, so existing PHP testing knowledge remains useful.
Install It As A Development Dependency
Follow the current Pest installation guide for runtime requirements and Composer commands. Initialise the suite with the project-local binary and keep tests/Pest.php in version control.
composer require pestphp/pest --dev --with-all-dependencies
./vendor/bin/pest --init
./vendor/bin/pest
Describe Behaviour In Small Tests
Pest uses functions such as test() or it() and an expectation API. Keep the same engineering standards as PHPUnit: deterministic setup, clear behaviour, useful assertions, and appropriate integration coverage.
<?php
declare(strict_types=1);
function discount(int $totalPence): int
{
return $totalPence >= 10_000 ? intdiv($totalPence, 10) : 0;
}
test('order receives ten percent discount at threshold', function (): void {
expect(discount(10_000))->toBe(1_000);
});
Common Mistakes
- Choosing Pest only for shorter syntax while ignoring suite design.
- Using a global binary instead of the project dependency.
- Assuming every plugin is installed by default.
- Forgetting to check current Pest runtime requirements.
What To Practise
- Install and initialise Pest from official docs.
- Recognise
test(),it(), andexpect(). - Apply the same boundary decisions as PHPUnit.
Practice
Practice: Outline A First Pest Test
Write the Pest shape for checking a 10 percent discount at the threshold.
Requirements
- Use a descriptive test label.
- Call the rule with 10000 pence.
- Use an exact expectation.
- Keep the rule separate from the test.
Show solution
A small Pest test reads as a behaviour example.
<?php
declare(strict_types=1);
function discount(int $totalPence): int
{
return $totalPence >= 10_000 ? intdiv($totalPence, 10) : 0;
}
test('order receives ten percent discount at threshold', function (): void {
expect(discount(10_000))->toBe(1_000);
});
The label describes behaviour, and toBe() checks both the value and type.