phpunit中執行指定測試case的方法

一. 問題

一個測試文件中,可能包含多個case,如何只執行其中的某個或某幾個case呢?php

好比下面的這段測試代碼(demotest.php),是否能夠只執行鍼對FuncA的兩個測試~testFuncA_1,testFuncA_2呢?mysql

<?php
use PHPUnit\Framework\TestCase;

class Unittest_Demo extends TestCase{
    public function testFuncA_1(){
        echo "\nFuncA1 test\n";
        $this->assertTrue(true);
    }
    
    public function testFuncA_2(){
        echo "\nFuncA2 test\n";
        $this->assertTrue(true);
    }

    public function testFuncB_1(){
        echo "\nFuncB1 test\n";
        $this->assertTrue(true);
    }

    public function testFuncB_2(){
        echo "\nFuncB2 test\n";
        $this->assertTrue(true);
    }
}

二. 解決

2.1 方法一 @group

能夠用 @group 標註來標記某個case屬於一個或多個組,就像這樣:sql

class MyTest extends PHPUnit_Framework_TestCase{
    /**
     * @group specification
     */
    public function testSomething(){
    }

    /**
     * @group regresssion
     * @group bug2204
     */
    public function testSomethingElse(){
    }
}

測試能夠基於組來選擇性的執行,使用命令行phpunit的 --group選項+組名,能夠執行對應測試組的測試。測試

對於1中的問題,咱們能夠作以下標註:this

class Unittest_Demo extends TestCase{
    /** 
     *@group FuncA
     * */
    public function testFuncA_1(){
        ... ...
    }
    /** 
     *@group FuncA
     * */
    public function testFuncA_2(){
         ... ...
    }
    
    ...

執行spa

phpunit test.php --group FuncA

獲得結果.net

PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

.
FuncA1 test
.                                                              2 / 2 (100%)
FuncA2 test


Time: 88 ms, Memory: 8.00MB

OK (2 tests, 2 assertions)

可使用--list-group選項,查看文件中存在的group。
好比針對上例,咱們執行的效果以下:命令行

phpunit test.php --list-group
PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

Available test group(s):
 - FuncA
 - default

default分組就是未特別標識的case(testFuncB_1,testFuncB_2)。有須要,你可使用以下命令執行這些case。code

phpunit test.php --group default

特別注意

@group是以註釋的形式存在,註釋的第一行必須是/**,不然phpunit將不識別。ci

2.2 方法二 --filter

命令行的phpunit支持以下選項:

--filter <pattern>

能夠用於篩選知足條件的用例。

對於1中的問題,咱們能夠執行經過以下命令達到目的。

phpunit test.php --filter FuncA

說明

  • pattern部分相似於mysql的like,即%FuncA%。所以命中了名爲testFuncA_1,testFuncA_2的兩個case。

做者:跑馬溜溜的球 連接:https://www.jianshu.com/p/fc85d763cbb8 來源:簡書 簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。

相關文章
相關標籤/搜索