Laravel 测试用例方法上的 @beforeClass 注释被忽略

@beforeClass annotation on Laravel testcase method ignored

在我的 Laravel 5.1 应用程序的扩展 Illuminate\Foundation\Testing\TestCase 基础测试用例中,我有这个方法:

/**
 * @beforeClass
 */
public function resetDatabase()
{
    echo "I never see this message\n";
    $this->artisan("migrate:refresh");
}

class 中的其他@before 和@after 注释与广告一样受到尊重。为什么我的单元测试没有调用这个方法?

原来所有的注解都不相等!

检查 vendor/phpunit/phpunit/src/Util/Test.php 显示:

private static function isBeforeClassMethod(ReflectionMethod $method)
{
    return $method->isStatic() && strpos($method->getDocComment(), '@beforeClass') !== false;
}


private static function isBeforeMethod(ReflectionMethod $method)
{
    return preg_match('/@before\b/', $method->getDocComment());
}

因此,如果我 将函数设为静态 ,它会在 class 测试之前被识别并运行。周围的@before注解示例都是实例方法,所以这导致混淆。