W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
有時(shí)候,想要斷言(比如說(shuō))某方法的運(yùn)行過(guò)程中生成了預(yù)期的輸出(例如,通過(guò) echo
或 print
)。PHPUnit_Framework_TestCase
類使用 PHP 的 輸出緩沖 特性來(lái)為此提供必要的功能支持。
Example?2.16, “對(duì)函數(shù)或方法的輸出進(jìn)行測(cè)試”展示了如何用 expectOutputString()
方法來(lái)設(shè)定所預(yù)期的輸出。如果沒(méi)有產(chǎn)生預(yù)期的輸出,測(cè)試將計(jì)為失敗。
Example?2.16.?對(duì)函數(shù)或方法的輸出進(jìn)行測(cè)試
<?php
class OutputTest extends PHPUnit_Framework_TestCase
{
public function testExpectFooActualFoo()
{
$this->expectOutputString('foo');
print 'foo';
}
public function testExpectBarActualBaz()
{
$this->expectOutputString('bar');
print 'baz';
}
}
?>
phpunit OutputTest
PHPUnit 5.0.0 by Sebastian Bergmann and contributors.
.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.
Table?2.2, “用于對(duì)輸出進(jìn)行測(cè)試的方法”中列舉了用于對(duì)輸出進(jìn)行測(cè)試的各種方法。
Table?2.2.?用于對(duì)輸出進(jìn)行測(cè)試的方法
方法 | 含義 |
---|---|
void expectOutputRegex(string $regularExpression) | 設(shè)置輸出預(yù)期為輸出應(yīng)當(dāng)匹配正則表達(dá)式 $regularExpression 。 |
void expectOutputString(string $expectedString) | 設(shè)置輸出預(yù)期為輸出應(yīng)當(dāng)與 $expectedString 字符串相等。 |
bool setOutputCallback(callable $callback) | 設(shè)置回調(diào)函數(shù),用來(lái)做諸如將實(shí)際輸出規(guī)范化之類的動(dòng)作。 |
Note
在嚴(yán)格模式下,本身產(chǎn)生輸出的測(cè)試將會(huì)失敗。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: