如何从xml config中的PHPUnit测试套件中排除文件?

时间:2022-03-02 13:56:20

I have following, very simple, XML config for PHPUnit:

我有以下非常简单的PHPUnit XML配置:

<phpunit bootstrap="/_tests/TestAutoload.php">
    <testsuites>
        <testsuite name="Unit Tests">
            <directory suffix=".php">_tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

How to exclude certain file in this directory from test suite? I tried <exclude> and <blacklist>, but it doesn't seem to work in this context. Also couldn't find any other documentation than phpunit.de one, which doesn't mention anything about it. Else than that, this config works perfectly.

如何从测试套件中排除此目录中的某些文件?我尝试了 ,但它似乎不适用于此上下文。还找不到除phpunit.de之外的任何其他文档,其中没有提及任何相关内容。除此之外,此配置完美无缺。

5 个解决方案

#1


30  

There are a number of ways to not run a particular test - putting it into a blacklist so it's never run may not be the way - as changing it means editing the blacklist, and you'll often endup bouncing it in and out of version control.

有许多方法可以不运行特定测试 - 将其放入黑名单,因此它永远不会运行 - 因为更改它意味着编辑黑名单,并且您通常会最终将其反弹并进出版本控制。

There are several other ways that may be more appropriate:

还有其他几种可能更合适的方法:

If a test is not yet ready to run:

如果测试尚未准备好运行:

$this->markTestIncomplete('This test has not been implemented yet.');

If there's an outside reason it should not be run, skip it:

如果有外部原因不应该运行,请跳过它:

if (!extension_loaded('mysqli')) {
    $this->markTestSkipped('The MySQLi extension is not available.');
}

You can also put that into the setUp() function, so it will skip all the tests in a test-class.

您也可以将它放入setUp()函数中,因此它将跳过测试类中的所有测试。

You can make a test dependant on a previous one succeeding:

您可以根据前一个成功进行测试:

public function testEmpty()
{
    $stack = array();
    $this->assertTrue(empty($stack));
    return $stack;   // also sends this variable to any following tests - if this worked
}
/**
 * only runs if testEmpty() passed
 *
 * @depends testEmpty
 */
public function testPush(array $stack)
{
}

The @group -name- annotation is one of the best ways to specifically stop, or run one group of tests

@group -name-注释是专门停止或运行一组测试的最佳方法之一

/**
 * @group database
 * @group remoteTasks
 */
public function testSomething()
{
}

testSomething() is now in two groups, and if either is added on the command line (or in the config.xml) --exclude-group parameter. it won't be run. Likewise, you could run only tests that belong to a particular group - say named after a feature, or bug report.

testSomething()现在分为两组,如果在命令行(或config.xml)中添加了任一组--exclude-group参数。它不会被运行。同样,您只能运行属于特定组的测试 - 例如以功能或错误报告命名。

#2


20  

To exclude the file name TestCase.php.

要排除文件名TestCase.php。

add this to your phpunit.xml

将此添加到您的phpunit.xml

<testsuites>
    <testsuite name="BLABLA">
        <directory suffix=".php">./tests</directory>
        <exclude>./tests/TestCase.php</exclude>
    </testsuite>
</testsuites>

Here is an additional excerpt from a real-live test-suite I can confirm it working with:

这是一个真实的测试套件的额外摘录,我可以确认它与:

...
    <testsuites>
        <testsuite name="n98-magerun-tests">
            <directory>./tests</directory>
            <exclude>tests/N98/Magento/Command/Installer/UninstallCommandTest.php</exclude>
        </testsuite>
    ...

#3


7  

Whit this PHPUnit configuration-file I have made very good experiences.

Whit这个PHPUnit配置文件我已经取得了非常好的经验。

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    colors="true"
    processIsolation="true"
    stopOnFailure="true"
    syntaxCheck="false"
    backupGlobals="false"
    bootstrap="test-bootstrap.php">
    <testsuites>
        <testsuite name="php-dba-cache">
          <directory suffix="Test.php">tests</directory>
        </testsuite>
    </testsuites>
    <logging>
        <log type="coverage-html"
             target="build/coverage"
             charset="UTF-8"
             yui="true"
             highlight="true"
             lowUpperBound="35"
             highLowerBound="70"/>
   </logging>
    <filter>
        <whitelist addUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">src</directory>
            <exclude>
             <file>test-bootstrap.php</file>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

https://github.com/gjerokrsteski/php-dba-cache

https://github.com/gjerokrsteski/php-dba-cache

#4


4  

The phpunit documentation is a bit minimalistic when it comes to exclusion in a testsuite. Apparently, only entire directories can be excluded but not individual files. I would be very happy to be proven wrong. The workaround seems to be using the @group feature as posted above by Alister Bulman.

当涉及到在测试套件中排除时,phpunit文档有点简约。显然,只能排除整个目录,但不能排除单个文件。我很高兴被证明是错的。解决方法似乎是使用Alister Bulman上面发布的@group功能。

It's kind of a pain needing to tag every single test in those test suites I'd like to keep.

在我想保留的那些测试套件中标记每一个测试都是一种痛苦。

#5


-4  

Hey there, Make sure that you put your exclusions in the Whitelist. Example:

嘿那里,确保你把你的排除列入白名单。例:

<phpunit>
    <filter>
        <blacklist>
            <directory suffix=".php">/not/even/looked/at/</directory>
        </blacklist>
        <whitelist>
            <directory suffix=".php">/path/to/test/dir/</directory>
            <exclude>
                <file suffix=".php">/path/to/fileToExclude.php</file>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

http://www.phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.blacklist-whitelist

http://www.phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.blacklist-whitelist

#1


30  

There are a number of ways to not run a particular test - putting it into a blacklist so it's never run may not be the way - as changing it means editing the blacklist, and you'll often endup bouncing it in and out of version control.

有许多方法可以不运行特定测试 - 将其放入黑名单,因此它永远不会运行 - 因为更改它意味着编辑黑名单,并且您通常会最终将其反弹并进出版本控制。

There are several other ways that may be more appropriate:

还有其他几种可能更合适的方法:

If a test is not yet ready to run:

如果测试尚未准备好运行:

$this->markTestIncomplete('This test has not been implemented yet.');

If there's an outside reason it should not be run, skip it:

如果有外部原因不应该运行,请跳过它:

if (!extension_loaded('mysqli')) {
    $this->markTestSkipped('The MySQLi extension is not available.');
}

You can also put that into the setUp() function, so it will skip all the tests in a test-class.

您也可以将它放入setUp()函数中,因此它将跳过测试类中的所有测试。

You can make a test dependant on a previous one succeeding:

您可以根据前一个成功进行测试:

public function testEmpty()
{
    $stack = array();
    $this->assertTrue(empty($stack));
    return $stack;   // also sends this variable to any following tests - if this worked
}
/**
 * only runs if testEmpty() passed
 *
 * @depends testEmpty
 */
public function testPush(array $stack)
{
}

The @group -name- annotation is one of the best ways to specifically stop, or run one group of tests

@group -name-注释是专门停止或运行一组测试的最佳方法之一

/**
 * @group database
 * @group remoteTasks
 */
public function testSomething()
{
}

testSomething() is now in two groups, and if either is added on the command line (or in the config.xml) --exclude-group parameter. it won't be run. Likewise, you could run only tests that belong to a particular group - say named after a feature, or bug report.

testSomething()现在分为两组,如果在命令行(或config.xml)中添加了任一组--exclude-group参数。它不会被运行。同样,您只能运行属于特定组的测试 - 例如以功能或错误报告命名。

#2


20  

To exclude the file name TestCase.php.

要排除文件名TestCase.php。

add this to your phpunit.xml

将此添加到您的phpunit.xml

<testsuites>
    <testsuite name="BLABLA">
        <directory suffix=".php">./tests</directory>
        <exclude>./tests/TestCase.php</exclude>
    </testsuite>
</testsuites>

Here is an additional excerpt from a real-live test-suite I can confirm it working with:

这是一个真实的测试套件的额外摘录,我可以确认它与:

...
    <testsuites>
        <testsuite name="n98-magerun-tests">
            <directory>./tests</directory>
            <exclude>tests/N98/Magento/Command/Installer/UninstallCommandTest.php</exclude>
        </testsuite>
    ...

#3


7  

Whit this PHPUnit configuration-file I have made very good experiences.

Whit这个PHPUnit配置文件我已经取得了非常好的经验。

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    colors="true"
    processIsolation="true"
    stopOnFailure="true"
    syntaxCheck="false"
    backupGlobals="false"
    bootstrap="test-bootstrap.php">
    <testsuites>
        <testsuite name="php-dba-cache">
          <directory suffix="Test.php">tests</directory>
        </testsuite>
    </testsuites>
    <logging>
        <log type="coverage-html"
             target="build/coverage"
             charset="UTF-8"
             yui="true"
             highlight="true"
             lowUpperBound="35"
             highLowerBound="70"/>
   </logging>
    <filter>
        <whitelist addUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">src</directory>
            <exclude>
             <file>test-bootstrap.php</file>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

https://github.com/gjerokrsteski/php-dba-cache

https://github.com/gjerokrsteski/php-dba-cache

#4


4  

The phpunit documentation is a bit minimalistic when it comes to exclusion in a testsuite. Apparently, only entire directories can be excluded but not individual files. I would be very happy to be proven wrong. The workaround seems to be using the @group feature as posted above by Alister Bulman.

当涉及到在测试套件中排除时,phpunit文档有点简约。显然,只能排除整个目录,但不能排除单个文件。我很高兴被证明是错的。解决方法似乎是使用Alister Bulman上面发布的@group功能。

It's kind of a pain needing to tag every single test in those test suites I'd like to keep.

在我想保留的那些测试套件中标记每一个测试都是一种痛苦。

#5


-4  

Hey there, Make sure that you put your exclusions in the Whitelist. Example:

嘿那里,确保你把你的排除列入白名单。例:

<phpunit>
    <filter>
        <blacklist>
            <directory suffix=".php">/not/even/looked/at/</directory>
        </blacklist>
        <whitelist>
            <directory suffix=".php">/path/to/test/dir/</directory>
            <exclude>
                <file suffix=".php">/path/to/fileToExclude.php</file>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

http://www.phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.blacklist-whitelist

http://www.phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.blacklist-whitelist