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 about [PhPUnit].

Installing PhPUnit

In order to have PhPUnit fully operating on your computer system Pear, 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 not a totally different activity from what you should already be doing. It is just a different way of doing it. The difference is between testing, that is, checking that your program behaves as expected, and running a suite of tests, runnable code-fragments that automatically test the correctness of parts (units) of the software. These runnable code-fragments are called unit tests.

The goal of using automated tests is to make fewer mistakes. While your code will still not be perfect, even with excellent tests, you will likely see a dramatic reduction in defects once you start automating tests. Automated tests give you justified confidence in your code. You can use this confidence to take more daring leaps in design (Refactoring), get along with your teammates better (Cross-Team Tests), improve relations with your customers, and go home every night with proof that the system is better now than it was this morning because of your efforts.

For full examples and instructions of PhPUnit written test can be found at Example Coding.

PhPUnit Testing

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