code coverage in python using coverage.py

2.3k Views Asked by At

I am trying to understand running coverage for python scripts. I am not able to understand a scenario where i try to run coverage for a simple script which has an infinite loop:

#!/usr/bin/python

print "The only statement!!!"

while True:
    pass

After invoking coverage for this script, I will kill this process, since it is an infinite loop and if i try to get the result I am getting like:

  1. coverage run sample.py
  2. kill sample
  3. coverage report -m

Name Stmts Miss Cover Missing -------------------------------------

I am not getting any coverage report. Am i doing some thing which is fundamentally wrong?

1

There are 1 best solutions below

3
On BEST ANSWER

coverage needs to be able to write its data out at program end, and if it can't handle the exit signal then it will not generate a report.

So it depends how you are killing your process and how coverage handles the signal - works fine for me when using Ctrl+C (i.e. sending SIGINT) to interrupt sample.py

$ coverage run sample.py
The only statement!!!
Traceback (most recent call last):
  File "sample.py", line 5, in <module>
    while True:
KeyboardInterrupt

$ coverage report -m
Name     Stmts   Miss  Cover   Missing
--------------------------------------
sample       3      0   100%

If you are using the kill utility with no options then you are sending SIGTERM by default, try kill -INT <pid> instead.