140

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

Ben Creasy
  • 3,825
  • 4
  • 40
  • 50
filype
  • 8,034
  • 10
  • 40
  • 66

3 Answers3

267

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.');
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
jsteinmann
  • 4,502
  • 3
  • 17
  • 21
  • 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. – jsteinmann Nov 29 '12 at 00:39
  • 7
    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 = '')` – Daniel W. Jul 20 '15 at 14:11
  • Needs a better example of the full unit test file. Not just a snippet. – akahunahi Jun 05 '17 at 21:57
  • @DanielW. the [official phpunit manual](https://phpunit.de/manual/6.5/en/incomplete-and-skipped-tests.html#incomplete-and-skipped-tests.skipping-tests) has examples showing `$this->markTestSkipped()` – nulll Oct 01 '20 at 09:47
  • @nulll you can see [in the sourcecode of 6.5](https://github.com/sebastianbergmann/phpunit/blob/9535c8ca4e0c096f22e53bade686884f3d0b6107/src/Framework/Assert.php), `markTestSkipped()` is still a static method. – Daniel W. Oct 01 '20 at 13:20
41

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>
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 1
    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 Apr 20 '12 at 03:58
  • 5
    @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 – zerkms Apr 20 '12 at 04:19
35

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

Konrad Gałęzowski
  • 1,761
  • 1
  • 20
  • 29
  • What is a ```Config``` class? Where do I place it? – cronfy Oct 05 '16 at 09:28
  • @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(...); } – Konrad Gałęzowski Oct 07 '16 at 11:41
  • remove the conditional and your answer would be straight forward... "markTestSkipped inside the setup to skip the whole file" – SparK Jun 12 '19 at 21:49
  • 1
    @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. – Konrad Gałęzowski Jun 14 '19 at 08:43