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