国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

PHPUnit9.0 編寫PHPUnit測試-對輸出進行測試

2022-03-22 11:12 更新

有時候,想要斷言(比如說)某方法的運行過程中生成了預(yù)期的輸出(例如,通過 ?echo ?或 ?print?)。?PHPUnit\Framework\TestCase? 類使用 PHP 的輸出緩沖特性來為此提供必要的功能支持。

示例 2.14 展示了如何用 ?expectOutputString()? 方法來設(shè)定所預(yù)期的輸出。如果沒有產(chǎn)生預(yù)期的輸出,測試將計為失敗。

示例 2.14 對函數(shù)或方法的輸出進行測試

<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class OutputTest extends TestCase
{
    public function testExpectFooActualFoo(): void
    {
        $this->expectOutputString('foo');

        print 'foo';
    }

    public function testExpectBarActualBaz(): void
    {
        $this->expectOutputString('bar');

        print 'baz';
    }
}
$ phpunit OutputTest
PHPUnit latest.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.

表格 2.1 中列舉了用于對輸出進行測試的各種方法

表格 2.1 測試輸出的方法

方法 含義
void expectOutputRegex(string $regularExpression) 設(shè)置輸出預(yù)期為輸出應(yīng)當匹配正則表達式 $regularExpression。
void expectOutputString(string $expectedString) 設(shè)置輸出預(yù)期為輸出應(yīng)當與 $expectedString 相等。
bool setOutputCallback(callable $callback) 設(shè)置回調(diào)函數(shù),用來做諸如將實際輸出規(guī)范化之類的動作。
string getActualOutput() 獲取實際輸出。

注解:

在嚴格模式下,本身產(chǎn)生輸出的測試將會失敗。


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號