[IronPython] Code Coverage

Michael Foord michael.foord at resolversystems.com
Thu Jun 22 13:12:37 CEST 2006

Hello all,

I've been looking at code coverage tools for Python, as we would like to 
check the coverage of our unit tests.

Unfortunately all the Python code coverage tools use ``sys.settrace`` 
which isn't implemented in IronPython. This (I guess) is because Python 
stack frames and code objects aren't used in IronPython.

Unfortunately this makes coverage tools impossible (or just very 
difficult),  unless an alternative mechanism is provided. (They also 
tend to use the parser module which is another issue.)

I browsed the IronPython source (is there an API doc ?) but couldn't see 
anything useful to get a callback as new source-code lines are entered. 
Line information is obviously *somewhere*, as it is presented in 
traceback information.

Two alternative ideas.

1) Implement a 'cut down' version of sys.settrace which only implements 
what is possible.

For sys.settrace - see 

coverage.py is one of the most common tools :


It uses settrace with the following function :

def t(f, x, y):
    c[(f.f_code.co_filename, f.f_lineno)] = 1
    return t

So it only uses a filename reference from the code object and the 
line_no from the frame. Either dummy objects could be constructed, or an 
IronPython specific implementation could be made which presents this 
information directly.

2) Alternatively a callback hook on the C# side could be implemented, so 
that as IronPython enters a new line we could cache this information - 
and generate a report once execution has stopped.

Either of these would be much appreciated.

All the best,

Michael Foord

More information about the Ironpython-users mailing list