A WordPress plugin author follows the official PHPUnit setup, runs phpunit, and gets Fatal error: Uncaught Error: Class "WP_UnitTestCase" not found. The standard WordPress test bootstrap is non-obvious — it needs a database fixture, a wp-tests-config.php that mirrors wp-config.php, and a PHPUnit version that matches the WordPress test framework’s expectations. The setup is mechanical once you’ve done it; the first time it eats a day.
What’s happening
WordPress ships its own PHPUnit harness in the wordpress-develop repository, not in the public WordPress release. That harness includes WP_UnitTestCase, a wipe-and-reset database fixture between tests, and helpers for creating posts, users, and terms. To use it from a plugin or theme test suite, you need to:
- Install the test framework into a known location.
- Write
wp-tests-config.phppointing at a throwaway test database. - Have your
phpunit.xmlbootstrap point at the framework’s bootstrap, which then loads your plugin.
The version trap: PHPUnit 9 broke API compatibility with PHPUnit 10. WordPress’s test framework only fully supports PHPUnit 9 (and earlier 7-8 on older WP). If you composer require --dev phpunit/phpunit ^11, tests fail on framework methods that no longer exist.
How to verify
Confirm the setup is correct by inspecting the test config and PHPUnit version:
# PHPUnit version
./vendor/bin/phpunit --version
# WordPress test framework version
grep "WP_TESTS_DOMAIN" /tmp/wordpress-tests-lib/wp-tests-config.php
# WP core version the tests use
cat /tmp/wordpress/wp-includes/version.php | grep wp_version
A working setup has PHPUnit 9.6, a wp-tests-config.php in /tmp/wordpress-tests-lib/, and a /tmp/wordpress/ install matching the WordPress version your plugin targets.
Smoke-test the harness without your plugin code:
cd /tmp/wordpress-tests-lib
./vendor/bin/phpunit tests/phpunit/tests/admin/includesMisc.php
If that fails, the harness itself is broken — fix it before touching your plugin’s tests.
What to do
The boilerplate that gets a plugin or theme to a green test run.
- Use the official
install-wp-tests.shscript fromwp-cli/scaffold-package-commandor the manual version from WordPress’s own repo. Run it once to populate/tmp/wordpress/and/tmp/wordpress-tests-lib/:
bin/install-wp-tests.sh wordpress_test root '' localhost latest
That creates a wordpress_test MySQL database, downloads the WordPress source to /tmp/wordpress/, downloads the test framework to /tmp/wordpress-tests-lib/, and writes a wp-tests-config.php there.
- Your plugin’s
composer.jsonpins PHPUnit at a version the framework supports:
"require-dev": {
"phpunit/phpunit": "^9.6",
"yoast/phpunit-polyfills": "^2.0",
"wp-coding-standards/wpcs": "^3.1"
}
yoast/phpunit-polyfills is what makes PHPUnit 9 tests work against the WordPress framework’s older assertion style.
- Your
phpunit.xml.dist:
<?xml version="1.0"?>
<phpunit
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertWarningsToExceptions="true">
<testsuites>
<testsuite name="plugin">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
- Your
tests/bootstrap.phpis what bridges your plugin into the WordPress harness:
<?php
$_tests_dir = getenv('WP_TESTS_DIR') ?: '/tmp/wordpress-tests-lib';
require_once $_tests_dir . '/includes/functions.php';
function _manually_load_plugin() {
require dirname(__DIR__) . '/your-plugin.php';
}
tests_add_filter('muplugins_loaded', '_manually_load_plugin');
require $_tests_dir . '/includes/bootstrap.php';
- Write tests against
WP_UnitTestCase:
<?php
class Test_Your_Feature extends WP_UnitTestCase {
public function test_post_creation() {
$post_id = self::factory()->post->create([
'post_title' => 'Test post',
]);
$this->assertGreaterThan(0, $post_id);
$post = get_post($post_id);
$this->assertEquals('Test post', $post->post_title);
}
}
self::factory() is the test harness’s content factory — uses real WordPress APIs, runs in the wipe-and-reset transaction, so each test starts clean.
-
Run:
composer install && ./vendor/bin/phpunit. First green run is the milestone. -
For CI, use the WordPress official GitHub Action
actions/wordpress-test-action— it sets up MySQL, WordPress, and the test framework in a runner. See WordPress deployment automation with GH Actions for the full pipeline.
For the deeper development workflow around tests, Stack Harbor’s Enterprise WordPress practice ships CI-gated plugin development as part of every engagement. If a custom plugin has no test coverage and you want to add it without rewriting the world, we can scope the test harness.