PHPUnit

Written by: Chris in Product Reviews, Web Technology

PHPUnit is a unit testing framework written in and for PHP.

What is unit testing?

Unit testing is the practice of testing particular functions or classes to ensure that they behave as expected. Think of it as an automated test run of your code.

Is unit testing really useful?

Unity testing is useful for any projects but small ones.  It is extremely easy to run and re-run all your tests the moment you or a co-worker have finished implementing a new feature, and this can be done in the background while you work on another feature or project.

It is not as effective with smaller projects because these are usually easy and quick to test anyway, and the time spent writing the tests would not be futile.

But you can’t test output, can you?

Of course you can! PHPUnit provides you with functions (using PHP’s own output buffering) to capture your program’s output and compare it to expected output.  This means that you can test everything that reaches a web browser.  The only thing that is left out of your grasp here is how it will display in browsers, and how your JavaScript will behave (but this is where Selenium comes into play, which PHPUnit can work in conjunction).

How easy is PHPUnit to use?

It is incredibly easy to use.  The documentation provides examples that can be mimicked or played with after copying and pasting.  Writing tests from scratch requires only reading the appropriate sections of the documentation. PHPUnit provides an in-depth framework that is simple and easy to understand thanks to the documentation.

But how well documented is it?

The PHPUnit documentation is very extensive, with plenty of examples and explanations given along the way. Some important sections are even linked directly from the home page to make it easy to get started.

So go on, explore PHPUnit and start testing your code!

One Response to “PHPUnit”