Testing is a task every developer has to do eventually on any programming project. You can do it manually after writing all your code to see if it works as you intended, or better yet, before writing your code, using test driven development techniques that will save you time and frustrations down the road. One of those techniques is called Unit Testing.
Ideally, unit testing takes place before writing any code. Its a way to plan out how your functions, classes and objects will behave. The basic idea is that you plan the outcomes of each part of your program, then you write those parts to provide that outcome. So think of it as a black box that you will give some input and expect some output. This way you know how objects will be organized, what methods to write in each class, what needs to be their input and how are they going to output the results. Having all your code in tests will help you add new features, change or refactor parts of your code faster without the fear of breaking something else in your software. If you changed something and all your tests still pass, you’re good to go, if not, you can easily track down where the error is and what got affected.
PHP is famous for having spaghetti code and being too flexible that you can mess things out pretty fast, specially when you’re not an experienced programmer, or you have a team of developers with different programming styles and experience working on several components of the same project.
Although there are several unit testing frameworks for PHP, I’ll talk about SimpleTest since its very light, easy to install and easy to learn. Also, if you’re a Drupal developer, its the testing framework of choice.
To install it, go to the Simpletest download page and get the latest version. Extract the files to your PHP path, or use require_once() calls to the autorun file:
Here’s an example of a class’ tests (testfile.php):
/* test result is an array */
/* test correct number of output elements */
/* make a different call with different parameter */
$result = $obj->getList();
/* confirm that output is always an array, even with 0 elements */
Now you have a basic skeleton of you methods and their behaviors, now you can go ahead and write the real code (myclass.php).
Once we’ve written the class, we can test out its methods by running our tests script. You can do that by using the command line, or by viewing the php script in a browser window.
Personally, I prefer to use the command line for convenience and faster testing without needing even to setup any web server. Running it will look like this:
$ php testfile.php
Test cases run: 1/1, Passes: 3, Failures: 0, Exceptions: 0
This is a very small and simple test, but you can also test out the interface of your project. SimpleTest has a browser class that can perform some events like simulating browser visits, doing clicks on your interface’s links and checking the HTML structure of the web server responses.
Check out the full documentation, which is very easily laid out as a big tutorial, and start testing all your code!