If you’re writing a plugin that really needs to be bug free, functional or unit tests are highly recommended. Wercker is a Continues Integration platform that seamlessly works with WordPress. Lately, I have been working on a WordPress plugin that sends and receives data with accounting systems. For such an critical system functional and unit tests are really important.
This article will tell you exactly how to integrate Wercker with your WordPress plugin. There are plenty of Continues Integration providers. I chose Wercker because they support private repositories, is free* and works seamlessly with Bitbucket. I will go through the whole process from scratch.
A prerequiste is that you have a wordpress installation with your custom plugin installed. Also this plugin should be version controlled using git.
Lets first install some essentials.
PHPUnit is the testing framework. Let’s install that on your local machine if it’s not already installed.
sudo apt-get install phpunit
Wp CLI is an awesome tool for WordPress. We will use it to generate boilerplate files inside our plugin folder.
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
Scaffold phpunit configuration
Lets now use WP-CLI to generate test files and configuration.
wp scaffold plugin-tests my-plugin-name
Some new files will then be created in your plugin folder (phpunit.xml.dist, tests etc).
Lets see if everything works.
Write your own tests
Tests should be located inside the “tests” folder. By default phpunit is configured to add files that start with “test-” as a test class. By default the scaffold should have created a simple test for you.
Here is a working wercker.yml script that works with the awesome Continues integration service Wercker. Put the wercker.yml file in the root of your plugin. The gotcha here is that we re-enable mysql, since mysql_X functions are disabled by default.
- id: mariadb
packages: git subversion phpunit mysql-client php5-mysql
name: setup tests
bash bin/install-wp-tests.sh wordpress_test root rdb "$MARIADB_PORT_3306_TCP_ADDR" latest
name: enable extensions
name: echo php information
echo "php version $(php --version) running"
echo "from location $(which php)"
name: run unit tests