錯(cuò)誤相關(guān)信息的輸出

2018-02-24 15:41 更新

錯(cuò)誤相關(guān)信息的輸出

當(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è)值是匹配的。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)