您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit单文件测试的更细粒度filter
发布时间:2021-09-20 17:02:03编辑:雪饮阅读()
像是上篇,可以实现在组织测试中对单文件单类进行测试,但是有时候就是一个单类中也有很多测试方法。
像是之前的UserTest.php修改为:
<?php
use PHPUnit\Framework\TestCase;
class UserTest extends TestCase
{
protected $user;
public function setUp():void
{
$this->user = new User('');
}
public function testIsempty()
{
$this->user->name='mark';
$result =$this->user->Isempty();
//原型:assertEquals(mixed $expected, mixed $actual[, string $message = ''])
//当两个变量 $expected 和 $actual 不相等时报告错误,错误讯息由 $message 指定。
$this->assertEquals('welcome mark',$result);
$this->user->name='';
$results =$this->user->Isempty();
$this->assertEquals('its null',$results);
}
public function testAdd1()
{
$this->assertTrue(true);
}
public function testAdd2()
{
$this->assertTrue(true);
}
}
use PHPUnit\Framework\TestCase;
class UserTest extends TestCase
{
protected $user;
public function setUp():void
{
$this->user = new User('');
}
public function testIsempty()
{
$this->user->name='mark';
$result =$this->user->Isempty();
//原型:assertEquals(mixed $expected, mixed $actual[, string $message = ''])
//当两个变量 $expected 和 $actual 不相等时报告错误,错误讯息由 $message 指定。
$this->assertEquals('welcome mark',$result);
$this->user->name='';
$results =$this->user->Isempty();
$this->assertEquals('its null',$results);
}
public function testAdd1()
{
$this->assertTrue(true);
}
public function testAdd2()
{
$this->assertTrue(true);
}
}
这样就算是单类测试也会有多个tests
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar --configuration C:\Users\Administrator\PhpstormProjects\untitled\organizing\phpunit.xml C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\UserTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 00:00.005, Memory: 20.00 MB
OK (3 tests, 4 assertions)
可以看到这里有3个tests,那么如果这里只想测试刚才新增的这两个测试方法,而这两个测试方法仅仅是方法名后缀的数字不同,则可以利用filter进行匹配。
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar --configuration C:\Users\Administrator\PhpstormProjects\untitled\organizing\phpunit.xml --filter testAdd C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\UserTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 00:00.005, Memory: 20.00 MB
OK (2 tests, 2 assertions)
这样就可以看到只有两个tests了,这样就实现了phpunit组织测试单文件更细粒度的测试了。
关键字词:phpunit,单文件,更细粒度,filter