8

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.

4
  • I suspect this will get closed as asking for a recommendation for software. Likely this will be a feature of your CI (unittest and coverage don't understand git and for good reason). You may look at codeclimate, which IIRC does this (free for open source as well). Commented Mar 23, 2018 at 10:23
  • @BaileyParker Thank you for the note. I do not necessarily require integration with Git (although that would be nice), the difference from a "previous run" would be fine too. I expected coverage to be able to show the difference between two runs, but so far have not found anything short of running textual diff on HTML output. Updated the question accordingly. Commented Mar 23, 2018 at 10:33
  • Ah, yes I just assumed since you were using coveralls.io that you were using version control. For diffing two runs, (you'd need to do some legwork), but coverage xml can output an xml report which you may be able to parse yourself and then diff (it may also provide utilities to parse the XML for you). Commented Mar 23, 2018 at 10:36
  • 4
    Diff cover is the tool github.com/Bachmann1234/diff-cover Commented Mar 23, 2018 at 14:58

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.