PHPUnit9.0 標注-@backupStaticAttributes

2022-03-24 10:58 更新
PHPUnit9.0 標注集合PHPUnit9.0 標注集合

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ù),并污染后繼測試。


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號