我有一个PHPUnit测试用例类(包含一些测试函数).我想oneTimeSetUp()为类中的所有测试编写一个函数来调用一次(不像标准setUp()函数,在类中为每个测试调用一次).换句话说,我正在寻找一个等效于JUnit @BeforeClass注释的PHPUnit .
与oneTimeTearDown()功能相同的问题.
是否有可能在PHPUnit中这样做?
我有:
1. IntegrationTestCase extends TestCase
2. UnitTestCase extends TestCase
3. AcceptanceTestCase extends TestCase
Run Code Online (Sandbox Code Playgroud)
在这些中,我有很多非静态方法,它们在很多测试中使用.我的所有Test类都扩展了这3个类中的一个.
现在在很多测试类中,我有一个setUp方法,它准备所需的数据和服务,并将它们分配给类变量:
class SomeTestClass extends IntegrationTestCase
{
private $foo;
public function setUp()
{
parent::setUp();
$bar = $this->createBar(...);
$this->foo = new Foo($bar);
}
public function testA() { $this->foo...; }
public function testB() { $this->foo...; }
}
Run Code Online (Sandbox Code Playgroud)
setUp每个测试都会遇到问题,无法完成我想要做的事情,如果setUp需要花费很长时间,那么这个方法会乘以测试方法的数量.
使用public function __construct(...) { parent::__construct(..); ... }会产生问题,因为现在Laravel中的低级方法和类不可用.