wp scaffold plugin-tests
Overview
Add a complete PHPUnit test suite to an existing plugin — bootstrap files, test configuration (phpunit.xml), a sample test class, and a WordPress test library installer script. Jumpstart plugin testing without configuring everything from scratch.
What It Does
wp scaffold plugin-tests generates the files needed to run PHPUnit tests against your plugin within the WordPress test suite. It creates a test bootstrap that loads WordPress's test infrastructure so your plugin's functions can be tested in a real WordPress context.
Syntax
wp scaffold plugin-tests [<plugin>] [OPTIONS]
Options
| Flag | Description |
|---|---|
PLUGIN | Plugin slug to target (default: current directory) |
--dir=DIR | Override directory for test files |
--force | Overwrite existing test files |
Basic Usage
wp scaffold plugin-tests my-awesome-plugin
Output:
Success: Created test files.
Generated File Structure
wp-content/plugins/my-awesome-plugin/
└── tests/
├── bootstrap.php
├── test-sample.php
├── phpunit.xml.dist
└── bin/
└── install-wp-tests.sh
test-sample.php (Example)
<?php
class SampleTest extends WP_UnitTestCase {
public function test_sample() {
$this->assertTrue( true );
}
}
Setting Up Tests
Step 1: Install WordPress test suite
bash tests/bin/install-wp-tests.sh wordpress_test root password localhost latest
Step 2: Run PHPUnit
phpunit
Output:
PHPUnit 9.6.0 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 00:00.348, Memory: 26.00 MB
OK (3 tests, 3 assertions)
Best Practices
- Run tests on every pull request — add to your CI/CD pipeline.
- Test against multiple WP versions — use the
latest,nightly, and a pinned version. - Don't commit
/tmp/wordpress-tests-lib— add it to.gitignore.
Quick Reference
wp scaffold plugin-tests <plugin> # Add tests to plugin
wp scaffold plugin-tests . --force # In current dir, overwrite
Next Steps
wp scaffold plugin— scaffold the plugin itself (includes tests by default).