| Prev | Next |
PHPUnit provides extensions to the standard base-class for test classes,
PHPUnit_Framework_TestCase, that aid in the writing
of tests for output and performance regressions.
Sometimes you want to assert that the execution of a method, for
instance, generates an expected output (via echo or
print, for example). The
PHPUnit_Extensions_OutputTestCase class uses PHP's
Output
Buffering feature to provide the functionality that is
necessary for this.
Example 8.1
shows how to subclass PHPUnit_Extensions_OutputTestCase
and use its expectOutputString() method to set the
expected output. If this expected output is not generated, the test
will be counted as a failure.
Example 8.1: Using PHPUnit_Extensions_OutputTestCase
<?php
require_once 'PHPUnit/Extensions/OutputTestCase.php';
class OutputTest extends PHPUnit_Extensions_OutputTestCase
{
public function testExpectFooActualFoo()
{
$this->expectOutputString('foo');
print 'foo';
}
public function testExpectBarActualBaz()
{
$this->expectOutputString('bar');
print 'baz';
}
}
?>
phpunit OutputTest
PHPUnit 3.3.0 by Sebastian Bergmann.
.F
Time: 0 seconds
There was 1 failure:
1) testExpectBarActualBaz(OutputTest)
Failed asserting that two strings are equal.
expected string <bar>
difference < x>
got string <baz>
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
Table 8.1
shows the methods provided by
PHPUnit_Extensions_OutputTestCase.
Table 8.1. OutputTestCase
| Method | Meaning |
|---|---|
void expectOutputRegex(string $regularExpression) | Set up the expectation that the output matches a $regularExpression. |
void expectOutputString(string $expectedString) | Set up the expectation that the output is equal to an $expectedString. |
bool setOutputCallback(callable $callback) | Sets up a callback that is used to, for instance, normalize the actual output. |
You can extend your test class from PHPUnit_Extensions_PerformanceTestCase
to test whether the execution of a function or method call, for
instance, exceeds a specified time limit.
Example 8.2
shows how to subclass PHPUnit_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.2: Using PHPUnit_Extensions_PerformanceTestCase
<?php
require_once 'PHPUnit/Extensions/PerformanceTestCase.php';
class PerformanceTest extends PHPUnit_Extensions_PerformanceTestCase
{
public function testPerformance()
{
$this->setMaxRunningTime(2);
sleep(1);
}
}
?>
Table 8.2
shows the methods provided by
PHPUnit_Extensions_PerformanceTestCase.
Table 8.2. PerformanceTestCase
| Method | Meaning |
|---|---|
void setMaxRunningTime(int $maxRunningTime) | Set the maximum running time for the test to $maxRunningTime (in seconds). |
integer getMaxRunningTime() | Return the maximum running time allowed for the test. |
There are two other extensions to PHPUnit_Framework_TestCase,
PHPUnit_Extensions_Database_TestCase and
PHPUnit_Extensions_SeleniumTestCase, that are covered
in Chapter 9 and Chapter 18,
respectively.
| Prev | Next |
assertArrayHasKey()assertClassHasAttribute()assertClassHasStaticAttribute()assertContains()assertContainsOnly()assertEqualXMLStructure()assertEquals()assertFalse()assertFileEquals()assertFileExists()assertGreaterThan()assertGreaterThanOrEqual()assertLessThan()assertLessThanOrEqual()assertNotNull()assertObjectHasAttribute()assertRegExp()assertSame()assertSelectCount()assertSelectEquals()assertSelectRegExp()assertStringEqualsFile()assertTag()assertThat()assertTrue()assertType()assertXmlFileEqualsXmlFile()assertXmlStringEqualsXmlFile()assertXmlStringEqualsXmlString()Copyright © 2005-2009 Sebastian Bergmann.