Unit tests are a tool that developers use to test that code is performing properly. The idea behind them is to create a set of tests to determine if your code, given a known data set, gives the expected response.

A unit test should check that one piece of code, such as a function or method, runs properly and returns the expected results.

Through this tutorial series, we will walk through how to setup unit tests for your plugin, how to write the unit tests, how to run the tests, how to write testable code, and also some of the tools that we have available to us to help us leverage the benefits of unit tests. I also want to try and answer the question of “why” you should be writing unit tests for your plugin.

As we go through this tutorial series, I will be writing real unit tests for real plugins to try and better illustrate exactly how they work and how you can implement them in your own plugins.

Let’s look quickly at a simple example of how unit tests are valuable and what we use them for. This is just a simple example and there are many reasons beyond this, but let’s start simple.

Imagine we have a function in our plugin called get_tax( $price ), and this function gets the amount of tax charged for a certain price. If our tax rate is 10% and our price is $10, then we know with some simple math (and assuming simple tax laws) that the amount of tax amount should be $1. The question at this point is whether or not our function, get_tax( 10 ), returns 1 or does it return something unexpected? By writing some simple unit tests, we can determine whether our get_tax() function is working properly.

A good unit test for our get_tax() function would try passing in various numbers in various formats to determine if the results are always what we expect them to be.

For example, our function should be able to account for all of the following number formats:

10

$10

10.00

$10.00

$10.25

1,000

$1000

$1,000

$1,023.522234

10.087622244

If our function is working properly, it will be able to accurately calculate tax on all of those numbers, even with their inconsistent formatting. Note, this is assuming that our get_tax() function includes some kind of input sanitization. We will assume it does.

By writing unit tests that check how the function performs with this variety of inputs, we can better know how reliable our function is.

In English, unit tests for this function would look something like this:

Hey function, if I give you $10, what do I get? I’m hoping for 1.

Hey function, if I give you $1,000, what do I get? I’m hoping for 100.

Hey function, if I give you 10.00233113552, what do I get? I’m hoping for 1.

If any of these checks (we call them assertions) fail, then we know that our function is not functioning properly (assuming our test isn’t broken).

A unit test is nothing more than a programmatic check to determine if a chunk of code performs the way it is supposed to.

So as to not leave you without seeing a real unit test, here’s a real test that we use in Easy Digital Downloads:

public function test_get_sales_by_date ( ) { $stats = new EDD_Payment_Stats ; $sales = $stats -> get_sales ( 0 , 'this_month' ) ; $this -> assertEquals ( 1 , $sales ) ; } public function test_get_sales_by_date() { $stats = new EDD_Payment_Stats; $sales = $stats->get_sales( 0, 'this_month' ); $this->assertEquals( 1, $sales ); }

This unit test is testing whether the get_sales() method of our EDD_Payment_Stats class is functioning properly. We know (because we created it shortly before running this test) that the number of sales for the current month is 1, so does our get_sales() method work properly and return one?

In English, $this->assertEquals( 1, $sales ); says:

Hey get_sales(), if I get sales for the current month, do I get 1 like I hope I do?

In the next part of this series, we will utilize WP-CLI to setup a basic unit tests suite for a simple plugin, then we’ll dive right into writing unit tests.