Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top