今天我們來聊聊覆蓋率報告吧!
所謂的覆蓋率報告,指的是能指出我們的專案程式庫,有被測試程式碼實際測試到的部分的佔比有多少。當我們了解有多少程式碼有被覆蓋到,以及有多少程式碼沒被覆蓋到時,理論上我們可以對程式碼是否可正常運作更有信心!
# Laradock 目錄下
docker-compose down
# Check PHP version:
字樣,在其上方加入以下幾行:# 上略
# Add following commmands
RUN pecl install xdebugRUN docker-php-ext-enable xdebug# End of adding commands
###########################################################################
# Check PHP version:
###########################################################################
docker-compose up -d php-fpm
docker exec -it laradock-php-fpm-1 bash
測試覆蓋率報告主要有2種產製方式,一種是純文字輸出於終端機上,一種是輸出 HTML 檔,以下我們就分別測試看看:
純文字輸出
XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-text
PHPUnit 9.5.24 #StandWithUkraine
........... 11 / 11 (100%)
Time: 00:00.321, Memory: 14.00 MB
OK (11 tests, 48 assertions)
Code Coverage Report:
2022-10-07 06:28:48
Summary:
Classes: 0.00% (0/11)
Methods: 0.00% (0/12)
Lines: 6.45% (2/31)
App\Console\Kernel
Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 2)
App\Exceptions\Handler
Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 1)
App\Http\Controllers\Controller
Methods: ( 0/ 0) Lines: ( 0/ 0)
App\Http\Kernel
Methods: ( 0/ 0) Lines: ( 0/ 0)
App\Http\Middleware\Authenticate
Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 2)
App\Http\Middleware\EncryptCookies
Methods: ( 0/ 0) Lines: ( 0/ 0)
App\Http\Middleware\ForbidLoginDuringLunch
Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 4)
App\Http\Middleware\PreventRequestsDuringMaintenance
Methods: ( 0/ 0) Lines: ( 0/ 0)
App\Http\Middleware\RedirectIfAuthenticated
Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 5)
App\Http\Middleware\TrimStrings
Methods: ( 0/ 0) Lines: ( 0/ 0)
App\Http\Middleware\TrustHosts
Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 1)
App\Http\Middleware\TrustProxies
Methods: ( 0/ 0) Lines: ( 0/ 0)
App\Http\Middleware\ValidateSignature
Methods: ( 0/ 0) Lines: ( 0/ 0)
App\Http\Middleware\VerifyCsrfToken
Methods: ( 0/ 0) Lines: ( 0/ 0)
App\Models\User
Methods: ( 0/ 0) Lines: ( 0/ 0)
App\Providers\AppServiceProvider
Methods: ( 0/ 0) Lines: ( 0/ 0)
App\Providers\AuthServiceProvider
Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 1)
App\Providers\BroadcastServiceProvider
Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 2)
App\Providers\EventServiceProvider
Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 1)
App\Providers\RouteServiceProvider
Methods: 0.00% ( 0/ 2) Lines: 0.00% ( 0/ 9)
App\Services\TestService
Methods: 0.00% ( 0/ 1) Lines: 66.67% ( 2/ 3) 11 / 11 (100%)Time: 00:00.321, Memory: 14.00 MBOK (11 tests, 48 assertions)Code Coverage Report: 2022-10-07 06:28:48 Summary: Classes: 0.00% (0/11) Methods: 0.00% (0/12) Lines: 6.45% (2/31)App\Console\Kernel Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 2)App\Exceptions\Handler Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 1)App\Http\Controllers\Controller Methods: ( 0/ 0) Lines: ( 0/ 0)App\Http\Kernel Methods: ( 0/ 0) Lines: ( 0/ 0)App\Http\Middleware\Authenticate Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 2)App\Http\Middleware\EncryptCookies Methods: ( 0/ 0) Lines: ( 0/ 0)App\Http\Middleware\ForbidLoginDuringLunch Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 4)App\Http\Middleware\PreventRequestsDuringMaintenance Methods: ( 0/ 0) Lines: ( 0/ 0)App\Http\Middleware\RedirectIfAuthenticated Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 5)App\Http\Middleware\TrimStrings Methods: ( 0/ 0) Lines: ( 0/ 0)App\Http\Middleware\TrustHosts Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 1)App\Http\Middleware\TrustProxies Methods: ( 0/ 0) Lines: ( 0/ 0)App\Http\Middleware\ValidateSignature Methods: ( 0/ 0) Lines: ( 0/ 0)App\Http\Middleware\VerifyCsrfToken Methods: ( 0/ 0) Lines: ( 0/ 0)App\Models\User Methods: ( 0/ 0) Lines: ( 0/ 0)App\Providers\AppServiceProvider Methods: ( 0/ 0) Lines: ( 0/ 0)App\Providers\AuthServiceProvider Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 1)App\Providers\BroadcastServiceProvider Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 2)App\Providers\EventServiceProvider Methods: 0.00% ( 0/ 1) Lines: 0.00% ( 0/ 1)App\Providers\RouteServiceProvider Methods: 0.00% ( 0/ 2) Lines: 0.00% ( 0/ 9)App\Services\TestService Methods: 0.00% ( 0/ 1) Lines: 66.67% ( 2/ 3)
Summary
的地方,這邊會顯示3種覆蓋率:Classes
、Methods
、Lines
,前2者都是要將單一 Class/Method 內每一行可執行程式碼都有被測試程式碼覆蓋到,才會計算在內,而 Lines 就單純多了,就是有幾行可執行程式碼有被測試程式碼覆蓋到。輸出HTML檔
XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-html report/
以上就是今天的介紹,大家是不是更了解測試覆蓋率了呢?
之後的篇幅,讓我們來演練一些情境題吧!
如果您喜歡這篇文章,歡迎加入追蹤以接收新文章通知 😄