Sunday, May 2, 2021

Published May 02, 2021 by with 0 comment

Simple way to see code coverage in python

Sometimes you want to quickly see unit test coverage of your code. Coverage.py makes that really simple.
First, what do I mean by test coverage? Below is an example for a really simple usage:



That tells me how much of the code is executed when I executed my tests (start with test_* here). For that example, I just have two files with two methods in each. The files are identical and look like this:



I have one file with unit tests. I'm using pytest for this but coverage works with other test frameworks. Here is the unit test file:



With a file this simple, we can see some obvious test gaps like:
  • it doesn't test all functions in all files
  • it doesn't test all paths in the functions (e.g., no 0 test in file1)
You can imagine how hard it is to see that for any realistic code though. This is where coverage checks can help. Clicking into the coverage for file2 here we get:


Really simple to see that we missed the non-zero case in is_zero and that we didn't call the is_zero_wrapper function at all in any of the tested paths.

It is important to note that 100% coverage doesn't mean your code is perfectly tested and that less than 100% coverage doesn't mean your codebase is garbage. This is just one of many useful metrics for gauging test coverage and testing gaps.

To set this up and run it:
  1. install coverage (e.g., 'pip install coverage')
  2. install pytest (e.g., 'pip install pytest')
  3. run 'coverage run -m pytest'
  4. run 'coverage html'
  5. open the index.html file in the htmlcov folder that it created
That index.html file is what I have in the screenshot at the start.

If you want to use my exact code to test this, it's available here.


      edit

0 comments:

Post a Comment