您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit如何生成代码覆盖率?“No filter is configured, code coverage will not be processed”问题
发布时间:2021-10-21 22:07:12编辑:雪饮阅读()
php的代码覆盖率生成有点麻烦。
大多数新手按照网上的一贯教程都会出现有驱动问题,当然这个一般就是按照官网的文档安装相关扩展即可。
那么还有一类错误官网没有直接明说,就是下面这个错误:
No filter is configured, code coverage will not be processed
那么这里从头到尾谈谈这个代码覆盖率如何生成。
首先运行命令如:
D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe -r "phpinfo();" | findstr "bug" >>1.txt & D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe -m >>1.txt & type 1.txt
输出结果很多,需要注意xdebug必须是2.7.0或以上
对于扩展类一般至少要有下面这几个
上面这些扩展都有了接下来如果去尝试代码覆盖率生成就基本不会报错与驱动有关的了,而是:
No filter is configured, code coverage will not be processed
此时通过配置生成向导命令:
D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\WWW\phpunitLearning\phpunit-9.5.10.phar --generate-configuration
生成一个phpunit.xml的一个配置文件,默认会生成在当前cmd中你的当前目录中。
然后拷贝这个配置文件到你想要的地方中,然后根据自己的情况修改下里面的比如默认的引导bootstrap之类的。
这里以我这个修改好的phpunit.xml为例:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="src/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
src目录中的autoload.php:
<?php
function autoload($class){
include $class.'.php';
}
spl_autoload_register('autoload');
以及src下面一个空(内容为空)的default.php。
然后tests目录中的JavaTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class JavaTest extends TestCase
{
public function testOne(): void
{
echo "\r\n".__METHOD__."\r\n";
$this->assertTrue(true);
}
public function testTwo(): void
{
echo "\r\n".__METHOD__."\r\n";
$this->assertTrue(true);
}
public function testThree(): void
{
echo "\r\n".__METHOD__."\r\n";
$this->assertTrue(true);
}
}
以及test目录中的MyTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class MyTest extends TestCase
{
public function testOne(): void
{
echo "\r\n".__METHOD__."\r\n";
$this->assertTrue(true);
}
public function testTwo(): void
{
echo "\r\n".__METHOD__."\r\n";
$this->assertTrue(true);
}
public function testThree(): void
{
echo "\r\n".__METHOD__."\r\n";
$this->assertTrue(true);
}
}
那么接下来就可以运行代码覆盖率生成了:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\WWW\phpunitLearning\phpunit-9.5.10.phar -c D:\phpstudy_pro\WWW\phpunitLearning\phpunit.xml --coverage-html=D:\phpstudy_pro\WWW\phpunitLearning\report\
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.4 with PCOV 1.0.9
Configuration: D:\phpstudy_pro\WWW\phpunitLearning\phpunit.xml
RR
JavaTest::testOne
RR
JavaTest::testTwo
RR 6 / 6 (100%)
JavaTest::testThree
RR
MyTest::testOne
RR
MyTest::testTwo
RR
MyTest::testThree
Time: 00:00.008, Memory: 22.00 MB
There were 12 risky tests:
1) JavaTest::testOne
Warning: include(PHPUnit\Composer\Autoload\ClassLoader.php): failed to open stream: No such file or directory in D:\phpstudy_pro\WWW\phpunitLearning\src\autoload.php on line 3
Call Stack:
0.0200 1651120 1. {main}() D:\phpstudy_pro\WWW\phpunitLearning\phpunit-9.5.10.phar:0
0.1376 19809920 2. PHPUnit\TextUI\Command::main(???) D:\phpstudy_pro\WWW\phpunitLearning\phpunit-9.5.10.phar:2281
0.1376 19810032 3. PHPUnit\TextUI\Command->run(array(4), true) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/Command.php:93
0.1446 20172432 4. PHPUnit\TextUI\TestRunner->run(class PHPUnit\Framework\TestSuite, array(16), array(0), true) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/Command.php:124
0.1922 20391248 5. PHPUnit\TextUI\DefaultResultPrinter->printResult(class PHPUnit\Framework\TestResult) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/TestRunner.php:496
0.1925 20391272 6. PHPUnit\TextUI\DefaultResultPrinter->printRisky(class PHPUnit\Framework\TestResult) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/DefaultResultPrinter.php:155
0.1925 20391272 7. PHPUnit\TextUI\DefaultResultPrinter->printDefects(array(12), string(10)) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/DefaultResultPrinter.php:311
0.1930 20391272 8. PHPUnit\TextUI\DefaultResultPrinter->printDefect(class PHPUnit\Framework\TestFailure, long) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/DefaultResultPrinter.php:272
0.1935 20391272 9. PHPUnit\TextUI\DefaultResultPrinter->printDefectTrace(class PHPUnit\Framework\TestFailure) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/DefaultResultPrinter.php:279
0.1935 20391272 10. PHPUnit\Framework\MissingCoversAnnotationException->__toString() phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/DefaultResultPrinter.php:288
0.1935 20391384 11. PHPUnit\Util\Filter::getFilteredStacktrace(class PHPUnit\Framework\MissingCoversAnnotationException) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Framework/Exception/Exception.php:58
0.1935 20392520 12. PHPUnit\Util\Filter::shouldPrintFrame(array(2), string(26), class PHPUnit\Util\ExcludeList) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/Filter.php:57
0.1935 20392640 13. PHPUnit\Util\Filter::fileIsExcluded(string(95), class PHPUnit\Util\ExcludeList) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/Filter.php:76
0.1935 20392640 14. PHPUnit\Util\ExcludeList->isExcluded(string(95)) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/Filter.php:80
0.1935 20392640 15. PHPUnit\Util\ExcludeList->initialize() phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/ExcludeList.php:166
0.1935 20392640 16. class_exists(string(37)) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/ExcludeList.php:182
0.1935 20392704 17. spl_autoload_call(string(37)) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/ExcludeList.php:182
0.1936 20392768 18. autoload(string(37)) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/ExcludeList.php:182
Warning: include(): Failed opening 'PHPUnit\Composer\Autoload\ClassLoader.php' for inclusion (include_path='.;C:\php\pear') in D:\phpstudy_pro\WWW\phpunitLearning\src\autoload.php on line 3
Call Stack:
0.0200 1651120 1. {main}() D:\phpstudy_pro\WWW\phpunitLearning\phpunit-9.5.10.phar:0
0.1376 19809920 2. PHPUnit\TextUI\Command::main(???) D:\phpstudy_pro\WWW\phpunitLearning\phpunit-9.5.10.phar:2281
0.1376 19810032 3. PHPUnit\TextUI\Command->run(array(4), true) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/Command.php:93
0.1446 20172432 4. PHPUnit\TextUI\TestRunner->run(class PHPUnit\Framework\TestSuite, array(16), array(0), true) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/Command.php:124
0.1922 20391248 5. PHPUnit\TextUI\DefaultResultPrinter->printResult(class PHPUnit\Framework\TestResult) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/TestRunner.php:496
0.1925 20391272 6. PHPUnit\TextUI\DefaultResultPrinter->printRisky(class PHPUnit\Framework\TestResult) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/DefaultResultPrinter.php:155
0.1925 20391272 7. PHPUnit\TextUI\DefaultResultPrinter->printDefects(array(12), string(10)) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/DefaultResultPrinter.php:311
0.1930 20391272 8. PHPUnit\TextUI\DefaultResultPrinter->printDefect(class PHPUnit\Framework\TestFailure, long) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/DefaultResultPrinter.php:272
0.1935 20391272 9. PHPUnit\TextUI\DefaultResultPrinter->printDefectTrace(class PHPUnit\Framework\TestFailure) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/DefaultResultPrinter.php:279
0.1935 20391272 10. PHPUnit\Framework\MissingCoversAnnotationException->__toString() phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/TextUI/DefaultResultPrinter.php:288
0.1935 20391384 11. PHPUnit\Util\Filter::getFilteredStacktrace(class PHPUnit\Framework\MissingCoversAnnotationException) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Framework/Exception/Exception.php:58
0.1935 20392520 12. PHPUnit\Util\Filter::shouldPrintFrame(array(2), string(26), class PHPUnit\Util\ExcludeList) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/Filter.php:57
0.1935 20392640 13. PHPUnit\Util\Filter::fileIsExcluded(string(95), class PHPUnit\Util\ExcludeList) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/Filter.php:76
0.1935 20392640 14. PHPUnit\Util\ExcludeList->isExcluded(string(95)) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/Filter.php:80
0.1935 20392640 15. PHPUnit\Util\ExcludeList->initialize() phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/ExcludeList.php:166
0.1935 20392640 16. class_exists(string(37)) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/ExcludeList.php:182
0.1935 20392704 17. spl_autoload_call(string(37)) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/ExcludeList.php:182
0.1936 20392768 18. autoload(string(37)) phar://D:/phpstudy_pro/WWW/phpunitLearning/phpunit-9.5.10.phar/phpunit/Util/ExcludeList.php:182
This test does not have a @covers annotation but is expected to have one
2) JavaTest::testOne
This test printed output:
JavaTest::testOne
3) JavaTest::testTwo
This test does not have a @covers annotation but is expected to have one
4) JavaTest::testTwo
This test printed output:
JavaTest::testTwo
5) JavaTest::testThree
This test does not have a @covers annotation but is expected to have one
6) JavaTest::testThree
This test printed output:
JavaTest::testThree
7) MyTest::testOne
This test does not have a @covers annotation but is expected to have one
8) MyTest::testOne
This test printed output:
MyTest::testOne
9) MyTest::testTwo
This test does not have a @covers annotation but is expected to have one
10) MyTest::testTwo
This test printed output:
MyTest::testTwo
11) MyTest::testThree
This test does not have a @covers annotation but is expected to have one
12) MyTest::testThree
This test printed output:
MyTest::testThree
OK, but incomplete, skipped, or risky tests!
Tests: 6, Assertions: 6, Risky: 12.
Generating code coverage report in HTML format ... done [00:00.009]
这里虽然报了些错误,因为当初没有用composer安装phpunit,而是直接下载的phar包。如果是composer应该会好点的。这里暂不讨论。
那么生成之后的报告存放目录在我们命令所指定的report目录中
是一个web前端小项目的形式,以浏览器打开其index.html
因为这里命令生成我们指定的是html,所以就是生成了web前端的html格式代码覆盖报告率了。
关键字词:phpunit,生成,代码覆盖率,No filter is configured, code coverage will not be processed
上一篇:php实现图片缩放(png)实现二维码尺寸随二维码内容而变动
下一篇:phpunit-phpunit.xml-phpunit-testsuites-testsuite-directory-prefix
相关文章
- phpunit-phpunit.xml-testsuites、testsuite
- phpunit-phpunit.xml-phpunit-testdox
- phpunit-phpunit.xml-phpunit-executionOrder
- phpunit-phpunit.xml-phpunit-stderr
- phpunit-phpunit.xml-phpunit-verbose
- phpunit-phpunit.xml-phpunit-defaultTestSuite
- phpunit-phpunit.xml-phpunit-testSuiteLoaderClass与
- phpunit-phpunit.xml-phpunit-timeoutForLargeTests
- phpunit-phpunit.xml-phpunit-timeoutForMediumTests
- phpunit-phpunit.xml-phpunit-timeoutForSmallTests