获取正在 `setUp` 中执行的测试

Getting the test that is executing in `setUp`

有没有办法在[=18]中的setUp()中获取即将执行的测试(实际的函数名) =]PHPUnit?

我知道我可以将代码放在测试函数本身而不是 setUp() 中,但我已经创建了一些 Abstract 测试用例并且自 setUp() 需要很长时间才能执行,我希望能够跳过不必要的操作甚至 setUp().

中的测试

可以使用getName方法,return测试用例的名称(被调用的函数)。如果您将 true 作为参数传递,它将 return 数据集的名称(在数据提供者的情况下)。所以只需使用:

public function setUp()
{
    var_dump($this->getName(false) );  // The name of the method without dataprovider

    var_dump(get_called_class()); // The name of the TestCase Class
}

希望对您有所帮助