php 如何跳过 PHPunit 中的测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10239264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:43:47  来源:igfitidea点击:

How to skip tests in PHPunit?

phpphpunit

提问by Filype

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

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

回答by jsteinmann

The fastest and easiest way to skip tests that are either broken or you need to continue working on later is to just add the following to the top of your individual unit test:

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

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

回答by zerkms

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>

回答by Konrad Ga??zowski

Sometimes it's useful to skip all tests from particular file based on custom condition(s) defined as php code. You can easily do that using setUp function in which makeTestSkipped works as well.

有时根据定义为 php 代码的自定义条件跳过特定文件中的所有测试很有用。您可以使用 setUp 函数轻松地做到这一点,其中 makeTestSkipped 也可以工作。

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

your_custom_conditioncan be passed via some static class method/property, a constant defined in phpunit bootstrap file or even a global variable.

your_custom_condition可以通过一些静态类方法/属性、在 phpunit 引导文件中定义的常量甚至全局变量传递。