Pest PHP Testing Framework

repository·4.x·Indexed Apr 15, 2026

https://github.com/pestphp/pest

Pest 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.

Tokens
15.3K
Snippets
82
Records
92
Agent score
95%

What's inside Pest

  1. Configure Test Directory

    4.x

    Specify a custom directory for test files using the --test-directory option. This overrides the default tests directory.

    Usage:

    php bin/pest --test-directory tests/custom

    You can also use the short form with an equals sign:

    php bin/pest --test-directory=tests/custom

    Sources: bin/pest

  2. Access Configuration

    4.x

    Use 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.php

  3. Dynamic Method Calls

    4.x
    Pest 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.
  4. Unsupported Expectation Methods

    4.x

    Certain expectation methods are explicitly unsupported when used with the not operator. Attempting to use these will throw an InvalidExpectation error.

    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 error

    Sources: src/Expectations/OppositeExpectation.php

  5. Debugging with `dump()` and `dd()`

    4.x

    Inspect values during tests without breaking the flow:

    expect($value)->dump('Debug message');
    expect($value)->dd(); // Dump and exit

    Conditional debugging:

    expect($value)->ddWhen($condition, 'Debug when true');
    expect($value)->ddUnless($condition, 'Debug when false');
  6. Filter and Select Tests

    4.x

    Pest 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 as todo to 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 --flaky

    Sources: src/Plugins/Help.php

  7. Unsupported Expectation Methods

    4.x

    The following methods are defined in the Expectation class but are not supported and will throw an InvalidExpectation exception 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: unknown

  8. Assert Invokable Classes

    4.x

    Use the toBeInvokable method to assert that a class is an invokable class (i.e., it implements the __invoke method).

    Example:

    expect('MyCallable')->toBeInvokable();

    Sources: src/Expectation.php

  9. Assert Trait Usage

    4.x

    Use the toUseTrait and toUseTraits methods 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.php

  10. Assert Class Attributes and Magic Methods

    4.x

    Use the toHaveAttribute, toHaveConstructor, and toHaveDestructor methods 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 __construct method.
    • toHaveDestructor(): Asserts the class has a __destruct method.

    Example:

    expect('MyClass')->toHaveAttribute('MyAttribute');
    expect('MyClass')->toHaveConstructor();
    expect('MyClass')->toHaveDestructor();

    Sources: src/Expectation.php