第七章 测试异常和性能回归
PHPUnit提供了二个扩展,基于测试类的标准基类PHPUnit2_Framework_TestCase,协助为书写异常和性能回归测试。
7-1 异常
怎么测试异常?当异常抛出时,无法直接使用断言。相反,必须使用PHP 的异常处理机制来书写测试。以下例子示范了入阁测试异常:
<?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) {
}
}
}
?>
如果预计要抛出异常的代码没有抛出异常,随后调用的fail()函数(参见本书后的表7)将暂停测试,抛出一个测试有问题的信号。如果预计的异常抛出,每个catch的语句块都会被执行,测试将继续执行。
另外一个测试异常的方法是,测试类可以继承PHPUnit2 _ Extensions_ExceptionTestCase,这可以测试被测试的代码是否抛出了异常。例7展示了如何子类化PHPUnit2_Extensions_ExceptionTestCase 和使用它的setExpectedException() 方法设置预计的异常。如果预计的异常没有抛出,测试将算作是一次失败。
例7.使用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.
表1显示的PHPUnit2_Extensions_ExceptionTestCase 实现的外部协议。
表1 扩展TestCase的外部协议方法
方法 |
描述 |
void setExpectedException(String $exceptionName) |
将$exceptionName变量设置为预计的异常名 |
String getExpectedException( ) |
返回期望的异常名称 |
7-2 性能回归
从PHPUnit2_Extensions _ PerformanceTestCase扩展测试类,可以用于测试函数或方法的调用,如,是否超出的运行时限。
例8展示了怎么继承PHPUnit2_Extensions _ PerformanceTestCase类,使用setMaxRunningTime() 方法设置测试的最大运行时间。如果测试的执行超出了时限,这可以作为测试失败了。
例8 使用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 2. Performance TestCase external protocols Method
表2 性能测试用例的外部协议方法
表2
方法 |
描述 |
void setMaxRunningTime(integer $maxRunningTime) |
将变量$maxRunningTime(秒)设为测试运行的最大时间。 |
integer getMaxRunningTime( ) |
返回测试允许的最大运行时间 |
--------------------------------------------------------------------------------------------------------------------
原文:
Chapter 7. Testing Exceptions and Performance Regressions
PHPUnit provides two extensions that aid in the writing of tests for exceptions and performance regressions to the standard base class for test classes, PHPUnit2_Framework_TestCase.
7-1. Exceptions
How do you test exceptions? You cannot assert directly 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 7, later in this book) 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 7 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 7. 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 1 shows the external protocol implemented by PHPUnit2_Extensions_ExceptionTestCase.
Table 1. Extension TestCase external protocols Method
Description
void setExpectedException(String $exceptionName)
Sets the name of the expected exception to $exceptionName.
String getExpectedException( )
Returns the name of the expected exception.
7-2. Performance Regressions
You can extend your test class from PHPUnit2_Extensions_ PerformanceTestCase to test whether the execution of a function or a method call, for instance, exceeds a specified time limit.
Example 8 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 8. 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 2 shows the external protocol implemented by PHPUnit2_Extensions_PerformanceTestCase.
Table 2. Performance TestCase external protocols Method
Description
void setMaxRunningTime(integer $maxRunningTime)
Sets the maximum running time for the test to $maxRunningTime (in seconds)..
integer getMaxRunningTime( )
Returns the maximum running time allowed for the test.