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()
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()
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
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
}
}
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.'
);
}
}
?>
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:
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() {
...
}
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.
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.
Exclude the group manually at run time when running phpunit:
phpunit --exclude-group ignore