PHPUnit9.0 标注-@backupStaticAttributes
2022-03-24 10:58 更新
PHPUnit9.0 标注集合
PHPUnit 可选地允许在每个测试之前备份所有已声明类的静态属性,并在每个测试结束后还原这些备份。
可以在类级别使用 @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 的内部实现,在某些情况下即使使用了 @backupStaticAttributes
也可能有个别静态值出现意料外的延续,并污染后继测试。
以上内容是否对您有帮助:
更多建议: