W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
開始寫新的測試用例類時(shí),可能想從寫下空測試方法開始,比如:
public function testSomething()
{
}
以此來跟蹤需要編寫的測試??諟y試的問題是 PHPUnit 框架會(huì)將它們解讀為成功。這種錯(cuò)誤解讀導(dǎo)致錯(cuò)誤報(bào)告變得毫無用處——無法分辨出測試是真的成功了還是根本就未編寫實(shí)現(xiàn)。在未實(shí)現(xiàn)的測試中調(diào)用 $this->fail()
同樣沒啥幫助,因?yàn)闇y試將被解讀為失敗。這和將未實(shí)現(xiàn)的測試解讀為成功是一樣的錯(cuò)誤。
假如把成功的測試視為綠燈、測試失敗視為紅燈,那么還額外需要黃燈來將測試標(biāo)記為未完成或尚未實(shí)現(xiàn)。PHPUnit_Framework_IncompleteTest
是一個(gè)標(biāo)記接口,用于將測試方法拋出的異常標(biāo)記為測試未完成或目前尚未實(shí)現(xiàn)而導(dǎo)致的結(jié)果。PHPUnit_Framework_IncompleteTestError
是這個(gè)界面的標(biāo)準(zhǔn)實(shí)現(xiàn)。
Example?7.1, “將測試標(biāo)記為未完成”展示了一個(gè)測試用例類 SampleTest
,它有一個(gè)測試方法 testSomething()
。通過在測試方法中調(diào)用便捷方法 markTestIncomplete()
(會(huì)自動(dòng)拋出一個(gè) PHPUnit_Framework_IncompleteTestError
異常)將這個(gè)測試標(biāo)記為未完成。
Example?7.1.?將測試標(biāo)記為未完成
<?php
class SampleTest extends PHPUnit_Framework_TestCase
{
public function testSomething()
{
// 可選:如果愿意,在這里隨便測試點(diǎn)什么。
$this->assertTrue(TRUE, '這應(yīng)該已經(jīng)是能正常工作的。');
// 在這里停止,并將此測試標(biāo)記為未完成。
$this->markTestIncomplete(
'此測試目前尚未實(shí)現(xiàn)。'
);
}
}
?>
在 PHPUnit 命令行測試執(zhí)行器的輸出中,未完成的測試記為 I
,如下例所示:
phpunit --verbose SampleTest
PHPUnit 5.0.0 by Sebastian Bergmann and contributors.
I
Time: 0 seconds, Memory: 3.95Mb
There was 1 incomplete test:
1) SampleTest::testSomething
This test has not been implemented yet.
/home/sb/SampleTest.php:12
OK, but incomplete or skipped tests!
Tests: 1, Assertions: 1, Incomplete: 1.
Table?7.1, “用于未完成的測試的 API”列舉了用于將測試標(biāo)記為未完成的 API。
Table?7.1.?用于未完成的測試的 API
方法 | 含義 |
---|---|
void markTestIncomplete() |
將當(dāng)前測試標(biāo)記為未完成。 |
void markTestIncomplete(string $message) | 將當(dāng)前測試標(biāo)記為未完成,并用 $message 作為說明信息。 |
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)系方式:
更多建議: