[TIP] Calculating coverage of runtime-generated functions

C. Titus Brown ctb at msu.edu
Mon May 4 06:51:36 PDT 2009

-> >   If you have ideas about how to 
-> > automate value coverage measurement, I'd love to hear them.  I've given 
-> > it no thought, but it could be very valuable.
-> I don't like to call this value coverage since that's a much more
-> general thing. I do have some ideas but those involve source code
-> changes (decorations on the applicable functions) and changes in both
-> test runners as well as coverage tools. Not sure it's possible at all,
-> and even if it is it's rather ugly. Will try to get something working
-> though.

Hi Nicolas,

it does involve reaching into the guts of CPython a bit, but I know it's
relatively easy to "adjust" the filenames and line numbers of compiled
Python code; check out the function twill/parse.py:_execute_script,
look for 'cmdinfo'.  This code tells the Python interpreter that the
code being executed belongs to a twill script file, and this information
is passed through to the settrace function that records code coverage.

I can't see how you'd use this exactly, but at least you can manipulate
what line numbers are being recorded.  It might be possible to build
something around that for your purposes.

C. Titus Brown, ctb at msu.edu

More information about the testing-in-python mailing list