Tox

使用 tox 時,可以使用極為精簡的設定 - 您可以在 tox.ini 中設定所有內容

[tox]
envlist = ...

[tool:pytest]
...

[coverage:paths]
...

[coverage:run]
...

[coverage:report]
..

[testenv]
commands = ...

常見的使用問題是,pytest-cov 會預設清除上一次的涵蓋範圍資料,所以如果您使用 tox 執行多個環境,最後將只會取得不完整的涵蓋範圍資料。

若要防止這個問題,您需要使用 --cov-append。仍然建議清除上一次的涵蓋範圍資料,以得到一致的輸出內容。如下所示的 tox.ini 應該足夠用於順序執行

[tox]
envlist = clean,py27,py36,...

[testenv]
commands = pytest --cov --cov-append --cov-report=term-missing ...
deps =
    pytest
    pytest-cov

[testenv:clean]
deps = coverage
skip_install = true
commands = coverage erase

對於平行執行,我們需要設定一些依賴關係,而且需要有一個額外的回報環境,如下所示

[tox]
envlist = clean,py27,py36,report

[testenv]
commands = pytest --cov --cov-append --cov-report=term-missing
deps =
    pytest
    pytest-cov
depends =
    {py27,py36}: clean
    report: py27,py36

[testenv:report]
deps = coverage
skip_install = true
commands =
    coverage report
    coverage html

[testenv:clean]
deps = coverage
skip_install = true
commands = coverage erase

根據您的專案配置,您可能需要額外的設定,請參閱 https://github.com/pytest-dev/pytest-cov/tree/master/examples 來取得兩個常見的配置範例。