Pest for WordPress plugin tests
Pest is a testing framework on top of PHPUnit with a shorter syntax: tests are closures with a description instead of methods in classes, and expectations read like sentences. For WordPress plugins, it runs on the same WordPress test suite and bootstrap as PHPUnit, so an existing setup can switch file by file, and both styles can live in one project.
Problem: A plugin’s PHPUnit tests are verbose: each test class repeats the same setUp(), assertion names are long, and data-driven tests with @dataProvider are hard to read. New contributors write few tests.
Solution: Install Pest as a dev dependency next to the existing WordPress test bootstrap, bind tests to WP_UnitTestCase in tests/Pest.php, write new tests as it() closures with expect(), use datasets for input variations, and run them with vendor/bin/pest locally and in CI.
Install on top of the existing setup
composer require --dev pestphp/pest --with-all-dependencies vendor/bin/pest --init
Pest uses the existing phpunit.xml.dist, including its bootstrap that loads the WordPress test library and the plugin. The setup from wp scaffold plugin-tests keeps working: unit tests with PHPUnit and WP-CLI scaffold.
<?php // tests/Pest.php uses( WP_UnitTestCase::class )->in( 'Feature', 'Unit' );
Every test file in these folders now runs inside a WP_UnitTestCase: each test gets a database transaction that is rolled back afterwards, and the factory ($this->factory()) creates posts and users.
Tests as closures
<?php
// tests/Feature/BookingTest.php
it( 'creates a booking for a free slot', function () {
$user_id = $this->factory()->user->create( array( 'role' => 'subscriber' ) );
wp_set_current_user( $user_id );
$booking_id = ha_create_booking( '2026-10-01 10:00', 'anna@example.com' );
expect( $booking_id )->toBeInt()->toBeGreaterThan( 0 )
->and( get_post_meta( $booking_id, '_ha_slot', true ) )->toBe( '2026-10-01 10:00' );
} );
it( 'rejects a slot that is already taken', function () {
ha_create_booking( '2026-10-01 10:00', 'first@example.com' );
expect( ha_create_booking( '2026-10-01 10:00', 'second@example.com' ) )
->toBeInstanceOf( WP_Error::class );
} );
$this inside the closure is the test case, so WordPress helpers and the factory work as in class-based tests. Expectations chain with ->and() for several values.
Datasets instead of data providers
it( 'validates booking emails', function ( string $email, bool $valid ) {
expect( ha_is_valid_booking_email( $email ) )->toBe( $valid );
} )->with( array(
'normal address' => array( 'anna@example.com', true ),
'missing domain' => array( 'anna@', false ),
'with spaces' => array( ' anna@example.com ', false ),
'disposable domain' => array( 'x@mailinator.com', false ),
) );
Each dataset entry becomes its own test with the key as its name in the output, so a failure says “validates booking emails with disposable domain”.
Run
vendor/bin/pest vendor/bin/pest --filter=booking vendor/bin/pest --parallel # needs the brianium/paratest package
Old PHPUnit test classes run alongside, so there is no need to convert everything at once. In CI, replace the PHPUnit command with vendor/bin/pest; everything else in the workflow stays the same.
NOTE: Pest 3 needs PHP 8.2 and Pest 4 needs PHP 8.3, while the WordPress test suite runs on older versions too. For plugins that must test on PHP 7.4 or 8.0 in CI, keep PHPUnit for those jobs or pin an older Pest version. Static analysis complements tests: PHPStan for WordPress plugins.