大家好,我遇到一個問題,我想看我測試寫的覆蓋率,但會出現錯誤
➜ ironman git:(master) ✗ php -dxdebug.mode=coverage vendor/bin/phpunit --coverage-html build/coverage
PHPUnit 9.5.9 by Sebastian Bergmann and contributors.
.......... 10 / 10 (100%)
Time: 00:01.871, Memory: 44.00 MB
OK (10 tests, 21 assertions)
Generating code coverage report in HTML format ... PHP Fatal error: Cannot declare class App\Http\Controllers\api\UserController, because the name is already in use in /Users/jimchien/work/ironman/app/Http/Controllers/api/UserController.php on line 11
Fatal error: Cannot declare class App\Http\Controllers\api\UserController, because the name is already in use in /Users/jimchien/work/ironman/app/Http/Controllers/api/UserController.php on line 11
PHP Fatal error: Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /Users/jimchien/work/ironman/vendor/laravel/framework/src/Illuminate/Container/Container.php:1089
是說我的UserController重複宣告了,後來我在測試中自己先new UserController後,錯誤訊息變了
public function test1()
{
new UserController();
assertTrue(true);
}
錯誤訊息變成
➜ ironman git:(master) ✗ php -dxdebug.mode=coverage vendor/bin/phpunit --coverage-html build/coverage
PHPUnit 9.5.9 by Sebastian Bergmann and contributors.
........... 11 / 11 (100%)
Time: 00:02.102, Memory: 44.00 MB
OK (11 tests, 22 assertions)
Generating code coverage report in HTML format ... PHP Fatal error: Cannot declare class App\Providers\EventServiceProvider, because the name is already in use in /Users/jimchien/work/ironman/app/Providers/QueryLogServiceProvider.php on line 8
Fatal error: Cannot declare class App\Providers\EventServiceProvider, because the name is already in use in /Users/jimchien/work/ironman/app/Providers/QueryLogServiceProvider.php on line 8
PHP Fatal error: Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /Users/jimchien/work/ironman/vendor/laravel/framework/src/Illuminate/Container/Container.php:1089
於是我改了phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app/Http</directory>
</include>
</coverage>
<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<!-- <server name="DB_CONNECTION" value="sqlite"/> -->
<!-- <server name="DB_DATABASE" value=":memory:"/> -->
<server name="MAIL_MAILER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="TELESCOPE_ENABLED" value="false"/>
</php>
</phpunit>
原本include directory 是 ./app 後來改成 ./app/Http
就可以了..., 想問問是發生什麼問題,導致重複宣告
phpunit.xml 加上屬性 processIsolation="true"
就可以了
不過 tests 會變的跑超慢,因為變成一個 test 是一個獨立的 process 在跑