154

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

6
  • 1
    KO: what about fixing the test? btw, hi from welly ;-) Commented Apr 20, 2012 at 2:00
  • 1
    I didn't write the tests, it's something irrelevant, and also don't want to change the core files Commented Apr 20, 2012 at 2:56
  • Doubtfully you can do so. It is a very strange requirement Commented Apr 20, 2012 at 3:01
  • PS: you mentioned filters - but it couldn't help you, because it excludes all the path. Otherwise <exclude> - would do the work for you Commented Apr 20, 2012 at 3:03
  • can you post the proposed phpunit.xml.dist file? Commented Apr 20, 2012 at 3:36

3 Answers 3

296

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.');
Sign up to request clarification or add additional context in comments.

5 Comments

you can always add directories or tests to the xml config file, however, if this is a controller or similar that's really not very practical because you probably have dozens of other tests in that file. i guess if you have no access to unit tests, not sure i understand why, then you have no other choice than to exclude.
As it is a static method (at least in PHPUnit 3), and some classes use late static binding afaik, you should use static::markTestSkipped(''); instead of $this->. It will generate a warning in newer PHP Versions. Signature: public static function markTestSkipped($message = '')
Needs a better example of the full unit test file. Not just a snippet.
@DanielW. the official phpunit manual has examples showing $this->markTestSkipped()
@nulll you can see in the sourcecode of 6.5, markTestSkipped() is still a static method.
46

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>

2 Comments

The tests take about 20min to run, is there an easy way to see how many tests it will run? currently, I have to wait till the first row is completed ....... 63 / 893 ( 7%)
@Filype: then you probably specified the wrong path. It works well for me. Not sure if it is possible to get tests count. PS: unit tests should not run so long. I'd recommend using @group annotation and split tests by their nature
40

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

4 Comments

What is a Config class? Where do I place it?
@cronfy it can be any global scope accessible class (Singleton / Register design pattern) or even global variable set in bootstrap file in phpunit. Basically the logic is: if ($testsFromThisFileShouldBeSkipped) { $this->markTestSkipped(...); }
remove the conditional and your answer would be straight forward... "markTestSkipped inside the setup to skip the whole file"
@SparK I see your point but I was providing way of conditional skipping of whole test class. I've changed it a little, removing my custom way of doing it in favor of general rule.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.