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?

1
  • Can you include the console output from the steps above? Commented Aug 26, 2014 at 10:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
I don't know about other coverage tools, but this documentation page for Coverage.py says it will work fine if you use the higher-level threading interface. So you could wrap your thread functions with thread classes.
Thanks. That helped. Also i found that to get coverage results you should use sys.exit on all threads.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.