Beruflich Dokumente
Kultur Dokumente
Log in
POSTED ON
September 1, 2014
CATEGORIES
Advanced
Tutorials
TAGS
Unit Tests
COMMENTS
93
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 1/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
This entry is part 2 of 4 in the Unit Tests for WordPress Plugins Series
Show All
← Unit Tests for WordPress Plugins – An Unit Tests for WordPress Plugins – Writing
Introduction Tests →
In part one of this tutorial series we brie ly looked at what unit tests are and we
walked through a couple of simple examples to illustrate how we can use them to
help ensure our code is working properly. Now it is time to actually setup our
testing suite and run our irst unit test.
There are quite a few different ways to setup unit tests in your WordPress plugins,
but by far the easiest is through WP-CLI, a command line interface for WordPress.
Due to its simplicity, we will use WP-CLI to setup our unit tests in this tutorial.
If you are not familiar with or comfortable with the command line, you might be
tempted to walk away from unit tests now and maybe come back another day. I am
going to try and keep this as simple as possible, so I’d encourage you to stick with
me as you will quickly ind that the command line is not nearly as scary or di icult
as some make it out to be.
The phrase “testing suite” simply refers to our collection of unit tests and our
con iguration for those unit tests. When we say that we are going to setup the
testing suite, we mean that we are going to install PHPUnit (or similar), we are going
to create our con iguration ile, and we are going to write our initial unit tests.
Watch the video below for a quick example of what running our unit tests looks like:
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 2/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
Once we are done with the steps below, we should be able to run
the phpunit command from our plugin’s directory and have our unit tests run.
For this tutorial series, we are going to write unit tests for my Restrict Content Pro
plugin.
1. Install PHPUnit
The irst thing we need to do is install PHPUnit. The readme ile in the Github
repository for PHPUnit has the instructions on how to do this.
Simply follow the installation instructions on the Github page and then proceed to
the next step below.
If you run into any issues installing PHPUnit, leave a comment below and I will do
my best to help you.
2. Install WP-CLI
WP-CLI is also exceptionally simple to install, and you can ind the install
instructions here.
If you run into any issues installing WP-CLI, leave a comment below and I will do my
best to help you.
Now we are almost ready to really get into the fun part of unit tests: actually seeing
our unit tests run. Before we do that, however, we need to utilize WP-CLI to create
our initial unit tests con iguration.
Since I’m doing this along with you for my Restrict Content Pro plugin, I will
continually refer to Restrict Content Pro (or RCP). Anytime I do, just substitute in
your own plugin’s name.
WP-CLI includes a step-by-step guide of how to setup unit tests in your plugin, but
I’ i t lk th h it h ll
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 3/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
I’m going to walk through it here as well.
cd sites/wordpress/
Second, we need to instruct WP-CLI to create the initial unit test iles for us:
wp scaffold plugin-tests restrict-content-pro
This will generate all of the iles needed for our unit tests. If you now navigate to
your plugin’s folder and type ls -l, you will see several new iles and folders added.
cd wp-content/plugins/restrict-content-pro
ls -l
Before:
After:
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 4/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
bin/
install-wp-tests.sh
tests/
bootstrap.php
test-sample.php
phpunit.xml
.travis.yml
Replace “root” with the username of your database and replace ” with the database
password. Also replace “localhost” with the hostname of your database. You can
ind all three of these values in your wp-con ig.php ile.
You can now type phpunit into the command line and the unit tests will run:
phpunit
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 5/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
The testing suite that WP-CLI sets up for us includes one sample unit test, which
you can see was run successfully. Let’s take a look at the sample test. It is located
in tests/test-sample.php:
If you look at what this test does, you will probably notice that it isn’t really testing
anything useful. It simple checks if true is equal to true, which we know it is, but it
provides a nice example that is easy to follow.
Now our testing suite is setup and we are ready to begin writing our own tests.
Before we jump into writing an actual unit test, let’s irst look how PHPUnit knows
what to execute as unit tests. The phpunit.xml ile is the main con iguration ile that
instructs PHPUnit on what to do. By default, it looks like this:
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<directory prefix="test-" suffix=".php"d
</testsuite>
</testsuites>
</phpunitd>
This tells PHPUnit where to look for the PHP ile that gets everything running
(tests/bootstrap php) de ines a few options and then also tells PHPUnit where the
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 6/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
(tests/bootstrap.php), de ines a few options, and then also tells PHPUnit where the
actual unit tests live. In this case, the unit tests live in the tests directory and are all
of the .php iles pre ixed with test-. This means that any ile we place in
the tests directory and name test-something.php will be considered unit tests.
This allows us to easily organize our unit tests into groups, which we will discuss
more later.
Note: only methods pre ixed with “test” will be considered a unit test. All other
methods will be skipped.
Let’s write a quick sample test now to help get a better grasp of what we’re doing.
This test won’t do much meaningful, beyond providing a nice example.
To write a new test, all we need to do is create a new method inside the SampleTest
class, like this:
function test_sample_string() {
$string = 'Unit tests are sweet';
$this->assertEquals( 'Unit tests are sweet', $string );
}
This does nothing more than setup a variable called $string, set its value to “Unit
tests are sweet”, and then checks that “Unit tests are sweet” is indeed equal to the
$string variable. If we now run PHPUnit, we will see that we have two tests and both
are passing successfully:
Awesome! We have now written a successful unit test. That’s awesome and you are
now a heck of a lot further along on your journey to understanding and utilizing unit
tests than a huge number of developers.
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 7/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
function test_sample_string() {
$string = 'Unit tests are sweet';
$this->assertEquals( 'Failing Unit tests are sad', $string );
}
Our simple unit test now fails because we are trying to assert that two very-non-
equal strings are equal to each other. Since the two strings are not the same, our
test fails, and PHPUnit tells us that, including why it failed and the exact line number
that failed.
When writing unit tests, there are two main terms we often use to describe what we
are doing: “tests” and “assertions”. A “test” is a collection of assertions and an
assertion is one check within a test. Each test must contain at least one assertion
but a test may also contain many assertions. The sample test we wrote above
contains one assertion.
We will learn more about writing tests and assertions in the next part of this series.
93 thoughts so far
Have your say
← Older Comments
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 8/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
prajakta
March 17, 2017
Your PHP installation appears to be missing the MySQL extension which is required
by WordPress
Reply
Pippin Williamson
eyeoncomputers
May 2, 2017
Really useful. Bugs can pop up over time and having such a tool can be really useful
to ind out where the issues are. Nice work!
Reply
truckeesolutions
June 7, 2017
Reply
truckeesolutions
June 7, 2017
#svn co –quiet
https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/
$WP_TESTS_DIR/includes
#svn co –quiet
https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/
$WP_TESTS_DIR/data
git clone
https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/
$WP_TESTS_DIR/includes
git clone
https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/
$WP_TESTS_DIR/data
truckeesolutions
June 7, 2017
Reply
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 10/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
Andrzej
janro
July 3, 2017
Kyle Tozer
October 6, 2017
Thanks for the fantastic guide! I initially had no problem setting this up, however,
now when I try to run phpunit I receive an error on line 14 of the bootstrap.php ile. I
did not receive this before and I am not sure how to resolve this.
Reply
Jaume
March 5, 2018
Reply
Christian Saborío
April 11, 2018
This was so easy to follow, thank you for taking the time to write this with such
detail.
One minor change that needs to be corrected as it can throw someone off:
https://drive.google.com/open?id=19rFzWL7_9eEzZpO8pMydHvupKUtSogZ7
Reply
Alex Haines
May 8, 2018
Just for your information if you get an issue that no test ran it may be because it is
excluded in the phpunit.xml.dist ile which is installed in the plugin folder.
Reply
Reply
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 12/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
Reply
← Older Comments
Leave a reply
Join the discussion
Comment
Name *
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 14/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
Go for it
POSTED ON
September 1, 2014
CATEGORIES
Advanced
Tutorials
TAGS
Unit Tests
COMMENTS
93
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 15/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
Username
Password
Remember Me
Login
Latest Tutorials
78
)
Gravity Forms is a tremendously powerful plugin for WordPress and Help Scout is an
awesome customer support system that also...
21
)
The WordPress metadata API is a simple way to store and retrieve information related to
various objects in WordPress, such...
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 17/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
23
)
In the previous part of this series we wrote the base class that we can then extend for
each table...
WP Core Contributions
40965
35808
35763
35122
35114
WP Codex Contributions
Function:
get metadata
Function:
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 18/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
Function:
Function:
Function:
Function:
Site Links
Home
Products
Learn
Blog
Refund Policy
Pippin Williamson
Reviews
Contact
A iliate Area
Other Useful Links
Tag Index
Apply Filters
Support the Site
Plugin Support
Random Post
WPBeginner
WP Migrate DB Pro
Pippin.com
Sandhills Development, LLC
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 19/20
02/07/2020 Unit Tests for WordPress Plugins - Setting Up Our Testing Suite - Pippins Plugins
Recommended Products
A iliateWP
Easy Digital Downloads
Restrict Content Pro
Sugar Calendar
SellBird
WP Simple Pay
Email Newsletter
Email Address *
Sign Me Up!
https://pippinsplugins.com/unit-tests-wordpress-plugins-setting-up-testing-suite/ 20/20