PHPUnit9.0 標(biāo)注-@depends

2022-03-24 10:59 更新
PHPUnit9.0 標(biāo)注集合PHPUnit9.0 標(biāo)注集合

PHPUnit 支持對測試方法之間的顯式依賴關(guān)系進行聲明。這種依賴關(guān)系并不是定義在測試方法的執(zhí)行順序中,而是允許生產(chǎn)者(?producer?)返回一個測試基境(?fixture?)的實例,并將此實例傳遞給依賴于它的消費者(?consumer?)們。用 ?@depends? 標(biāo)注來表示依賴關(guān)系,展示了如何用 ?@depends? 標(biāo)注來表達測試方法之間的依賴關(guān)系。

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

final class StackTest extends TestCase
{
    public function testEmpty(): array
    {
        $stack = [];
        $this->assertEmpty($stack);

        return $stack;
    }

    /**
     * @depends testEmpty
     */
    public function testPush(array $stack): array
    {
        array_push($stack, 'foo');
        $this->assertSame('foo', $stack[count($stack)-1]);
        $this->assertNotEmpty($stack);

        return $stack;
    }

    /**
     * @depends testPush
     */
    public function testPop(array $stack): void
    {
        $this->assertSame('foo', array_pop($stack));
        $this->assertEmpty($stack);
    }
}
    


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號