Приглашаем посетить
Древнерусская литература (drevne-rus-lit.niv.ru)

Chapter 2.  Automating Tests

Previous
Table of Contents
Next

Chapter 2. Automating Tests

Even good programmers make mistakes. The difference between a good programmer and a bad programmer is that the good programmer uses tests to detect his mistakes as soon as possible. The sooner you test for a mistake, the greater your chance of finding it, and the less it will cost to find and fix. This explains why it is so problematic to leave testing until just before releasing software. Most errors do not get caught at all, and the cost of fixing the ones you do catch is so high that you have to perform triage with the errors because you just cannot afford to fix them all.

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 testingthat is, checking that your program behaves as expectedand performing a battery of testsrunnable code-fragments that automatically test the correctness of parts (units) of the software. These runnable code-fragments are called unit tests.

In this section, we will go from simple print-based testing code to a fully automated test. Imagine that we have been asked to test PHP's built-in Array. One bit of functionality to test is the function sizeof( ). For a newly created array, we expect the sizeof( ) function to return 0. After we add an element, sizeof( ) should return 1. Example 1 shows what we want to test.

Example 1. Testing Array and sizeof( )
<?php
$fixture = Array( );
// $fixture is expected to be empty.

$fixture[] = "element";
// $fixture is expected to contain one element.
?>

A really simple way to check whether we are getting the results we expect is to print the result of sizeof( ) before and after adding the element (see Example 2). If we get 0 and then 1, Array and sizeof( ) are behaving as expected.

Example 2. Using print to test Array and sizeof( )
<?php
$fixture = Array( );
print sizeof($fixture) . "\n";

$fixture[] = "element";
print sizeof($fixture) . "\n";
?>
0
1

Now, we would like to move from tests that require manual interpretation to tests that can run automatically. In Example 3, we write the comparison of the expected and actual values into the test code and print ok if the values are equal. If we see a not ok message, we know something is wrong.

Example 3. Comparing expected and actual values to test Array and sizeof( )
<?php
$fixture = Array( );
print sizeof($fixture) == 0 ? "ok\n" : "not ok\n";

$fixture[] = "element";
print sizeof($fixture) == 1 ? "ok\n" : "not ok\n";
?>
ok
ok

We now factor out the comparison of expected and actual values into a function that raises an exception when there is a discrepancy (Example 4). Now our test output gets simpler. Nothing gets printed if the test succeeds. If we see an unhandled exception, we know something has gone wrong.

Example 4. Using an assertion function to test Array and sizeof( )
<?php
$fixture = Array( );
assertTrue(sizeof($fixture) = = 0);

$fixture[] = "element";
assertTrue(sizeof($fixture) = = 1);

function assertTrue($condition) {
 if (!$condition) {
  throw new Exception("Assertion failed.");
 }
}
?>

The test is now completely automated. Instead of just testing as we did with our first version, with this version, we have an automated test.

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 (see "Refactoring," later in this book), get along better with your teammates (see "CrossTeam Tests," later in this book), improve relations with your customers, and go home every night with proof that the system is better now than it was that morning because of your efforts.


Previous
Table of Contents
Next