Python pytest 整合 coverage

Posted on  Oct 27, 2017  in  Python 模組/套件推薦  by  Amo Chen  ‐ 2 min read

軟體測試的覆蓋率(coverage)也是衡量測試品質的其中一項重要指標,如果是使用 pytest 作為測試框架(framework)的話,可以安裝 pytest-cov 衡量覆蓋率的高低。

本文環境

  • Ubuntu
  • Python 3.5.2
  • pytest 3.1.2
  • pip 9.0.1

安裝 pytest-cov

在 Python & pytest 執行環境的情況下,只要利用以下指令安裝即可:

$ pip install pytest-cov

執行 pytest-cov

安裝完 pytest-cov 之後,執行 py.test -h 就會發現多了一個區塊是給 coverage 用的參數:

coverage reporting with distributed testing support:
  --cov=[path]          measure coverage for filesystem path (multi-allowed)
  --cov-report=type     type of report to generate: term, term-missing,
                        annotate, html, xml (multi-allowed). term, term-
                        missing may be followed by ":skip-covered". annotate,
                        html and xml may be followed by ":DEST" where DEST
                        specifies the output location. Use --cov-report= to
                        not generate any output.
  --cov-config=path     config file for coverage, default: .coveragerc
  --no-cov-on-fail      do not report coverage if test run fails, default:
                        False
  --no-cov              Disable coverage report completely (useful for
                        debuggers) default: False
  --cov-fail-under=MIN  Fail if the total coverage is less than MIN.
  --cov-append          do not delete coverage but append to current, default:
                        False
  --cov-branch          Enable branch coverage.

所以要衡量 coverage 就是在執行 py.test 時放入這些參數即可,例如:

$ py.test --cov=./ --cov-report=html tests/

上述指令的意思為執行 tests/ 資料夾底下的所有測試,衡量當前路徑下的測試覆蓋率,然後將結果產出為 html 格式,結果會儲存在 htmlcov 這個新增的資料夾。

如果不加 --cov-report=html 的話,預設會將結果輸出到 console ,例如:

---------- coverage: platform darwin, python 3.5.2-final-0 -----------
Name                   Stmts   Miss  Cover
------------------------------------------
test.py                    2      0   100%
tests/__init__.py          0      0   100%
tests/test_assert.py       3      0   100%
------------------------------------------
TOTAL                      5      0   100%

從上述結果就可以直接看到各個檔案的 Cover 是多少。

.coveragerc 設定檔

有時候有些程式其實不需要衡量覆蓋率,譬如 __init__.py 一般都會是空的,如果我們希望將它在報告中排除,就可以用 .coveragerc 設定檔進行排除,以下是 .coveragerc 的範例:

[run]
omit =
    */__init__.py
    */migrations/*

上述的範例就是排除各資料夾底下的 __init__.py 以及 migrations/ 資料夾底下的所有檔案。

接著我們在執行 pytest 時可以設定讀取這個設定檔:

$ py.test --cov-config=.coveragerc --cov=./ tests/

就會看到報告中的 __init__.py 被排除了:

Name                   Stmts   Miss  Cover
------------------------------------------
test.py                    2      0   100%
tests/test_assert.py       3      0   100%
------------------------------------------
TOTAL                      5      0   100%

其他的 CONFIG 設定可以到 這裡 查看!

以上就是本篇 pytest 整合 coverage 的教學。

References

  • https://pypi.python.org/pypi/pytest-cov

  • http://coverage.readthedocs.io/en/latest/config.html

對抗久坐職業傷害

研究指出每天增加 2 小時坐著的時間,會增加大腸癌、心臟疾病、肺癌的風險,也造成肩頸、腰背疼痛等常見問題。

然而對抗這些問題,卻只需要工作時定期休息跟伸展身體即可!

你想輕鬆改變現狀嗎?試試看我們的 PomodoRoll 番茄鐘吧! PomodoRoll 番茄鐘會根據你所設定的專注時間,定期建議你 1 項辦公族適用的伸展運動,幫助你打敗久坐所帶來的傷害!

追蹤新知

看完這篇文章了嗎?還意猶未盡的話,追蹤粉絲專頁吧!

我們每天至少分享 1 篇文章/新聞或者實用的軟體/工具,讓你輕鬆增廣見聞提升專業能力!如果你喜歡我們的文章,或是想了解更多特定主題的教學,歡迎到我們的粉絲專頁按讚、留言讓我們知道。你的鼓勵,是我們的原力!

贊助我們的創作

看完這篇文章了嗎? 休息一下,喝杯咖啡吧!

如果你覺得 MyApollo 有讓你獲得實用的資訊,希望能看到更多的技術分享,邀請你贊助我們一杯咖啡,讓我們有更多的動力與精力繼續提供高品質的文章,感謝你的支持!