This file provides detailed testing patterns and examples. For basic test commands, see the Development Environment documentation.
Tests for the development-only PHPStan extension live in tests/phpstan/tests/, with a class-test- file matching the extension's source file. Helpers and fixtures live in tests/phpstan/data/. These tests use PHPStan's testing base class rather than WordPress's, and run independently of WordPress and Docker with composer analyze:test.
tests/phpunit/
├── bootstrap.php # Test bootstrap file
├── includes/ # Shared test helpers (base test cases, stubs, traits)
├── data/ # Fixtures, mocks and other test data
└── tests/ # The tests, mirroring the plugin's file structure
├── includes/ # Tests for includes/, e.g. includes/rest/class-test-seek-controller.php
└── integration/ # Tests for integration/ (third-party plugin integrations)
Every test file mirrors the source file it tests: same path below tests/phpunit/tests/, file name prefixed with class-test-.
| Source file | Test file |
|---|---|
includes/class-query.php |
tests/phpunit/tests/includes/class-test-query.php |
includes/rest/class-seek-controller.php |
tests/phpunit/tests/includes/rest/class-test-seek-controller.php |
includes/rest/trait-event-stream.php |
tests/phpunit/tests/includes/rest/class-test-trait-event-stream.php |
integration/stream/class-connector.php |
tests/phpunit/tests/integration/stream/class-test-connector.php |
- Add new tests to the existing test file of the source file they cover. Tests for a REST route go into the test file of the controller that registers it.
- Do not create test files that span several source files. If a test needs a new kind of file, document the pattern here first.
- Shared setup belongs in
tests/phpunit/includes/, not in a test file. - Snippet tests mirror
snippets/belowtests/phpunit/tests/snippets/. Load the opt-in snippet explicitly and remove its hooks in teardown.
The pre-commit hook enforces this with bin/precommit/check-test-file-location.js. A few older test files predate the rule and are listed as exceptions there; do not add to that list.
Use @group annotations to organize tests. Run with: npm run env-test -- --group=groupname.
Common groups: migration, rest, integration.
// Test class names.
class Test_Notification extends \WP_UnitTestCase {}
// Test method names.
public function test_send_notification() {}
public function test_notification_with_invalid_data() {}
public function test_should_handle_empty_followers() {}<?php
/**
* Test_Feature_Name class.
*
* @package Activitypub
* @group activitypub
*/
namespace Activitypub\Tests;
use Activitypub\Feature_Name;
/**
* Test Feature_Name functionality.
*/
class Test_Feature_Name extends \WP_UnitTestCase {
/**
* Set up test environment.
*/
public function set_up(): void {
parent::set_up();
// Test-specific setup.
}
/**
* Tear down test environment.
*/
public function tear_down(): void {
// Clean up.
parent::tear_down();
}
/**
* Test basic functionality.
*/
public function test_basic_feature() {
// Arrange.
$input = 'test';
// Act.
$result = Feature_Name::process( $input );
// Assert.
$this->assertEquals( 'expected', $result );
}
}// Equality assertions.
$this->assertEquals( $expected, $actual );
$this->assertSame( $expected, $actual ); // Strict comparison.
$this->assertNotEquals( $expected, $actual );
// Type assertions.
$this->assertIsArray( $value );
$this->assertIsString( $value );
$this->assertIsBool( $value );
// WordPress assertions.
$this->assertWPError( $result );
$this->assertNotWPError( $result );
$this->assertQueryTrue( 'is_single', 'is_singular' );
// Count assertions.
$this->assertCount( 3, $array );
$this->assertEmpty( $value );
$this->assertNotEmpty( $value );
// Exception assertions.
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Error message' );Since WordPress 7.2, wp_kses() runs on the HTML API. It sorts attributes, drops the slash of void elements (<input /> becomes <input>), re-escapes character references and removes the text inside <script>. CI runs one job against WordPress trunk, so an exact string compare on kses output fails there while it passes locally.
- Compare whole HTML with
assert_equal_html()from theEqual_Htmltrait (tests/phpunit/includes/trait-equal-html.php). It compares the parsed trees and falls back toassertSame()before WordPress 6.9. - Check a fragment inside a larger output (a block render) with
WP_HTML_Tag_Processor: find the tag withnext_tag()and assertget_attribute(). NotassertStringContainsString()with several attributes. - Test what differs between versions as the property that matters, for example
assertStringNotContainsString( '<script', … )instead of the text that is left.
use Activitypub\Tests\Equal_Html;
class Test_Example extends \WP_UnitTestCase {
use Equal_Html;
public function test_markup() {
$this->assert_equal_html( '<p><a href="https://example.org" rel="nofollow">Link</a></p>', $content );
$form = new \WP_HTML_Tag_Processor( $output );
$this->assertTrue( $form->next_tag( 'form' ) );
$this->assertSame( 'post', $form->get_attribute( 'method' ) );
}
}-
Print debugging:
var_dump( $variable ); print_r( $array ); error_log( print_r( $data, true ) );
-
Stop on failure:
npm run env-test -- --stop-on-failure
-
Verbose output:
npm run env-test -- --verbose --debug
-
Single test isolation:
npm run env-test -- --filter=test_specific_method
-
Check test database:
npm run env -- run tests-cli wp db query "SELECT * FROM wp_posts"
Issue: Tests pass individually but fail together
- Check for test interdependencies
- Ensure proper tearDown() cleanup
- Look for global state modifications
Issue: Different results locally vs CI
- Check PHP/WordPress versions
- Verify timezone settings
- Check for environment-specific code
Issue: Timeout errors
- Increase timeout in test
- Check for infinite loops
- Verify external API mocks
Issue: Database not reset between tests
- Check transaction rollback
- Ensure proper cleanup in tearDown()
- Look for direct database writes
Creating test data:
// Create test user.
$user_id = self::factory()->user->create( [
'role' => 'editor',
] );
// Create test post.
$post_id = self::factory()->post->create( [
'post_author' => $user_id,
'post_status' => 'publish',
] );
// Create test term.
$term_id = self::factory()->term->create( [
'taxonomy' => 'category',
] );Mocking HTTP requests:
IMPORTANT: Always save filter callbacks to a variable so they can be removed properly. NEVER use remove_all_filters() or remove_all_actions() as they can break other tests and plugins.
// CORRECT: Save callback to variable for proper removal.
$http_filter_callback = function( $preempt, $args, $url ) {
if ( strpos( $url, 'example.com' ) !== false ) {
return array(
'response' => array( 'code' => 200 ),
'body' => json_encode( array( 'success' => true ) ),
);
}
return $preempt;
};
// Add the filter.
\add_filter( 'pre_http_request', $http_filter_callback, 10, 3 );
// Do your test...
// Remove the specific filter.
\remove_filter( 'pre_http_request', $http_filter_callback, 10 );
// NEVER DO THIS - it breaks other tests and plugins:
// remove_all_filters( 'pre_http_request' ); // ❌ WRONG
// remove_all_actions( 'some_action' ); // ❌ WRONGTime-based testing:
// Mock current time.
$now = '2024-01-01 12:00:00';
add_filter( 'current_time', function() use ( $now ) {
return $now;
} );