W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
當(dāng)有測(cè)試失敗時(shí),PHPUnit 全力提供盡可能多的有助于找出問題所在的上下文信息。
Example?2.17.?數(shù)組比較失敗時(shí)生成的錯(cuò)誤相關(guān)信息輸出
<?php
class ArrayDiffTest extends PHPUnit_Framework_TestCase
{
public function testEquality() {
$this->assertEquals(
array(1,2,3 ,4,5,6),
array(1,2,33,4,5,6)
);
}
}
?>
phpunit ArrayDiffTest
PHPUnit 5.0.0 by Sebastian Bergmann and contributors.
F
Time: 0 seconds, Memory: 5.25Mb
There was 1 failure:
1) ArrayDiffTest::testEquality
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
Array (
0 => 1
1 => 2
- 2 => 3
+ 2 => 33
3 => 4
4 => 5
5 => 6
)
/home/sb/ArrayDiffTest.php:7
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
在這個(gè)例子中,數(shù)組中只有一個(gè)值不同,但其他值也都同時(shí)顯示出來(lái),以提供關(guān)于錯(cuò)誤發(fā)生的位置的上下文信息。
當(dāng)生成的輸出很長(zhǎng)而難以閱讀時(shí),PHPUnit 將對(duì)其進(jìn)行分割,并在每個(gè)差異附近提供少數(shù)幾行上下文信息。
Example?2.18.?長(zhǎng)數(shù)組比較失敗時(shí)生成的錯(cuò)誤相關(guān)信息輸出
<?php
class LongArrayDiffTest extends PHPUnit_Framework_TestCase
{
public function testEquality() {
$this->assertEquals(
array(0,0,0,0,0,0,0,0,0,0,0,0,1,2,3 ,4,5,6),
array(0,0,0,0,0,0,0,0,0,0,0,0,1,2,33,4,5,6)
);
}
}
?>
phpunit LongArrayDiffTest
PHPUnit 5.0.0 by Sebastian Bergmann and contributors.
F
Time: 0 seconds, Memory: 5.25Mb
There was 1 failure:
1) LongArrayDiffTest::testEquality
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
13 => 2
- 14 => 3
+ 14 => 33
15 => 4
16 => 5
17 => 6
)
/home/sb/LongArrayDiffTest.php:7
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
當(dāng)比較失敗時(shí),PHPUnit 為輸入值建立文本表示,然后以此進(jìn)行對(duì)比。這種實(shí)現(xiàn)導(dǎo)致在差異指示中顯示出來(lái)的問題可能比實(shí)際上存在的多。
這種情況只出現(xiàn)在對(duì)數(shù)組或者對(duì)象使用 assertEquals 或其他“弱”比較函數(shù)時(shí)。
Example?2.19.?當(dāng)使用弱比較時(shí)在生成的差異結(jié)果中出現(xiàn)的邊緣情況
<?php
class ArrayWeakComparisonTest extends PHPUnit_Framework_TestCase
{
public function testEquality() {
$this->assertEquals(
array(1 ,2,3 ,4,5,6),
array('1',2,33,4,5,6)
);
}
}
?>
phpunit ArrayWeakComparisonTest
PHPUnit 5.0.0 by Sebastian Bergmann and contributors.
F
Time: 0 seconds, Memory: 5.25Mb
There was 1 failure:
1) ArrayWeakComparisonTest::testEquality
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
Array (
- 0 => 1
+ 0 => '1'
1 => 2
- 2 => 3
+ 2 => 33
3 => 4
4 => 5
5 => 6
)
/home/sb/ArrayWeakComparisonTest.php:7
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
在這個(gè)例子中,第一個(gè)索引項(xiàng)中的 1
and '1'
在報(bào)告中被視為不同,雖然 assertEquals 認(rèn)為這兩個(gè)值是匹配的。
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)系方式:
更多建議: