filype
filype

Reputation: 8400

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

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?

I would like to run all tests apart from testStuffThatAlwaysBreaks

Upvotes: 149

Views: 97977

Answers (3)

Konrad Gałęzowski
Konrad Gałęzowski

Reputation: 1993

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.

protected function setUp()
{
    parent::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.

Note: Don't forget calling parent::setUp() (see docs).

Upvotes: 39

jsteinmann
jsteinmann

Reputation: 4782

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.');

Upvotes: 289

zerkms
zerkms

Reputation: 255115

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>

Upvotes: 44

Related Questions