Phpunit默认情况下对一个测试方法若没有进行任何断言,则视为无用测试,那么无用测试默认情况下是会抛出错误的。
无用测试错误的抛出
首先这里我们还是基于上文中的组织测试,我们只修改下DatabaseTest.php让其中的一个测试方法变成无用测试,实际上本来也就只有一个测试方法:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class DatabaseTest extends TestCase
{
public function testAdd(): void
{
}
}
那么此时运行测试:
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 --testsuite database
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
.cs user..R 4 / 4 (100%)
Time: 00:00.006, Memory: 20.00 MB
There was 1 risky test:
1) DatabaseTest::testAdd
Warning: include(PHPUnit\Composer\Autoload\ClassLoader.php): failed to open stream: No such file or directory in C:\Users\Administrator\PhpstormProjects\untitled\organizing\src\autoload.php on line 3
Call Stack:
0.0147 1571472 1. {main}() D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar:0
0.1351 18893352 2. PHPUnit\TextUI\Command::main(???) D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar:2249
0.1351 18893464 3. PHPUnit\TextUI\Command->run(array(3), true) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/Command.php:93
0.1421 19322968 4. PHPUnit\TextUI\TestRunner->run(class PHPUnit\Framework\TestSuite, array(16), array(0), true) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/Command.php:124
0.1829 19365096 5. PHPUnit\TextUI\DefaultResultPrinter->printResult(class PHPUnit\Framework\TestResult) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/TestRunner.php:496
0.1837 19365120 6. PHPUnit\TextUI\DefaultResultPrinter->printRisky(class PHPUnit\Framework\TestResult) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:155
0.1837 19365120 7. PHPUnit\TextUI\DefaultResultPrinter->printDefects(array(1), string(10)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:311
0.1842 19365120 8. PHPUnit\TextUI\DefaultResultPrinter->printDefect(class PHPUnit\Framework\TestFailure, long) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:272
0.1851 19365120 9. PHPUnit\TextUI\DefaultResultPrinter->printDefectTrace(class PHPUnit\Framework\TestFailure) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:279
0.1851 19365120 10. PHPUnit\Framework\RiskyTestError->__toString() phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:288
0.1851 19365280 11. PHPUnit\Util\Filter::getFilteredStacktrace(class PHPUnit\Framework\RiskyTestError) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Framework/Exception/Exception.php:58
0.1851 19366416 12. PHPUnit\Util\Filter::shouldPrintFrame(array(2), string(25), class PHPUnit\Util\ExcludeList) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/Filter.php:57
0.1852 19366560 13. PHPUnit\Util\Filter::fileIsExcluded(string(101), class PHPUnit\Util\ExcludeList) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/Filter.php:76
0.1852 19366560 14. PHPUnit\Util\ExcludeList->isExcluded(string(101)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/Filter.php:80
0.1852 19366560 15. PHPUnit\Util\ExcludeList->initialize() phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:166
0.1852 19366560 16. class_exists(string(37)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:182
0.1852 19366624 17. spl_autoload_call(string(37)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:182
0.1852 19366688 18. autoload(string(37)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:182
Warning: include(): Failed opening 'PHPUnit\Composer\Autoload\ClassLoader.php' for inclusion (include_path='.;C:\php\pear') in C:\Users\Administrator\PhpstormProjects\untitled\organizing\src\autoload.php on line 3
Call Stack:
0.0147 1571472 1. {main}() D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar:0
0.1351 18893352 2. PHPUnit\TextUI\Command::main(???) D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar:2249
0.1351 18893464 3. PHPUnit\TextUI\Command->run(array(3), true) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/Command.php:93
0.1421 19322968 4. PHPUnit\TextUI\TestRunner->run(class PHPUnit\Framework\TestSuite, array(16), array(0), true) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/Command.php:124
0.1829 19365096 5. PHPUnit\TextUI\DefaultResultPrinter->printResult(class PHPUnit\Framework\TestResult) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/TestRunner.php:496
0.1837 19365120 6. PHPUnit\TextUI\DefaultResultPrinter->printRisky(class PHPUnit\Framework\TestResult) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:155
0.1837 19365120 7. PHPUnit\TextUI\DefaultResultPrinter->printDefects(array(1), string(10)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:311
0.1842 19365120 8. PHPUnit\TextUI\DefaultResultPrinter->printDefect(class PHPUnit\Framework\TestFailure, long) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:272
0.1851 19365120 9. PHPUnit\TextUI\DefaultResultPrinter->printDefectTrace(class PHPUnit\Framework\TestFailure) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:279
0.1851 19365120 10. PHPUnit\Framework\RiskyTestError->__toString() phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/TextUI/DefaultResultPrinter.php:288
0.1851 19365280 11. PHPUnit\Util\Filter::getFilteredStacktrace(class PHPUnit\Framework\RiskyTestError) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Framework/Exception/Exception.php:58
0.1851 19366416 12. PHPUnit\Util\Filter::shouldPrintFrame(array(2), string(25), class PHPUnit\Util\ExcludeList) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/Filter.php:57
0.1852 19366560 13. PHPUnit\Util\Filter::fileIsExcluded(string(101), class PHPUnit\Util\ExcludeList) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/Filter.php:76
0.1852 19366560 14. PHPUnit\Util\ExcludeList->isExcluded(string(101)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/Filter.php:80
0.1852 19366560 15. PHPUnit\Util\ExcludeList->initialize() phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:166
0.1852 19366560 16. class_exists(string(37)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:182
0.1852 19366624 17. spl_autoload_call(string(37)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:182
0.1852 19366688 18. autoload(string(37)) phar://D:/phpstudy_pro/Extensions/php/php7.3.4nts/phpunit-9.5.8.phar/phpunit/Util/ExcludeList.php:182
This test did not perform any assertions
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\DatabaseTest.php:6
OK, but incomplete, skipped, or risky tests!
Tests: 4, Assertions: 4, Risky: 1.
关闭无用测试检查
那么如果想要phpunit忽略对无用测试的严格检查可以有如下
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 --testsuite database --dont-report-useless-tests
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
.cs user... 4 / 4 (100%)
Time: 00:00.005, Memory: 20.00 MB
OK (4 tests, 4 assertions)
对就像是这样在phpunit命令追加参数--dont-report-useless-tests即可解决
通过phpunit.xml关闭无用测试检查
除了上面可以通过phpunit命令追加参数--dont-report-useless-tests以实现关闭无用测试检查外,另外还可以通过在phpunit.xml配置文件中配置beStrictAboutTestsThatDoNotTestAnything="false" 来禁用
phpunit.xml:
<phpunit bootstrap="src/autoload.php" beStrictAboutTestsThatDoNotTestAnything="false">
<testsuites>
<testsuite name="database">
<file>tests/UserTest.php</file>
<file>tests/DatabaseTest.php</file>
</testsuite>
</testsuites>
</phpunit>
然后再次运行测试,也就不用--dont-report-useless-tests参数达成同样的关闭无用测试检查的效果:
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 --testsuite database
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
.cs user... 4 / 4 (100%)
Time: 00:00.007, Memory: 20.00 MB
OK (4 tests, 4 assertions)