2

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?

oz123
  • 27,559
  • 27
  • 125
  • 187
Vijay
  • 155
  • 2
  • 12

1 Answers1

5

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.

snowcrash09
  • 4,694
  • 27
  • 45
  • Thanks. That worked. Now i have a different problem. Coverage.py's documentation says, it doesnt work well with thread module. So is there any other coverage tool for python that supports thread also? – Vijay Aug 26 '14 at 12:53
  • I don't know about other coverage tools, but [this documentation page](http://nedbatchelder.com/code/coverage/trouble.html) for Coverage.py says it will work fine if you use the [higher-level threading interface](https://docs.python.org/2/library/threading.html). So you could wrap your thread functions with thread classes. – snowcrash09 Aug 26 '14 at 13:00
  • Thanks. That helped. Also i found that to get coverage results you should use sys.exit on all threads. – Vijay Sep 04 '14 at 03:41