Chapter 6. Testing Exceptions and Performance Regressions

PHPUnit provides two extensions to the standard base-class for test classes, PHPUnit2_Framework_TestCase, that aid in the writing of tests for exceptions and performance regressions.

Exceptions

How do you test exceptions? You cannot directly assert that they are raised. Instead you have to use PHP's exception handling facilities to write the test. The following example demonstrates testing exceptions:

<?php
require_once 'PHPUnit2/Framework/TestCase.php';
 
class ExceptionTest extends PHPUnit2_Framework_TestCase {
    public function testException() {
        try {
            // ... Code that is expected to raise an Exception ...
            $this->fail('No Exception has been raised.');
        }
 
        catch (Exception $expected) {
        }
    }
}
?>

If the code that is expected to raise an exception does not raise an exception, the subsequent call to fail() (see Table 14.2) will halt the test and signal a problem with the test. If the expected exception is raised, the catch block will be executed, and the test will continue executing.

Alternatively, you can extend your test class from PHPUnit2_Extensions_ExceptionTestCase to test whether an exception is thrown inside the tested code. Example 6.1 shows how to subclass PHPUnit2_Extensions_ExceptionTestCase and use its setExpectedException() method to set the expected exception. If this expected exception is not thrown, the test will be counted as a failure.

Example 6.1: Using PHPUnit2_Extensions_ExceptionTestCase

<?php
require_once 'PHPUnit2/Extensions/ExceptionTestCase.php';
 
class ExceptionTest extends PHPUnit2_Extensions_ExceptionTestCase {
    public function testException() {
        $this->setExpectedException('Exception');
    }
}
?>
phpunit ExceptionTest
PHPUnit 2.3.0 by Sebastian Bergmann.

F

Time: 0.006798
There was 1 failure:
1) testException(ExceptionTest)
Expected exception Exception

FAILURES!!!
Tests run: 1, Failures: 1, Errors: 0, Incomplete Tests: 0.

Table 6.1 shows the external protocol implemented by PHPUnit2_Extensions_ExceptionTestCase.

Table 6.1. ExceptionTestCase external protocols

MethodMeaning
void setExpectedException(String $exceptionName)Set the name of the expected exception to $exceptionName.
String getExpectedException()Return the name of the expected exception.

Performance Regressions

You can extend your test class from PHPUnit2_Extensions_PerformanceTestCase to test whether the execution of a function or method call, for instance, exceeds a specified time limit.

Example 6.2 shows how to subclass PHPUnit2_Extensions_PerformanceTestCase and use its setMaxRunningTime() method to set the maximum running time for the test. If the test is not executed within this time limit, it will be counted as a failure.

Example 6.2: Using PHPUnit2_Extensions_PerformanceTestCase

<?php
require_once 'PHPUnit2/Extensions/PerformanceTestCase.php';
 
class PerformanceTest extends PHPUnit2_Extensions_PerformanceTestCase {
    public function testPerformance() {
        $this->setMaxRunningTime(2);
        sleep(1);
    }
}
?>

Table 6.2 shows the external protocol implemented by PHPUnit2_Extensions_PerformanceTestCase.

Table 6.2. ExceptionTestCase external protocols

MethodMeaning
void setMaxRunningTime(integer $maxRunningTime)Set the maximum running time for the test to $maxRunningTime (in seconds).
integer getMaxRunningTime()Return the maximum running time allowed for the test.