W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
PHPUnit 可選地允許在每個測試之前備份所有已聲明類的靜態(tài)屬性,并在每個測試結束后還原這些備份。
可以在類級別使用 ?@backupStaticAttributes enabled
? 標注來對本測試用例類中的所有測試啟用此操作:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* @backupStaticAttributes enabled
*/
final class MyTest extends TestCase
{
// ...
}
?@backupStaticAttributes
? 標注也可以用在測試方法這一級別。這樣可以對備份與還原操作進行更細粒度的配置:
use PHPUnit\Framework\TestCase;
/**
* @backupStaticAttributes enabled
*/
class MyTest extends TestCase
{
public function testThatInteractsWithStaticAttributes(): void
{
// ...
}
/**
* @backupStaticAttributes disabled
*/
public function testThatDoesNotInteractWithStaticAttributes(): void
{
// ...
}
}
受限于 PHP 的內(nèi)部實現(xiàn),在某些情況下即使使用了 ?@backupStaticAttributes
? 也可能有個別靜態(tài)值出現(xiàn)意料外的延續(xù),并污染后繼測試。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: