Code coverage in Python, or my first little challenge.

Please note that everything written below is just my personal observations, reasoning and conclusions. It may be useful to you, but do not consider it at as verified knowledge source, as information here may be incomplete or mistaken.

I got tired of reading Python documentation and thought that it was a good time to get my hands dirty in the code. After a while I had a simple sudoku solver. Nothing outstanding, just an interesting project to learn new programming language. Quite soon I realised that I need unit tests not only to protect functionality, but also to explore some neat language features. Covering code with test is simple and python has a good explanation in official documentation, but at some point I thought about code coverage, and that’s where I had some problems. Below I’ll provide short description of what was done to make it work as intended.

  • I’m using for code coverage, as it’s simple, well documented and popular. To install it use this command
python3 -m pip install coverage
  • Next make sure that your test code is executable as script. Complete documentation could be found here, but basically you just need to add these lines at the end of your test scipt
if __name__ == "__main__":
  • Now it’s time to execute test with code coverage. My project is named “likemammoth”. Include param specified for which files to collect coverage info. Note that I’m running coverage not by specifying test script, but rather module (you’ll also need file in each package in this case). This let’s me use in that is not visible easily, as is located in parent directory.
python3 -m coverage run --include=likemammoth/ -m likemammoth.test.test_puzzle

After this operation you should see how many tests passed and have .coverage file in your current directory

  • Next let’s generate readable report. The easiest what is to run
python3 -m coverage report

that will produce something like this

Name                    Stmts   Miss  Cover


likemammoth/     122     15    88%

Another option is to generate xml or html report.  In this case use

python3 -m coverage html

and if everything is good you’ll find “htmlcov” dir in your current working directory. Open index.html to see report.

  • Well actually that’s almost it. The last thing you’d like to do is to clean you space. Delete “htmlcov” manually and run
python3 -m coverage erase

to delete .coverage files.


That’s it for today. Not too much to gain, but hey, even tiny step forward is still good)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s