I am finding wp-cli increasingly useful. For those who haven’t come across it before—as the name suggests—it is a command line interface for WordPress. This in itself is fairly useful as a standalone tool for blog management (just run
wp to see the possibilities!). For developers it also has a handy feature for bootstrapping plugin unit tests.
Writing tests often feels like hard work. wp-cli aims to aid the situation for WordPress developers by trying to remove as many obstacles as possible.
Assuming you have wp-cli all setup and you’re in your WordPress project directory creating an initial testing framework is as easy as running a few commands:
wp scaffold plugin-tests your-plugin-name-here- this creates a skeleton phpunit.xml file, a sample test file in
tests/test-sample.php, a bootstrap file to load WordPress and the plugin for testing plus a useful shell script for setting up the test environment in
bin/install-wp-tests.sh. That’s a lot of useful helping-hands in getting setup.
- Next up we need to run the
install-wp-tests.shscript. This requires MySQL credentials to create a copy of your database, which will be used for testing.
- Write an actual test case for your plugin.
sudo apt-get install subversionif you're using ubuntu.