问题
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):
Generate coverage report:
python -m coverage run -m unittest
Output cobertura's XML format:
coverage xml --omit tests/* -o cover_old.xml
-- Modify code or checkout newer commit --
Generate coverage report:
python -m coverage run -m unittest
Output cobertura's XML format:
coverage xml --omit tests/* -o cover_new.xml
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