PHPUnit 支持對(duì)測(cè)試方法之間的顯式依賴關(guān)系進(jìn)行聲明。這種依賴關(guān)系并不是定義在測(cè)試方法的執(zhí)行順序中,而是允許生產(chǎn)者(?producer
?)返回一個(gè)測(cè)試基境(?fixture
?)的實(shí)例,并將此實(shí)例傳遞給依賴于它的消費(fèi)者(?consumer
?)們。用 ?@depends
? 標(biāo)注來表示依賴關(guān)系,展示了如何用 ?@depends
? 標(biāo)注來表達(dá)測(cè)試方法之間的依賴關(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);
}
}
更多建議: