![]() Join Up! 96840 members and counting! |
|
|||
Debugging PHP
Oier Blasco
Introduction
The debugging process is a time consuming activity. When the project is a
small group of scripts all is easy, but when the size and complexity of
the project grows, the debugging time increases dramatically. With PhpUnit
you can speed up your debugging process.
PhpUnit is a PHP port of JUnit test framework wrote by Fred Yankowski
, you can download it from
http://www.ontosys.com/phiki/PhpUnit.
With PhpUnit you can write a suite of tests to check the correctness of your
code like a security harness. After the creation of the test suite you can
run all the tests automatically in a single step.
When you detect a bug, you can write a test to catch it. If the same bug is
introduced again you will detect it as soon as you run your test suite. If
you run the test suite frequently your will increase the robustness of your
code.
Getting started
Suppose we want to write a test suite for our simple Account class.
Creating the test class
The first step is the creation of AccountTester class as a subclass of TestCase class provided by PhpUnit.
The TestCase class has two methods, setUp and tearDown, that are interesting to us. Both methods have an
empty implementation in the TetsCase class and must be overridden in order to be used.
The setUp method is used for initialization stuff. In our case we used to initialize the sample accounts
used in our tests.The tearDown method is used for clean up stuff, we don't need it for our tests.
Our AccountTester class looks like this:
Adding Individual Tests
Now we must add our first test methods to AccountTester class.
The key of the test is the assert method. If the expression inside the assert is true the test is
passed, otherwise an error as occurred.
A lot of asserts check for the equality of two values, for this reason the the TestCase class
has a assertEquals method. The assertEqual method has three parameters: the expected value, the
actual value, and a optional message to be printed if the test fails.
Running The Test
The time to run our test has arrived, we must write a script to run the tests. We create a
runtest.php and put the following lines of code inside it:
After the creation of the TestSuite object we must add each test to the suite. The parameter of
the AccountTester's constructor is the name of the test method that we want to execute.
The results of the tests are encapsulated in the TestResult class. In order to output the
results of the test we use the TextTestResult class provided by PhpUnit. The TextTestResult is a
specialization of TestResult for the creation of text/html reports. We can write our customized
subclasses of TestResult if we need more control over the output.
In the last lines we run the test and print the results.
Tips and tricks
When you write a new test, introduce a little bug to be sure that the test is working correctly.
For example, we can introduce a little bug into our Account class.
Now we run the tests and we can see quickly if the error is detected.
Write tests for relevant methods, you don't need a test for each method in your class.
Write the tests before you start programming. This will give you better understanding of how your code must work.
Now you can see how the bug hunting time is reduced, increasing your efficiency as a programmer. Happy hunting!
-- Oier
|