報告

可以為單次測試運行產生任何組合的報告。

可用的報告有終端機 (顯示或不顯示遺失的行號)、HTML、XML、JSON、LCOV 和附註來源程式碼。

不顯示行號的終端機報告 (預設)

pytest --cov-report term --cov=myproj tests/

-------------------- coverage: platform linux2, python 2.6.4-final-0 ---------------------
Name                 Stmts   Miss  Cover
----------------------------------------
myproj/__init__          2      0   100%
myproj/myproj          257     13    94%
myproj/feature4286      94      7    92%
----------------------------------------
TOTAL                  353     20    94%

顯示行號的終端機報告

pytest --cov-report term-missing --cov=myproj tests/

-------------------- coverage: platform linux2, python 2.6.4-final-0 ---------------------
Name                 Stmts   Miss  Cover   Missing
--------------------------------------------------
myproj/__init__          2      0   100%
myproj/myproj          257     13    94%   24-26, 99, 149, 233-236, 297-298, 369-370
myproj/feature4286      94      7    92%   183-188, 197
--------------------------------------------------
TOTAL                  353     20    94%

終端機報告顯示已略過的涵蓋範圍

pytest --cov-report term:skip-covered --cov=myproj tests/

-------------------- coverage: platform linux2, python 2.6.4-final-0 ---------------------
Name                 Stmts   Miss  Cover
----------------------------------------
myproj/myproj          257     13    94%
myproj/feature4286      94      7    92%
----------------------------------------
TOTAL                  353     20    94%

1 files skipped due to complete coverage.

您也可以將 skip-coveredterm-missing 搭配使用。例如: --cov-report term-missing:skip-covered

以下四種報告選項輸出到檔案,而不會在終端機上顯示任何內容

pytest --cov-report html
        --cov-report xml
        --cov-report json
        --cov-report lcov
        --cov-report annotate
        --cov=myproj tests/

可以指定這四種報告每種的輸出位置。XML、JSON 和 LCOV 報告的輸出位置是檔案。HTML 和附註來源程式碼報告的輸出位置是目錄

pytest --cov-report html:cov_html
        --cov-report xml:cov.xml
        --cov-report json:cov.json
        --cov-report lcov:cov.info
        --cov-report annotate:cov_annotate
        --cov=myproj tests/

最後一個報告選項也可以抑制列印到終端機

pytest --cov-report= --cov=myproj tests/

此模式特別適用於持續整合伺服器,因為需要涵蓋範圍檔案以供後續處理,但不需要檢視本機報告。例如,在 GitHub Actions 上執行的測試可以產生 .coverage 檔案供 Coveralls 使用。