I was working on updating some unit tests today at work and was wondering if there was a way to measure the unit test coverage on a set of code. A single google search returned the ability for you to tell the exact test coverage your unit tests achieve and the exact lines of code that are not included in your coverage.
Enter coverage.py: http://nedbatchelder.com/code/coverage/
I’m on fedora so $ yum install python-coverage installed it for me.
To get the details suggested above you need only run two commands.
~/git/audrey git:(oauth*)➤ coverage run audrey_start/test_audrey_startup.py
……………
Ran 15 tests in 2.601s
OK
The coverage run command executes your code just as if you had executed it without coverage.py, but has generated data about the code it’s executing as it’s running. You can see here I’ve executed this code’s unit test script. Next you display the report data:
~/git/audrey git:(oauth*)➤ coverage report audrey_start/*.py
Name Stmts Miss Cover
——————————————————
audrey_start/audrey_startup 472 270 43%
audrey_start/test_audrey_startup 217 19 91%
——————————————————
TOTAL 689 289 58%
~/git/audrey git:(oauth*)➤ coverage report -m audrey_start/*.py
Name Stmts Miss Cover Missing
—————————————————————-
audrey_start/audrey_startup 472 270 43% 126-132, 186-216, 237, 241-243, 247, 288, 352, 379, 382, 388, 414, 456, 544, 558-559, 585-601, 609, 630, 640, 654-689, 700-730, 739, 747, 774-794, 837-847, 854-857, 907-936, 955-957, 975-1125, 1139, 1151, 1190-1195, 1203-1229, 1264-1340, 1344
audrey_start/test_audrey_startup 217 19 91% 65, 130, 384, 420-428, 505-526
—————————————————————-
TOTAL 689 289 58%
The coverage report command will give you the percentages missing for each script. The coverage report -m command will give you the exact lines missing from the unit test coverage. I checked a few of these lines that it reported, a quick check seems to indicate these are all actual lines of code that were not executed during the unit test run. Pretty useful data for measuring the effectiveness of your unit tests.