31

What is the attribute that should be placed next to the PHP test method in order to ignore the test using PHPUnit ?

I know that for NUnit the attribute is :

[Test]
[Ignore]
public void IgnoredTest()
localheinz
  • 9,179
  • 2
  • 33
  • 44
Gabriel Diaconescu
  • 1,769
  • 3
  • 21
  • 31

6 Answers6

43

You can tag the test with a group annotation and exclude those tests from a run.

/**
 * @group ignore
 */
public void ignoredTest() {
    ...
}

Then you can run the all the tests but ignored tests like this:

phpunit --exclude-group ignore
Brady Olsen
  • 682
  • 7
  • 6
  • 1
    Note that with the default PHPUnit settings, a method that's doesn't start with `test`, e.g. `testFoo()`, won't be executed. – amphetamachine Jul 06 '22 at 16:03
  • 1
    Note also that `public void ignoredTest()` is a syntax error in all versions of PHP; the correct syntax is (PHP 7+) `public function ignoredTest(): void` or (PHP <7) `public function ignoredTest()`. – amphetamachine Jul 06 '22 at 16:05
23

The easiest way would be to just change the name of the test method and avoid names starting with "test". That way, unless you tell PHPUnit to execute it using @test, it won't execute that test.

Also, you could tell PHPUnit to skip a specific test:

<?php
class ClassTest extends PHPUnit_Framework_TestCase
{     
    public function testThatWontBeExecuted()
    {
        $this->markTestSkipped( 'PHPUnit will skip this test method' );
    }
    public function testThatWillBeExecuted()
    {
        // Test something
    }
}
m13r
  • 2,458
  • 2
  • 29
  • 39
Jose Armesto
  • 12,794
  • 8
  • 51
  • 56
10

You can use the method markTestIncomplete() to ignore a test in PHPUnit:

<?php
require_once 'PHPUnit/Framework.php';

class SampleTest extends PHPUnit_Framework_TestCase
{
    public function testSomething()
    {
        // Optional: Test anything here, if you want.
        $this->assertTrue(TRUE, 'This should already work.');

        // Stop here and mark this test as incomplete.
        $this->markTestIncomplete(
            'This test has not been implemented yet.'
        );
    }
}
?>
m13r
  • 2,458
  • 2
  • 29
  • 39
Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55
  • Thank you for the answer, but I don't want to modify the content of the test. It is just that sometimes I want to ignore some tests, then re-add them in the testing flow. – Gabriel Diaconescu Apr 17 '13 at 13:54
5

Since you suggested in one of your comments that you do not want to change the content of a test, if you are willing to add or adjust annotations, you could abuse the @requires annotation to ignore tests:

<?php

use PHPUnit\Framework\TestCase;

class FooTest extends TestCase
{
    /**
     * @requires PHP 9000
     */
    public function testThatShouldBeSkipped()
    {
        $this->assertFalse(true);
    }
}

Note This will only work until PHP 9000 is released, and the output from running the tests will be a bit misleading, too:

There was 1 skipped test:

1) FooTest::testThatShouldBeSkipped
PHP >= 9000 is required.

For reference, see:

localheinz
  • 9,179
  • 2
  • 33
  • 44
4

If you name your method not with test at the beginning then the method will not be executed by PHPUnit (see here).

public function willBeIgnored() {
    ...
}

public function testWillBeExecuted() {
    ...
}

If you want a method to be executed which does not start with test you can add the annotation @test to execute it anyway.

/**
 * @test
 */
public function willBeExecuted() {
    ...
}
m13r
  • 2,458
  • 2
  • 29
  • 39
3

Expanding on @Brady Olsen's answer..., you can use the @group annotation on your test method or class to put them into an arbitrary group, in this example, ignore:

/**
 * @group ignore
 */
public function testMethod(): void {
    // ignored...
}

And/or:

/**
 * Every test in this class will be ignored, whether it has a @group or not.
 * @group ignore
 */
class FooTest() {
    public function testBar() {
        // ignored...
    }
}

From there there's a couple of ways of excluding the test from being run.

Option 0: Exclude via settings

Exclude the group automatically by changing phpunit.xml to include a <groups> structure immediately under the root <phpunit> node:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <!-- ... -->
    </testsuites>
    <groups>
        <exclude>
            <group>ignore</group>
        </exclude>
    </groups>
    <coverage>
        <!-- ... -->
    </coverage>
</phpunit>

Again, the ignore text is the arbitrary part, but it must match the @group annotation. AFAIK, you can add multiple <group> elements inside the <exclude>.

Also note that <groups> must be immediately under the <phpunit> node, or PHPUnit will scream and die if your phpunit.xml is malformed.

Older versions of PHPUnit use a <filter> tag with different syntax. See this answer for PHPUnit 6.5. For posterity, this XML config works with PHPUnit 9.5.20.

Option 1: Exclude via command-line flags

Exclude the group manually at run time when running phpunit:

phpunit --exclude-group ignore
amphetamachine
  • 27,620
  • 12
  • 60
  • 72