$npx -y skills add Lonsdale201/wp-agent-skills --skill wp-phpunit-test-setupSet up the PHPUnit test harness for a WordPress plugin or theme, the WP way. Covers wp scaffold plugin-tests / wp scaffold theme-tests and the files it generates (phpunit.xml.dist, bin/install-wp-tests.sh, tests/bootstrap.php, tests/test-sample.php, .phpcs.xml.dist,
| 1 | # WordPress PHPUnit test setup |
| 2 | |
| 3 | Get a real WordPress integration test harness running for a plugin or theme. The fast path is WP-CLI's scaffolder, which generates the same layout WordPress core uses. This skill is the setup + CI half; writing the actual tests is `wp-phpunit-writing-tests`. |
| 4 | |
| 5 | ## When to use this skill |
| 6 | |
| 7 | - Adding automated tests to a plugin/theme that has none. |
| 8 | - Wiring PHPUnit into CI (GitHub Actions, GitLab, etc.). |
| 9 | - Debugging `install-wp-tests.sh`, the test database, `tests/bootstrap.php`, or "wrong PHPUnit version" errors. |
| 10 | - Reviewing a repo's `phpunit.xml.dist`, `composer.json` test scripts, or CI matrix. |
| 11 | |
| 12 | ## The PHPUnit version reality (read this first) |
| 13 | |
| 14 | The most common setup mistake: requiring PHPUnit 10 or 11. **The WordPress core PHPUnit test suite is capped at PHPUnit 9.x** — there is no PHPUnit 10/11/12 support in any WordPress version, including WP 7.0. See the official [compatibility table](https://make.wordpress.org/core/handbook/references/phpunit-compatibility-and-wordpress-versions/). |
| 15 | |
| 16 | `WP_UnitTestCase`-based tests boot the real WordPress test suite, so they run on the PHPUnit version the suite supports — **PHPUnit 9.x for current WordPress**. The bridge across PHPUnit majors is **`yoast/phpunit-polyfills`**, a hard dependency of the WP test suite since WordPress 5.9. |
| 17 | |
| 18 | Practical rule: pin PHPUnit to `^9` and `yoast/phpunit-polyfills` to the constraint your installed WordPress expects (the scaffolded CI uses `phpunit-polyfills:1.1`; WordPress core pins the `1.x` line). The polyfills package has newer majors (4.x), but match what the WP test suite bootstrap loads — do not assume the newest works. Verify against your WP version, do not guess. |
| 19 | |
| 20 | ## Scaffold the harness |
| 21 | |
| 22 | ```bash |
| 23 | wp scaffold plugin-tests my-plugin --ci=github |
| 24 | # or for a theme: |
| 25 | wp scaffold theme-tests my-theme --ci=github |
| 26 | ``` |
| 27 | |
| 28 | Synopsis: `wp scaffold plugin-tests [<plugin>] [--dir=<dir>] [--ci=<provider>] [--force]`. The `--ci` provider is one of `circle` (default), `gitlab`, `bitbucket`, `github` — pass `--ci=github` for GitHub Actions. |
| 29 | |
| 30 | It generates: |
| 31 | |
| 32 | | File | Purpose | |
| 33 | |---|---| |
| 34 | | `phpunit.xml.dist` | PHPUnit configuration (test suite paths, bootstrap). | |
| 35 | | `bin/install-wp-tests.sh` | Downloads + configures the WP test suite and test DB. | |
| 36 | | `tests/bootstrap.php` | Loads the test suite and activates your plugin/theme for the run. | |
| 37 | | `tests/test-sample.php` | A starter test extending `WP_UnitTestCase`. | |
| 38 | | `.phpcs.xml.dist` | A starter PHP_CodeSniffer ruleset (see `wp-phpcs-coding-standards`). | |
| 39 | | CI config | e.g. a `.github/workflows/*.yml` for `--ci=github`. | |
| 40 | |
| 41 | The scaffolder is the same tooling WordPress core and most plugins use, so the layout is familiar to contributors. Use `--force` only when intentionally regenerating. |
| 42 | |
| 43 | ## Install the WordPress test suite |
| 44 | |
| 45 | ```bash |
| 46 | bash bin/install-wp-tests.sh <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation] |
| 47 | # example: |
| 48 | bash bin/install-wp-tests.sh wordpress_test root '' localhost latest |
| 49 | ``` |
| 50 | |
| 51 | The first three arguments are required; `db-host` defaults to `localhost`, `wp-version` to `latest`. Pass `true` as the sixth argument to skip creating the database (e.g. in CI where a service container already created it). |
| 52 | |
| 53 | What it does: |
| 54 | |
| 55 | - Downloads WordPress core to `/tmp/wordpress/` and the test library (`includes/` + `data/`) to `/tmp/wordpress-tests-lib/` (override with `WP_CORE_DIR` / `WP_TESTS_DIR`). |
| 56 | - **Downloads via `curl`/`wget` from the WordPress develop GitHub mirror — Subversion is no longer required** by the current script. (Older tutorials say "install SVN first"; that is outdated.) |
| 57 | - Writes `wp-tests-config.php` with your DB credentials. |
| 58 | |
| 59 | The test suite uses a **separate, disposable |