[TIP] branch coverage

Andrew Dalke dalke at dalkescientific.com
Sat Mar 8 18:29:29 PST 2008

In late January I mentioned I was working on a parser for Python  
using PLY as the parser engine.  I wanted to experiment with branch  
coverage in Python by reading the source .py file, building a parse  
tree, adding instrumentation to the AST, and saving .pyc files with  
the instrumentation.  Then run the regression tests and see which  
branches weren't covered.

I'm not there yet.

What I have done is write a Python parser using PLY.  It's called  
"python4ply" and is available at:

What took even longer than writing the code was writing the  
tutorial.  It shows examples of how to change the lexer, parser, AST,  
and bytecode generation.  I worked through two examples  where I  
instrumented the code to improve testing.

The first of these assumes there are problems like this

     assert 0 not in results, "problem in: %r" % data

where the "data" variable should be "results".  Assert statements are  
rarely fully tested and so prone to failure if they ever occur.  My  
example transforms the code to something like

_$assert = Y
assert X, _$assert

where '$' is something that normal Python code can't generate but AST  
manipulation code can.

Details at: http://dalkescientific.com/Python/python4ply- 

The second shows a way to add statement coverage without using  
settrace.  Details at:


I didn't get to the point where I could do branch coverage.  I didn't  
have a good enough use cases so wasn't sure what I should implement.   
Perhaps you've got an idea of how to do it, or something else  
involving parsing Python code?  (If so, take a look at the _ast  
module from the standard library.  It's probably a better solution.)   
If so, I hope this grammar and code helps.

Download:  http://dalkescientific.com/Python/python4ply.html
Tutorial:  http://dalkescientific.com/Python/python4ply-tutorial.html

				dalke at dalkescientific.com

More information about the testing-in-python mailing list