Pest PHP Testing Framework
repository·4.x·Indexed Apr 15, 2026
https://github.com/pestphp/pestPest is an elegant PHP testing framework designed to simplify test writing with a modern, expressive syntax. It integrates with the PHP testing ecosystem, offering parallel execution via ParaTest, browser testing capabilities in v4+, and an Expectation API for assertions. Features include debugging with dump(), Ray integration, and architectural assertions for classes, interfaces, traits, and enums. The framework supports strict types, file permissions, and naming convention checks.
What's inside Pest
Configure Test Directory
4.xSpecify a custom directory for test files using the
--test-directoryoption. This overrides the defaulttestsdirectory.Usage:
php bin/pest --test-directory tests/customYou can also use the short form with an equals sign:
php bin/pest --test-directory=tests/customSources:
bin/pestAccess Configuration
4.xUse the
pest()function to create a new Pest configuration instance. This allows you to modify configuration settings for the current test file.pest()->timeout(30);The configuration is scoped to the file where
pest()is called.Sources:
src/Functions.phpDynamic Method Calls
4.xPest dynamically resolves expectation methods. If a method doesn't exist on the value, it throws an error unless it's an Arch expectation or a mixin.Unsupported Expectation Methods
4.xCertain expectation methods are explicitly unsupported when used with the
notoperator. Attempting to use these will throw anInvalidExpectationerror.Unsupported methods:
not->toOnlyUse()not->toUseNothing()not->toOnlyBeUsedIn()not->toBeUsedInNothing()
If you attempt to use these methods, you will receive an error message listing the unsupported methods.
// This will throw an InvalidExpectation error expect('App\\Models\\User') ->not->toOnlyUse(); // Throws errorSources:
src/Expectations/OppositeExpectation.phpIntegration with Ray
4.xSend values to Ray for debugging:
expect($value)->ray('Debug with Ray');Debugging with `dump()` and `dd()`
4.xInspect values during tests without breaking the flow:
expect($value)->dump('Debug message'); expect($value)->dd(); // Dump and exitConditional debugging:
expect($value)->ddWhen($condition, 'Debug when true'); expect($value)->ddUnless($condition, 'Debug when false');Filter and Select Tests
4.xPest provides several flags to filter and select specific tests for execution:
--bail: Stop execution immediately upon the first test failure.--todos: Output a list of tests marked astodoto standard output.--notes: Output tests marked with notes to standard output.--issue <number>: Output tests associated with a specific issue number.--pr <number>or--pull-request <number>: Output tests associated with a specific pull request number.--retry: Run non-passing tests first and stop on the first error or failure.--dirty: Run only tests that have uncommitted changes according to Git.--flaky: Output tests marked as flaky to standard output.
Examples:
pest --bail pest --dirty pest --issue 123 pest --flakySources:
src/Plugins/Help.phpUnsupported Expectation Methods
4.xThe following methods are defined in the Expectation class but are not supported and will throw an
InvalidExpectationexception if called:toHaveSuspiciousCharacters(): Asserts that the source code does not include suspicious characters (currently unsupported).toBeUsed(): Asserts that a dependency is used (currently unsupported).
Attempting to use these methods will result in an error indicating the method is not supported.
Sources:
unknownAssert Invokable Classes
4.xUse the
toBeInvokablemethod to assert that a class is an invokable class (i.e., it implements the__invokemethod).Example:
expect('MyCallable')->toBeInvokable();Sources:
src/Expectation.phpAssert Trait Usage
4.xUse the
toUseTraitandtoUseTraitsmethods to assert that a class uses specific traits. These assertions check for traits used directly or nested within other traits.toUseTrait(string $trait): Asserts the class uses a single trait.toUseTraits(array|string $traits): Asserts the class uses one or multiple traits.
Example:
expect('MyClass')->toUseTrait('Loggable'); expect('MyClass')->toUseTraits(['Loggable', 'Serializable']);Sources:
src/Expectation.phpAssert Class Attributes and Magic Methods
4.xUse the
toHaveAttribute,toHaveConstructor, andtoHaveDestructormethods to assert the presence of specific class elements.toHaveAttribute(string $attribute): Asserts the class has a specific PHP attribute.toHaveConstructor(): Asserts the class has a__constructmethod.toHaveDestructor(): Asserts the class has a__destructmethod.
Example:
expect('MyClass')->toHaveAttribute('MyAttribute'); expect('MyClass')->toHaveConstructor(); expect('MyClass')->toHaveDestructor();Sources:
src/Expectation.php