Two popular unit testing tools for WordPress are:
For this article, I will be focusing on option 1, the official he official WordPress unit test repository that is provided by WordPress.
1: Get PHPUnit installed
2: Obtain the unit test repository
svn co https://develop.svn.wordpress.org/trunk/ wordpress-develop
Note: Yes, at this time WordPress at this time still makes heavy use of SVN.
Using SVN, you can place this repo anywhere on your server. Placing it in your home directory is fine. It does not have to be located in your WordPress website’s folder.
You can put the unit tests anywhere on your machine, and simply configure the tests with the path to your WordPress site.
The main components are:
|b||WordPress test repo||~/|
|c||Your WordPress site||located anywhere|
Let’s look more closely at b “WordPress test repo.” The following table shows what we find. The bold items are the ones we need to focus on for now.
A more detailed look at each item
The configuration file for your test. Remember, even though we are doing WordPress unit tests, these are PHPUnit tests.
This folder actually contains an entire WordPress installation.
The tests themselves. These tests cover every WordPress function under the sun (and possibly under the moon as well.) There are over 1,000 tests in here which can test an entire WordPress installation. But the goal of this tutorial is to test a WordPress plugin. We will not need most of these tests in order to accomplish that. So eventually, we will be coming up with a second test folder, just for that purpose. The tests in our second folder will be exclusively concentrated on the functions of our plugin rather than testing all of WordPress.
This is related to the included copy of WordPress.
This is an important file to us.
1. Create MySQL database and user
2. Copy wp-tests-config-sample.php to wp-tests-config.php
3. Editwp-tests-config.php to include your database username and password
4. Then run phpunit.
This is all it takes to run unit test. To run all tests, we enter:
You should hopefully see many dots forming, perhaps with a few S’s and E’s. Each dot represents a test. A flag of S or E indicates an issue which needs to be addressed.
Simply entering phpunit runs all of the tests. But to run a specific test, we enter: