How can I ignore a test method in phpunit without modifying its content?

前端 未结 5 1624
礼貌的吻别
礼貌的吻别 2021-02-06 23:43

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 :



        
相关标签:
5条回答
  • 2021-02-07 00:19

    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
    
    0 讨论(0)
  • 2021-02-07 00:19

    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.'
            );
        }
    }
    ?>
    
    0 讨论(0)
  • 2021-02-07 00:22

    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:

    • https://phpunit.de/manual/current/en/incomplete-and-skipped-tests.html#incomplete-and-skipped-tests.skipping-tests-using-requires
    0 讨论(0)
  • 2021-02-07 00:23

    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() {
        ...
    }
    
    0 讨论(0)
  • 2021-02-07 00:25

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题