标签归档:测试

phpunit 单元测试中执行类的时候指定某个测试方法

php 开发的时候,我们经常会想要做单元测试,写单元测试的好处是,写完了,就可以经常使用,不用每次测试都打开浏览器,一个页面或者接口不停的测试,让测试变得自动化。

而我常用的是postman 和 phpunit。

在phpunit中,我们常常会把很多的case写在一个类中,这时候,执行测试的时候,所有的case都会执行,执行快的时候还好,要是碰到一些执行慢的,等所有的类都执行下来,时间就会花费很多,所以一般情况我们是希望可以最快的执行我们希望执行的类的,这时候就可以指定执行,从而避开不需要执行的案例。

举个栗子:

<?php
use PHPUnit\Framework\TestCase;

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

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

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

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

像这样的,我们就可以执行命令:

php phpunit  Demo --filter testB

这个时候,只有 testB 被执行了。

phpunit 单元测试中执行类的时候指定某个测试方法

php 开发的时候,我们经常会想要做单元测试,写单元测试的好处是,写完了,就可以经常使用,不用每次测试都打开浏览器,一个页面或者接口不停的测试,让测试变得自动化。

而我常用的是postman 和 phpunit。

在phpunit中,我们常常会把很多的case写在一个类中,这时候,执行测试的时候,所有的case都会执行,执行快的时候还好,要是碰到一些执行慢的,等所有的类都执行下来,时间就会花费很多,所以一般情况我们是希望可以最快的执行我们希望执行的类的,这时候就可以指定执行,从而避开不需要执行的案例。

举个栗子:

<?php
use PHPUnit\Framework\TestCase;
class Unittest_Demo extends TestCase{     public function testA(){         echo "\n testA test\n";         $this->assertTrue(true);
    }

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

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

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

像这样的,我们就可以执行命令:

php phpunit  Demo –filter testB

这个时候,只有 testB 被执行了。