2

I've been toying around with coverage.py, but can't seem to get it to gather coverage for the __main__ module.

I'm on Windows, and like to hack up scripts using IDLE. The edit-hit-F5 cycle is really convenient, fast, and fun. Unfortunately, it doesn't look like coverage.py is able (or willing) to gather coverage of the main module -- in the code below, it reports that no data is collected. My code looks like this:

import coverage
cov = coverage.coverage()
cov.start()

def CodeUnderTest():
  print 'do stuff'
  return True

assert CodeUnderTest()

cov.stop()
cov.save()
cov.html_report()

Anyone have any ideas? I've tried various options to coverage, but to no avail. It seems like the environment IDLE creates isn't very friendly towards coverage, since sys.modules['__main__'] points to a idle.pyw file, not the file its running.

oz123
  • 27,559
  • 27
  • 125
  • 187
Richard Levasseur
  • 14,562
  • 6
  • 50
  • 63

1 Answers1

1

You haven't said what behavior you are seeing, but I would expect that the two line in CodeUnderTest would show as covered, but none of the other lines in the file are. Coverage.py can't measure execution that happened before it was started, and here it isn't started until after the module has been executed. For example, the import coverage line has already been executed by the time coverage is started. Additionally, once coverage has been started, it isn't until the next function call that measurement truly begins.

The simplest way to run coverage.py is to use it from the command line. That way, you know that it is starting as early as possible:

$ coverage run my_prog.py arg1 arg2 ...

If you must use it programmatically, arrange your file so that all the excecution you're interested in happens inside a function that is invoked after coverage is started.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • Sorry, I'll add what behavior I'm seeing to the question: it reports no data collected – Richard Levasseur Feb 06 '12 at 17:45
  • Also, I'd like to avoid running it on the command line because the edit-hit-f5 cycle is so convenient. I have it run unittest.main and it prints the test output right in the window. No dealing with Windows' crappy command line. – Richard Levasseur Feb 06 '12 at 17:49
  • Hmm, it must be something about IDLE, because when I try your code on the command line, it behaves as I expected. IDLE isn't really great for serious development, you may be the first person to use coverage.py and IDLE together! – Ned Batchelder Feb 06 '12 at 22:26
  • @NedBatchelder But how do I run coverage from commandline on windows. I installed coverage using pip. Now coverage is installed under C:\Python\Lib\site-packages\coverage. How do I use it as a standalone command? I'm sorry if my doubt is very stupid, but since coverage is a python module, I presume we have to use it using python interpreter or python VM. Then how can I possibly use it as a standalone program? – VaidAbhishek Jul 15 '12 at 13:52