This article is focused on the practical implementation of WordPress plugin testing.
With WP-CLI we can generate a production-ready plugin structure that include comprehensive testing infrastructure from the start. We’ll walk through the process to get a professional WordPress plugin structure with proper testing setup.
By the end of this guide, you’ll understand how to:
- Generate plugins using WP-CLI’s scaffold command with complete testing infrastructure
- Set up WordPress test suites and databases for integration testing
Table of Contents
Scaffold a plugin with WP-CLI
Instead of manually creating files and headers, let WP-CLI’s scaffold command generate a clean, standards-friendly starter. The scaffold includes your main plugin file, a readme, editor/git config, and (by default) a full PHPUnit test skeleton and CI config, so you can start coding and testing immediately.
Navigate to plugins directory
cd wp-content/pluginsGenerate the plugin
wp scaffold plugin my-awesome-plugin \
--plugin_name="My Awesome Plugin" \
--plugin_description="Short description of what it does." \
--plugin_author="Your Name" \
--plugin_author_uri="https://example.com" \
--plugin_uri="https://example.com/my-awesome-plugin" \
--activate \
--ci=githubScaffold options explained:
--activate— immediately activates the plugin after creation--ci=github— generates GitHub Actions workflow (supportscircle,gitlab,bitbucket)--skip-tests— omit this flag to include PHPUnit test scaffolding
What gets generated
Core plugin files (always created):
my-awesome-plugin.php— main plugin file with proper headers and activation/deactivation hooksreadme.txt— WordPress.org-style readme with installation instructions.editorconfig— consistent coding standards across editors.gitignore— excludes vendor/, node_modules/, and build files.distignore— files excluded from distribution zips (tests, dev dependencies)
Development workflow files:
package.json— NPM scripts forstart,readme,i18ntasksGruntfile.js— automated tasks for internationalization and documentation
Testing infrastructure (included by default):
phpunit.xml.dist— PHPUnit configuration for integration testsbin/install-wp-tests.sh— script to download WordPress test suite and create test databasetests/bootstrap.php— loads WordPress testing environment and your plugintests/test-sample.php— example test case to get you started.phpcs.xml.dist— PHP CodeSniffer rules for WordPress coding standards
CI/CD configuration:
.github/workflows/— GitHub Actions workflow (when using--ci=github)- Supports CircleCI, GitLab CI, and Bitbucket Pipelines
Verify plugin creation
- Check
http://localhost:8888/mysite/wp-admin/plugins.php— your plugin should be active - Explore the generated files:
ls -la my-awesome-plugin/ - Enter into the plugin’s folder
cd wp-content/plugins/my-awesome-plugin
Add test environment (installer)
Now we need to install the WordPress test suite and create the test DB with the provided script. We can do that by running the script bin/install-wp-tests.sh.
Before running the installer, remember bin/install-wp-tests.sh needs the MySQL client tools (mysql, mysqladmin). On macOS with MAMP they live here:
mysql and mysqladmin client tools
With MAMP these MySQL client tools are available in several folders (depending on the MySQL version):
- MySQL 5.7 →
/Applications/MAMP/Library/bin/mysql57/bin/ - MySQL 8.0 →
/Applications/MAMP/Library/bin/mysql80/bin/
Add the right folder to your PATH (as I use zsh, in the ~/.zshrc file on my case ), then reload:
# Pick one:
echo 'export PATH="/Applications/MAMP/Library/bin/mysql80/bin:$PATH"' >> ~/.zshrc
# echo 'export PATH="/Applications/MAMP/Library/bin/mysql57/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
which mysql && mysql --versionsvn tool
bin/install-wp-tests.sh also needs svn client tool. In Mac it can be installed with homebrew
brew install subversionRunning install-wp-tests.sh
With mysql, mysqladmin and svn available from the terminal we can now run the installer to create the wordpress_test DB to fetch the WP test suite:
# This script downloads the WordPress testing suite from the official repository
bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:8889 latestThe bin/install-wp-tests.sh script sets up WordPress testing infrastructure by performing three key tasks: creating a test database, downloading WordPress core files, and installing the official WordPress test suite.
The script downloads content from two official WordPress repositories and creates this structure at either the path at the environment variable $WP_TESTS_DIR or at $TMPDIR/wordpress*:
- The
wordpressdirectory contains a complete WordPress installation downloaded from wordpress.org’s official releases at the version you specify - The
wordpress-tests-libdirectory contains the testing framework downloaded from WordPress core’s develop repository on SVN
This setup enables integration testing where your plugin can interact with actual WordPress APIs, hooks, and database functions rather than just mocked versions.
Conclusion
Using WP-CLI’s scaffold command gives you a complete plugin testing setup without the manual configuration. This approach creates professional plugin structures that include all the testing infrastructure from day one.
You now have a WordPress plugin development environment (including a DB for tests) that supports PHPUnit testing.
Leave a Reply