Incremental code coverage for Python unit tests?

可紊 提交于 2019-12-23 09:55:31

问题


How can I get an incremental report on code coverage in Python?

By "incremental", I mean what has been the change in the covered lines since some "last" report, or from a particular Git commit.

I'm using unittest and coverage (and coveralls.io) to get the code coverage statistics, which work great. But I'm involved only with a part of the project, and at first I'm concerned with what my last commit has changed. I expected coverage to be able to show the difference between two reports, but so far have not found anything short of running textual diff on HTML output.


回答1:


Brief

I use pycobertura.

pycobertura diff --format html --output cov_diff.html coverage_old.xml coverage_new.xml


Details

I use the following chain (coverage):

  1. Generate coverage report: python -m coverage run -m unittest

  2. Output cobertura's XML format: coverage xml --omit tests/* -o cover_old.xml

  3. -- Modify code or checkout newer commit --

  4. Generate coverage report: python -m coverage run -m unittest

  5. Output cobertura's XML format: coverage xml --omit tests/* -o cover_new.xml

  6. Generate diff: pycobertura diff --format html --output cov_diff.html coverage_old.xml coverage_new.xml




回答2:


I just posted an answer to similar question, it might help. Compares previous run and current run.

show-the-differences-in-two-test-coverage-runs



来源:https://stackoverflow.com/questions/49447326/incremental-code-coverage-for-python-unit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!