This guide covers how to run tests for plugins in the WPGraphQL monorepo.
# Start the test environment
npm run wp-env start
# Run WPUnit tests for wp-graphql
npm run -w @wpgraphql/wp-graphql test:codecept:wpunitTests run inside Docker containers managed by wp-env. The test environment includes:
- WordPress installation at
http://localhost:8889 - MySQL database
- PHP with Xdebug (optional)
- All plugins in
plugins/directory
# Standard start
npm run wp-env start
# With PCOV code coverage enabled
PCOV_ENABLED=1 npm run wp-env start
# With Xdebug debugging enabled
npm run wp-env start -- --xdebug| Variable | Description | Default |
|---|---|---|
TEST_THEME |
WordPress theme for tests | twentytwentyone |
MULTISITE |
Run as multisite | false |
WP_ENV_PHP_VERSION |
PHP version | 8.2 |
Example:
TEST_THEME=twentytwentyfive npm run -w @wpgraphql/wp-graphql test:codecept:wpunitIntegration tests that run within WordPress:
# Run all WPUnit tests
npm run -w @wpgraphql/wp-graphql test:codecept:wpunit
# Run a specific test file
npm run -w @wpgraphql/wp-graphql test:codecept:wpunit -- tests/wpunit/PostObjectQueriesTest.php
# Run a specific test method
npm run -w @wpgraphql/wp-graphql test:codecept:wpunit -- tests/wpunit/PostObjectQueriesTest.php:testPostQuery
# Run with verbose output
npm run -w @wpgraphql/wp-graphql test:codecept:wpunit -- --debugEnd-to-end tests via HTTP:
npm run -w @wpgraphql/wp-graphql test:codecept:acceptanceTests that interact with WordPress functions:
npm run -w @wpgraphql/wp-graphql test:codecept:functionalThe wp-graphql-acf plugin requires Advanced Custom Fields (ACF) to be installed for tests to run. ACF Extended is optional and is only used with ACF Pro (not with ACF Free).
# From repo root: install ACF Free only (no ACF Extended)
cd plugins/wp-graphql-acf && npm run install-test-deps && cd ../..
# Or use the alias
npm run -w @wpgraphql/wp-graphql-acf install-acf
# Then run tests
npm run -w @wpgraphql/wp-graphql-acf test:codecept:wpunitYou can provide the license key in three ways:
Option A: Using a .env file (Recommended)
# Create or edit plugins/wp-graphql-acf/.env
echo "ACF_LICENSE_KEY=your_license_key_here" >> plugins/wp-graphql-acf/.env
# Install ACF Pro + ACF Extended Free
npm run -w @wpgraphql/wp-graphql-acf install-acf:proOption B: Environment variable (session)
export ACF_LICENSE_KEY=your_license_key_here
npm run -w @wpgraphql/wp-graphql-acf install-acf:proOption C: Inline (one-time use)
ACF_LICENSE_KEY=your_license_key_here npm run -w @wpgraphql/wp-graphql-acf install-acf:proThen run tests:
npm run -w @wpgraphql/wp-graphql-acf test:codecept:wpunitUsing a .env file (Recommended):
# Create or edit plugins/wp-graphql-acf/.env
cat >> plugins/wp-graphql-acf/.env << EOF
ACF_LICENSE_KEY=your_acf_pro_license_key
ACF_EXTENDED_LICENSE_KEY=your_acf_extended_pro_license_key
EOF
# Install ACF Pro + ACF Extended Pro
npm run -w @wpgraphql/wp-graphql-acf install-acf:pro-extendedOr using environment variables:
export ACF_LICENSE_KEY=your_acf_pro_license_key
export ACF_EXTENDED_LICENSE_KEY=your_acf_extended_pro_license_key
npm run -w @wpgraphql/wp-graphql-acf install-acf:pro-extendedThen run tests:
npm run -w @wpgraphql/wp-graphql-acf test:codecept:wpunitNote: For local development, you can use ACF Free (Option 1) which doesn't require any license keys. The .env file is gitignored, so your license keys won't be committed. ACF Extended is only installed when using ACF Pro (Options 2 or 3).
You can also install ACF plugins manually via WP-CLI:
# Install ACF Free only (no ACF Extended with ACF Free)
npm run wp-env run tests-cli -- wp plugin install advanced-custom-fields --activate --allow-root
# With ACF Pro: install ACF Pro first, then optionally ACF Extended Free or Pro
export ACF_LICENSE_KEY=your_license_key
npm run wp-env run tests-cli -- wp plugin install "https://connect.advancedcustomfields.com/v2/plugins/download?p=pro&k=${ACF_LICENSE_KEY}" --activate --allow-root
# Then: npm run wp-env run tests-cli -- wp plugin install acf-extended --activate --allow-root # Extended FreeNote: ACF plugins only need to be installed once. They will persist in your wp-env environment until you destroy it (npm run wp-env destroy).
wp-graphql-acf has Playwright E2E tests. Install ACF first (e.g. npm run install-test-deps from plugins/wp-graphql-acf), then run:
npm run -w @wpgraphql/wp-graphql-acf test:e2eTests skip Pro-only or ACF Extended–only specs when running with ACF Free (set INSTALL_ACF_PRO / INSTALL_ACF_EXTENDED_PRO to match your install so skips match CI).
Local-only: full CI-like run (build, wp-env, install ACF, run E2E, stop) from repo root:
./bin/run-acf-e2e-local.sh
# Or with a specific ACF variant:
INSTALL_ACF_PRO=false INSTALL_ACF_EXTENDED_PRO=false ./bin/run-acf-e2e-local.sh # ACF FreePlaywright-based browser tests for the core plugin:
npm run -w @wpgraphql/wp-graphql test:e2eGenerate code coverage reports:
# Start with coverage enabled
PCOV_ENABLED=1 npm run wp-env start
# Run tests with coverage
npm run -w @wpgraphql/wp-graphql test:codecept:wpunit -- --coverage --coverage-xml
# Coverage report will be in plugins/wp-graphql/tests/_output/Tests are organized by type in plugins/wp-graphql/tests/:
tests/
├── wpunit/ # WordPress unit/integration tests
├── acceptance/ # HTTP acceptance tests
├── functional/ # Functional tests
└── e2e/ # End-to-end browser tests
<?php
class MyFeatureTest extends \Tests\WPGraphQL\TestCase\WPGraphQLTestCase {
public function setUp(): void {
parent::setUp();
// Test setup
}
public function tearDown(): void {
// Cleanup
parent::tearDown();
}
public function testMyFeature(): void {
// Create test data
$post_id = $this->factory()->post->create([
'post_title' => 'Test Post',
'post_status' => 'publish',
]);
// Execute GraphQL query
$query = '
query GetPost($id: ID!) {
post(id: $id, idType: DATABASE_ID) {
title
}
}
';
$response = $this->graphql([
'query' => $query,
'variables' => ['id' => $post_id],
]);
// Assert results
$this->assertQuerySuccessful($response, [
$this->expectedField('post.title', 'Test Post'),
]);
}
}The WPGraphQLTestCase base class provides helpers:
// Execute a GraphQL query
$response = $this->graphql(['query' => $query]);
// Assert successful response
$this->assertQuerySuccessful($response, [
$this->expectedField('post.title', 'Test Post'),
]);
// Assert error response
$this->assertQueryError($response);
// Create test users
$admin_id = $this->factory()->user->create(['role' => 'administrator']);
// Set current user
wp_set_current_user($admin_id);Smoke tests validate that the production plugin zip works correctly. These are lightweight tests that verify core functionality without running the full test suite.
For basic local testing (plugin already installed via wp-env):
# Start your WordPress environment
npm run wp-env start
# Run smoke tests against the local environment
./bin/smoke-test.sh
# Or specify a custom endpoint
./bin/smoke-test.sh --endpoint http://wpgraphql.local/graphql
# With verbose output (shows full responses)
./bin/smoke-test.sh --verboseTo test the actual production zip (mimicking what CI does):
# 1. Build the zip
cd plugins/wp-graphql && composer run-script zip && cd ../..
# 2. Start wp-env with a clean config (no plugins pre-installed)
cat > .wp-env.override.json << 'EOF'
{
"plugins": [],
"lifecycleScripts": { "afterStart": null }
}
EOF
npm run wp-env start
# 3. Copy zip to container and install via WP-CLI
CONTAINER=$(docker ps --format '{{.Names}}' | grep 'cli-1' | grep -v 'tests' | head -1)
docker cp plugin-build/wp-graphql.zip "$CONTAINER":/tmp/wp-graphql.zip
npm run wp-env -- run cli wp plugin install /tmp/wp-graphql.zip --activate
# 4. Flush permalinks and enable introspection
npm run wp-env -- run cli wp rewrite flush --hard
npm run wp-env -- run cli wp option update graphql_general_settings \
'{"graphql_endpoint":"graphql","public_introspection_enabled":"on"}' --format=json
# 5. Run smoke tests
./bin/smoke-test.sh
# 6. Clean up override file when done
rm .wp-env.override.json| Test | Description |
|---|---|
| GraphQL endpoint | Basic connectivity check |
| Introspection | Schema introspection works |
| Posts query | Can query posts |
| Pages query | Can query pages |
| Users query | Can query users |
| GeneralSettings | Can query site settings |
| ContentTypes | Can query content types |
| Taxonomies | Can query taxonomies |
| Menus | Can query menus (empty is OK) |
| MediaItems | Can query media items |
The smoke-test.yml workflow:
- Builds the production plugin zip
- Starts a clean WordPress environment (no plugins pre-installed)
- Installs the plugin from the zip via WP-CLI
- Flushes permalinks and enables public introspection
- Runs all smoke tests
This catches issues that unit tests might miss:
- Missing files from
.distignore - Build/bundling problems
- Activation errors
- Missing production dependencies
Tests run automatically on:
- Pull requests to
main - Pushes to
main
The CI runs tests across multiple configurations:
| WordPress | PHP | Theme | Multisite |
|---|---|---|---|
| 6.9 | 8.4 | twentytwentyfive | No |
| 6.9 | 8.4 | twentytwentyfive | Yes |
| 6.9 | 8.4 | twentytwentyone | No |
| 6.9 | 8.4 | twentytwentyone | Yes |
| 6.5 | 8.1 | twentytwentyone | No |
| 6.1 | 7.4 | twentytwentyone | No |
| trunk | 8.4 | twentytwentyfive | No |
Composer dependencies missing:
npm run wp-env -- run tests-cli --env-cwd=wp-content/plugins/wp-graphql/ -- composer installDocker not running:
# Start Docker, then:
npm run wp-env startEnvironment corrupted:
npm run wp-env destroy
npm run wp-env startIf tests fail with theme errors:
# Activate the test theme
npm run wp-env run tests-cli -- wp theme activate twentytwentyone
# Or specify the theme when running tests
TEST_THEME=twentytwentyone npm run -w @wpgraphql/wp-graphql test:codecept:wpunit# Reset the test database
npm run wp-env run tests-cli -- wp db reset --yesIf tests are running slowly:
- Ensure Docker has sufficient resources allocated
- Use
--groupto run specific test groups - Run individual test files instead of full suite
For performance or other reasons, you can run tests directly on your machine:
- PHP 7.4+ with required extensions
- MySQL/MariaDB
- Composer
- Copy
.env.distto.envinplugins/wp-graphql/ - Update database credentials
- Run:
composer -d plugins/wp-graphql install-test-env - Copy test config:
cp tests/wpunit.suite.dist.yml tests/wpunit.suite.yml - Update
tests/wpunit.suite.ymlwith your database details
cd plugins/wp-graphql
vendor/bin/codecept run wpunit