简体   繁体   English

如何跳过 PHPunit 中的测试?

[英]How to skip tests in PHPunit?

I am using phpunit in connection with jenkins, and I want to skip certain tests by setting the configuration in the XML file phpunit.xml我将 phpunit 与 jenkins 结合使用,我想通过在 XML 文件phpunit.xml设置配置来跳过某些测试

I know that I can use on the command line:我知道我可以在命令行上使用:

phpunit --filter testStuffThatBrokeAndIOnlyWantToRunThatOneSingleTest

how do I translate that to the XML file since the <filters> tag is only for code-coverage?由于<filters>标签仅用于代码覆盖,我该如何将其转换为 XML 文件?

I would like to run all tests apart from testStuffThatAlwaysBreaks我想运行除testStuffThatAlwaysBreaks之外的所有测试

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

$this->markTestSkipped('must be revisited.');

If you can deal with ignoring the whole file then如果您可以忽略整个文件,那么

<?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>

Sometimes it's useful to skip all tests from particular file based on custom condition(s) defined as php code.有时根据定义为 php 代码的自定义条件跳过特定文件中的所有测试很有用。 You can easily do that using setUp function in which makeTestSkipped works as well.您可以使用 setUp 函数轻松地做到这一点,其中 makeTestSkipped 也可以工作。

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

your_custom_condition can be passed via some static class method/property, a constant defined in phpunit bootstrap file or even a global variable. your_custom_condition可以通过一些静态类方法/属性、在 phpunit 引导文件中定义的常量甚至全局变量传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM