报错信息1:php
Error:No code coverage driver is available
问题和解决:html
# 没有成功安装xdebug brew search php70-xdebug brew install php70-xdebug brew services restart php70
# 查看php -v 若是信息以下则安装成功 PHP 7.0.25 (cli) (built: Oct 27 2017 12:56:53) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans
报错信息2:bootstrap
Error: No whitelist configured, no code coverage will be generated
问题和解决:php7
# 由于我须要测试覆盖率,而这里没有设置白名单,能够在项目目录下增长 phpunit.xml,xml中增长下面这写代码, 能够增长多个目录。 <filter> <whitelist processUncoveredFilesFromWhitelist="true"> <directory suffix=".php">./Api1</directory> </whitelist> <whitelist processUncoveredFilesFromWhitelist="true"> <directory suffix=".php">./Api2</directory> </whitelist> </filter>
报错信息3:app
. 1 / 1 (100%) Time: 340 ms, Memory: 10.00MB OK (1 test, 0 assertions)
问题和解决:单元测试
# 测试其实已经经过了,但 0 assertions,表明没有任何断言被执行。 增长(或修改) processIsolation="false" 这行到 phpunit.xml 的 <phpunit >中 <phpunit bootstrap="./tests/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" stopOnError="false" stopOnIncoplete="false" stopOnSkipped="false" >
--process-isolation 每一个测试都在独立的PHP进程中运行。
下面贴上完整的phpunit.xml,配置项详见:测试
https://phpunit.de/manual/cur...
<?xml version="1.0" encoding="UTF-8"?> <!-- bootstrap 能够使用项目的autoload文件 --> <phpunit bootstrap="./tests/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" stopOnError="false" stopOnIncoplete="false" stopOnSkipped="false" > <!-- testsuites 指定测试目录集--> <testsuites> <testsuite name="Api Tests"> <directory suffix="Test.php">./tests/Api</directory> </testsuite> <testsuite name="Util Tests"> <directory suffix="Test.php">./tests/Util</directory> </testsuite> </testsuites> <!-- 覆盖率的测试文件,blacklist 黑名单(不须要统计覆盖率的文件),whitelist 白名单(统计覆盖率的测试文件) 当黑名单与白名单文件重复时,白名单起做用 --> <filter> <whitelist processUncoveredFilesFromWhitelist="true"> <directory suffix=".php">./Util</directory> </whitelist> <whitelist processUncoveredFilesFromWhitelist="true"> <directory suffix=".php">./Api</directory> </whitelist> </filter> <!-- 生成单元测试覆盖率 html 文件的目录--> <logging> <log type="coverage-html" target="./tmp/cover" /> <log type="junit" target="./tmp/result.xml" /> </logging> <!-- 错误日志--> <php> <env name="LOCAL_ENV" value="test"/> <env name="APP_ENV" value="test"/> <ini name="error_log" value="/data/service/phpunit.log"/> </php> </phpunit>