Menu

PHP Unit Testing: Test your code like a pro

Running through a website checking for errors is time consuming. It’s also boring and prone to human error. Unit testing has been around for years and is a much faster and reliable way to test for errors.

How will we perform these tests

There are two clear options when it comes to testing: PHPUnit and SimpleTest. Which one you use is up to you, but I prefer SimpleTest as it is simply a class library as opposed to PHPUnit being part of the PEAR package. It adds less complication and speeds up the development cycle which, of course, is key. For this example you will want to download SimpleTest and extract it to the simpletest directory in your document root.

The basic idea

We want to create cases to test a specific action, we might have hundreds or even thousands of these per project. Running the tests will tell you in anywhere from seconds to a couple of minutes whether there are any errors for you to correct. Perfect for mission critical software.

These small test cases are black and white, they are either going to work or not work, there is no middle ground. An example might be checking a file exists, checking the file is writable, and then ensuring the files first line is not empty. Three very simple tests with very clear outcomes.

Our simple 3 test cases in code

First we are going to need to include the main SimpleTest class. I am going to assume it will be located in your document root in a folder named “simpletest“.

// Include the SimpleTest autorun
include 'simpletest/autorun.php';

There are two ways in which SimpleTest can run unit tests; you can include the autorun.php file like we have above, or you can instantiate the TestOfLogging class and then call the run function with a HtmlReporting parameter. Sticking with what works best, we’ll use autorun.

We now need to create our test. This will be a class than extends UnitTestCase. Each function that we then create that starts with “test” will be run automatically by our parent. As a rule of thumb we want to be as specific as possible when naming functions as to give us more feedback when errors do appear.

// Include the SimpleTest autorun
include 'simpletest/autorun.php';

// Our class extending UnitTestCase
class TestingFile extends UnitTestCase
{
    // Our function names starts with "test" and is very descriptive
    function testFileExistsIsWritableAndIsWrittenTo() {
        // File exists?
        $this->assertTrue(file_exists('test.txt'));

        // File is writable?
        $this->assertTrue(is_writable('test.txt'));

        // File has been written to?
        // Get the file contents into an array, line-by-line
        $fileContent = file('test.txt');
        $this->assertNotNull($fileContent[0]);
    }
}

The output

Of course this should generate three errors – we do not have a test.txt file. So once we add this file to our file system we should now, hopefully, pass the first and second test but fail the third. After all, we may have created the file but it still has no contents. So go ahead and add some content to the first file and re-run the test. All three of the test cases should now pass. Congratulations on your first simple unit test!

What kind of tests can we run?

SimpleTest has many different assertions, I find the most common being assertTrue, assertFalse, assertNull, assertNotNull and assertEqual, and assertNotEqual. For a full list of the assertions you can run with SimpleTest you can view the SimpleTest website, about halfway down the page.