報錯信息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>