Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incremental code coverage for Python unit tests?

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.

like image 414
Konstantin Shemyak Avatar asked Mar 23 '18 10:03

Konstantin Shemyak


1 Answers

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

like image 82
betontalpfa Avatar answered Oct 12 '22 19:10

betontalpfa