修改PHPUnit中测试方法的名称

4
有没有办法更改PHPUnit用于查找测试的方法名称?我想将默认的“test”前缀更改为使用BDD样式,并以“it”开头。
我的搜索只找到了更改phpunit.xml配置文件中文件名称的方法。
<directory prefix="test-" suffix=".php">./tests/</directory>

感谢您的选择。
1个回答

5

我进行了一些调查,并在 Framework\TestSuite.php 中找到了以下内容:

public static function isTestMethod(ReflectionMethod $method)
{
    if (strpos($method->name, 'test') === 0) {
        return TRUE;
    }

    // @scenario on TestCase::testMethod()
    // @test     on TestCase::testMethod()
    return strpos($method->getDocComment(), '@test')     !== FALSE ||
           strpos($method->getDocComment(), '@scenario') !== FALSE;
}

所以,名称要么以"test"开头,要么具有注释@test@scenario。我已经确认了注释的有效性。

正如您所看到的,此函数中没有传递任何配置,因此我认为您无法配置自定义前缀。


很遗憾,我也希望能够避免注释。感谢你的努力。 - Darshan Sawardekar
这是一个使用注释的好例子:https://github.com/laracasts/Flexible-Flash-Messages/blob/master/app/tests/ExampleTest.php - Michael J. Calkins

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接