Run the setup script (downloads WordPress + the WP test suite into the repo, and creates the test DB if needed):
npm run test:setup:phpDefaults used by test:setup:php:
- DB Name:
code_snippets_phpunit - DB User:
root - DB Password: (empty)
- DB Host:
127.0.0.1 - WP Version:
latest
Override defaults via env vars (example):
WP_PHPUNIT_DB_NAME=wp_phpunit_test \
WP_PHPUNIT_DB_USER=root \
WP_PHPUNIT_DB_PASS=root \
WP_PHPUNIT_DB_HOST=127.0.0.1 \
WP_PHPUNIT_WP_VERSION=latest \
npm run test:setup:phpRun all tests:
npm run test:phpRun tests with detailed output:
npm run test:php:watchOr run PHPUnit directly:
WP_TESTS_DIR=./.wp-tests-lib WP_DEVELOP_DIR=./.wp-core src/vendor/bin/phpunit -c phpunit.xmlThe test:setup:php script will:
- Download WordPress core to
./.wp-core/ - Download the WordPress test library to
./.wp-tests-lib/ - Create a test database (if it doesn't exist)
- Generate
./.wp-tests-lib/wp-tests-config.php
Run npm run test:setup:php to download the WordPress test suite.
Verify your database credentials and that MySQL is running.
Make sure the install script is executable:
chmod +x tests/install-wp-tests.shThe WordPress test suite download uses svn export. Install Subversion if you don't already have it.
Tests should be placed in tests/phpunit/ with the naming pattern test-*.php.
Example test:
<?php
namespace Code_Snippets\Tests;
class My_Test extends TestCase {
public function test_something() {
$this->assertTrue( true );
}
}- Keep tests isolated: create your own fixtures and clean up after each test where possible.
- Prefer plugin APIs (
save_snippet,delete_snippet, etc.) over direct SQL so behavior matches runtime (and keeps flat-file mode in sync). - Avoid depending on UI strings/markup in PHPUnit tests—assert on behavior, data, and registered WP objects (e.g.
WP_Admin_Barnodes).
Prereqs:
- Docker (required for
wp-env) - Node.js/npm
Install JS deps:
npm ciInstall Playwright browsers (once):
npx playwright installStart the WordPress environment:
npm run wp-env:startOptional (recommended when switching branches / after failures): reset the WP env:
npm run wp-env:clean
npm run wp-env:startPrepare the environment for E2E (cleans stale flat-file artifacts, ensures plugin active, etc.):
npm run test:setup:playwrightRun everything:
npm run test:playwrightRun a single project:
npm run test:playwright -- --project=chromium-db-snippetsRun the file-based snippets project (includes flat-file setup):
npm run test:playwright -- --project=chromium-file-based-snippetsRun with HTML reporter but don’t auto-open the report:
PW_TEST_HTML_REPORT_OPEN=never npm run test:playwright- Traces are saved under
test-results/on failures. View one with:
npx playwright show-trace test-results/**/trace.zipGuidelines / caveats:
- Prefer resilient locators (
getByRole,getByLabel, stable ids) over fragile CSS selectors. - Use
wpCli()for setup/fixtures when possible (fast + deterministic). - Always clean up created snippets/pages (prefer the helper methods so file-based mode stays in sync).
- Avoid leaking global state between tests (e.g. Safe Mode, mu-plugins, settings toggles).
- Keep per-test timeouts explicit only when needed (and use constants rather than magic numbers).