如何跳过PHPunit中的测试?

Fil*_*ype 73 php phpunit

我正在使用与jenkins相关的phpunit,我想通过在XML文件中设置配置来跳过某些测试 phpunit.xml

我知道我可以在命令行上使用:

phpunit --filter testStuffThatBrokeAndIOnlyWantToRunThatOneSingleTest

如何将其转换为XML文件,因为<filters>标记仅用于代码覆盖?

我想进行所有测试 testStuffThatAlwaysBreaks

jst*_*ann 137

跳过破坏或需要继续处理的测试的最快最简单的方法是将以下内容添加到单个单元测试的顶部:

$this->markTestSkipped('must be revisited.');
Run Code Online (Sandbox Code Playgroud)

  • 因为它是一个静态方法(至少在PHPUnit 3中),并且一些类使用后期静态绑定afaik,所以你应该使用`static :: markTestSkipped('');`而不是`$ this->`.它将在较新的PHP版本中生成警告.签名:`public static function markTestSkipped($ message ='')` (4认同)

zer*_*kms 28

如果你可以处理忽略整个文件然后

<?xml version="1.0" encoding="UTF-8"?>

<phpunit>

    <testsuites>
        <testsuite name="foo">
            <directory>./tests/</directory>
            <exclude>./tests/path/to/excluded/test.php</exclude>
                ^-------------
        </testsuite>
    </testsuites>

</phpunit>
Run Code Online (Sandbox Code Playgroud)

  • @Filype:那么你可能指定了错误的路径.这对我来说很有用.不确定是否可以计算测试数.PS:单元测试不应该运行这么久.我建议使用`@group`注释并按其性质分割测试 (3认同)

Kon*_*ski 16

有时,基于定义为php代码的自定义条件,跳过特定文件中的所有测试是有用的.您可以使用setUp函数轻松完成此操作,其中makeTestSkipped也可以使用.

protected function setUp()
{
    if (your_custom_condition) {
        $this->markTestSkipped('all tests in this file are invactive for this server configuration!');
    }
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句phpunit 5.2稳定的官方文档建议你应该使用markTestSkipped作为实例方法,而不是静态方法.没有检查定义是否在此过程中发生了变化.