1. 指定单元测试函数

使用 --filter 参数指定单元测试执行的函数

vendor\bin\phpunit  --filter test_oper

2. 生成html代码覆盖报告

使用 --coverage-html 参数指定生成代码覆盖率的html文件目录

vendor\bin\phpunit --coverage-html  /tmp/log/phpunit

3. 遇到错误时停止

vendor\bin\phpunit --stop-on-error


vendor\bin\phpunit --stop-on-failure

4. 测试前运行启动文件

--bootstrap 参数常常在测试运行前 先加载指定的依赖文件 常常是composer里的autoload.php 文件

vendor\bin\phpunit --bootstrap /vendor/autoload.php

5. 指定测试配置文件

--configuration 或者 -c 参数指定对应的 phpunit.xml文件的路径

6. 输出调试信息

vendor\bin\phpunit --debug 

7. 添加PHP.ini配置

vendor\bin\phpunit -d key[=value]

8. 批量测试

使用 @dataProvider在测试函数的注释中指明测试的数据提供函数 并依次接收所有参数,每组参数的

class CalcControllerTest extends TestCase
    private $calc;
    public function setUp()
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->calc = new \App\Http\Controllers\CalcController();
	 * @dataProvider operPrider
	public function testOper($a,$b,$c,$expected){

	public function operPrider(){
	    return [


8. 常用注释测试参数

@covers ClassName::methodName指明所标注的测试方法覆盖指定的方法。
@covers ClassName指明所标注的测试方法覆盖给定类的全部方法。
@covers ClassName<extended>指明所标注的测试方法覆盖给定类以及其所有父类与接口的全部方法。
@covers ClassName::<public>指明所标注的测试方法覆盖给定类的所有 public 方法。
@covers ClassName::<protected>指明所标注的测试方法覆盖给定类的所有 protected 方法。
@covers ClassName::<private>指明所标注的测试方法覆盖给定类的所有 private 方法。
@covers ClassName::<!public>指明所标注的测试方法覆盖给定类的所有非 public 方法。
@covers ClassName::<!protected>指明所标注的测试方法覆盖给定类的所有非 protected 方法。
@covers ClassName::<!private>指明所标注的测试方法覆盖给定类的所有非 private 方法。
@covers ::functionName指明所标注的测试方法覆盖给定的全局函数。
@large测试超过 60s会被标记为失败
@medium测试超过 10s会被标记为失败
@small测试超过 1s会被标记为失败
@test标记某个非 test 开头的函数能够被当做测试函数进行测试
<div class="post-text" itemprop="text"> <p>I have a unit test class in which I want to instantiate a object from another class in order to that I used setUpBeforeClass() fixtures of phpunit. So if I will use that recently instantiated object directly in test function then its working fine. </p> <p>If i'll use this object into another function which had been created for data providers. So that object sets to null cause providers always execute first.</p> <p>Is there a way to call dataProviders just before the test runs, instead?</p> <pre><code>require_once('Dashboard.php'); Class Someclass extends PHPUnit_Framework_TestCase { protected static $_dashboard; public static function setUpBeforeClass() { self::$_dashboard = new Dashboard(); self::$_dashboard->set_class_type('Member'); } /** * Test Org Thumb Image Existense * param org profile image : array * @dataProvider getOrgProfileImages */ public function testFieldValidation($a,$b){ //If I call that object function here it will give the result. //$members = self::$_dashboard->get_members(); //var_dump($members); Printing result as expected $this->assertTrue(true); } public function getOrgProfileImages() : array { //var_dump(self::$_dashboard); $members = self::$_dashboard->get_members(); $tmp_array = ['2','2']; return $tmp_array; } public static function tearDownAfterClass() { self::$_dashboard = null; } } </code></pre> <p><strong>Error:</strong> The data provider specified for Someclass::testFieldValidation is invalid. Call to a member function get_members() on null</p> <p>Please help to mitigate this issue. </p> </div>
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页