在有些情况比如下面这个程序:
MyTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class MyTest extends TestCase
{
public function testOne(){
$this->assertTrue(true);
}
}
这里就一个测试方法,且该测试方法绝对是成功的断言。
但是某些情况下,就比如说是从测试代码覆盖率方向来考虑,我认为该测试方法被覆盖了即@covers 标注时,我才认定它是真正的在我自己心中认定的成功。
有一种说法是代码覆盖率越高越好。那么这类人肯定都是这样认为的。
那么应对这种情况,它就应该测试结果显示为有风险才对,可是你看看它这里的运行结果:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.4.3nts\php.exe C:\Users\Administrator\PhpstormProjects\untitled\vendor\phpunit\phpunit\phpunit -c C:\Users\Administrator\PhpstormProjects\untitled\organizing\phpunit.xml C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:00.012, Memory: 6.00 MB
OK (1 test, 1 assertion)
没有错,它没有报出什么问题。一个完美的断言。
其实这是因为相当于phpunit.xml中的phpunit元素中默认配置forceCoversAnnotation的值为false。
那么如果将forceCoversAnnotation配置为true:
phpunit.xml:
<phpunit bootstrap="src/autoload.php" forceCoversAnnotation="true">
</phpunit>
那么其之运行结果中就会有了风险报告了:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.4.3nts\php.exe C:\Users\Administrator\PhpstormProjects\untitled\vendor\phpunit\phpunit\phpunit -c C:\Users\Administrator\PhpstormProjects\untitled\organizing\phpunit.xml C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
R 1 / 1 (100%)
Time: 00:00.009, Memory: 6.00 MB
There was 1 risky test:
1) MyTest::testOne
This test does not have a @covers annotation but is expected to have one
OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 1, Risky: 1.