Setting Up and Using PHPUnit



In the last decade, PHP has developed from a niche language for adding dynamic functionality to small websites to a powerful tool making strong inroads into large-scale, business-critical Web systems. Financial institutions such as banks and insurance companies use PHP, for instance, to develop and maintain solutions for Basel II Credit Rating. Critical business logic like this needs to work correctly. To make code testing viable, good tool support is needed. This is where PHPUnit comes into play. It provides both a framework that makes the writing of tests easy as well as the functionality to easily run the tests and analyse their results. But how do you ensure that it does? You test it, of course. For further information, visit PHPUnit website.

Installing PHPUnit

PHPUnit is a part of the PEAR Packages. PEAR is the backbone of running PhPunit since first being intergrated into PHP. Instructions to installing PhPUnit can be easily found at Installing PhPUnit:

Writing Test Code for PHPUnit

Testing with PHPUnit is simple to learn and implement. The basic idea is to check that the codes written behave as expected, and to run a suite of tests, runnable code-fragments that automatically test the correctness of parts (individual units) of the software. These runnable code-fragments are called unit tests.

The goal of using automated tests is to make fewer mistakes. While the codes might still not be perfect, even with excellent tests, it is likely to see a dramatic reduction in defects once automated tests are run. This makes the process of quality assurance and usability testing much easier.

Writing tests

Each class in your code should have a corresponding test class called [YourClassname]Test. Make sure your test class extends PHPUnit_Framework_TestCase. Inside these classes, you can define setUp() and tearDown() functions, which will be called before and after each test and can be used to setup the environment for the test. This is generally helpful to test functions which make database queries and manipulate with the data therein. So, the setup() functions help in setting up the insertion and anyother queries that would manipulate the data in the database. teardown() function on the other hand, would reverse all these changes so that the database remains intact after the tests are over.

The setup() and teardown() functions are followed by the test functions. Each test function within the class must have test in front of the function name. For better visual examples and instructions for writing PhPUnit tests, visit

PHPUnit Testing

The PHPUnit command-line test runner can be invoked through the phpunit command. Command operations for running PHPUnit for code testing

For extra information and examples of Orgainizing Test Suites can be found at Organize Test Suites