PHPUnitManual:4.5

From 흡혈양파의 번역工房
Jump to navigation Jump to search
4.5 출력 내용의 테스트

출력 내용의 테스트

메소드의 실행 결과를 확인하는 방법으로, (echo 나 print 등에 의한) 출력이 예상과 같은지를 조사하고 싶은 경우도 있을 것입니다. PHPUnit_Framework_TestCase 클래스는 PHP 의 출력 버퍼링 [1] 기능을 사용하여 이 기능을 제공합니다.


예제 4.13 "함수나 메소드의 출력 내용 테스트" 에는 예상되는 출력 내용을 expectedOutputString() 메소드로 설정하는 방법이 나와 있습니다. 예상과 같은 출력을 얻지 못 한 경우, 이 테스트는 실패로 취급됩니다.


예제 4.13 함수나 메소드의 출력 내용 테스트

<?php
class OutputTest extends PHPUnit_Framework_TestCase
{
	public function testExpectFooActualFoo()
	{
		$this->expectOutputString('foo');
		print 'foo';
	}
	public function testExpectBarActualBaz()
	{
		$this->expectOutputString('bar');
		print 'baz';
	}
}
?>
PHPUnit 3.7.0 by Sebastian Bergmann.
.F
Time: 0 seconds, Memory: 5.75Mb
There was 1 failure:
1) OutputTest::testExpectBarActualBaz
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'bar'
+'baz'
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.phpunit OutputTest
PHPUnit 3.7.0 by Sebastian Bergmann.
.F
Time: 0 seconds, Memory: 5.75Mb
There was 1 failure:
1) OutputTest::testExpectBarActualBaz
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'bar'
+'baz'
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.


표4.2 "테스트 출력용 메소드" 는, 테스트의 출력용으로 제공되는 메소드를 정리한 것입니다.

메소드 의미
void expectOutputRegex(string $regularExpression) 출력이 정규식 $regularExpression 에 매치할 것이라는 예상을 설정합니다.
void expectOutputString(string $expectedString) 출력이 문자열 $expetedString 과 같을 것이라는 예상을 설정합니다.
bool setOutputCallback(callable $callback) 예를 들어 출력할 때의 정규화 등에 사용하는 callback 함수를 설정합니다.
표4.2 테스트 출력용 메소드


Notes