您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit指明了要覆盖的类的测试类covers与uses标注
发布时间:2021-09-25 16:33:39编辑:雪饮阅读()
用 @covers 标注指定了一个方法,那么只有所指方法会被视为已覆盖,这个方法所调用的方法不会视为已覆盖。
而@uses 标注用来指明那些将会在测试中执行到但同时又不打算让其被测试所覆盖(我认为这里的覆盖应该是指覆盖率)的代码。
那么实例:
InvoiceTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* @covers \Invoice
* @uses \Money
*/
final class InvoiceTest extends TestCase
{
private $invoice;
protected function setUp(): void
{
$this->invoice = new Invoice;
}
public function testAmountInitiallyIsEmpty(): void
{
$this->assertEquals(new Money, $this->invoice->getAmount());
}
}
use PHPUnit\Framework\TestCase;
/**
* @covers \Invoice
* @uses \Money
*/
final class InvoiceTest extends TestCase
{
private $invoice;
protected function setUp(): void
{
$this->invoice = new Invoice;
}
public function testAmountInitiallyIsEmpty(): void
{
$this->assertEquals(new Money, $this->invoice->getAmount());
}
}
执行效果如:
C:\Users\Administrator\PhpstormProjects\untitled\organizing>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar --coverage-html 1.html --testsuite Database --filter 'InvoiceTest::testAmountInitiallyIsEmpty'
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
Warning: No filter is configured, code coverage will not be processed
. 1 / 1 (100%)
Time: 00:00.009, Memory: 22.00 MB
OK (1 test, 1 assertion)
这里竟然警告未配置筛选器,就不生成代码覆盖率报告。但是我有filter,暂时不晓得是为什么。
这里所依赖的Invoice.php可以自己胡诌一个就行了:
但是安装上面testAmountInitiallyIsEmpty方法的断言,则最好有getAmount方法,并且返回类型为Money类。
class Invoice{
public function getAmount(){
return new Money();
}
}
Money类则是真的可以胡诌了Money.php:
class Money{
}
关键字词:phpunit,covers,uses