[TIP] Using coverage.py with code that uses sys.settrace()

Ned Batchelder ned at nedbatchelder.com
Wed Dec 28 13:43:57 PST 2011

On 12/28/2011 10:58 AM, Leahy, Oliver wrote:
> What's the recommended way to use coverage.py with code that uses 
> sys.settrace()
> I'm using settrace() to simulate error conditions at certain lines 
> during testing, but
> this interferes with the use coverage.py makes of settrace() and I'm 
> getting incorrect
> coverage metrics.
> I'm hacking at my code to try to store an existing value for 
> sys.gettrace() whenever
> I call settrace(),  and then restoring the pre-existing trace function 
> whenever I stop
> tracing using my function, but it's not working for me at the moment.
> Is this a sensible approach to this problem?  Would anyone have any 
> other suggestions?
I don't know if anyone has tried measuring code that uses settrace.  You 
should be able to use gettrace and settrace to save and restore the 
coverage trace function, but I won't be that surprised if it isn't 
working.  You should try using the --timid flag on "coverage run" to see 
if it helps.  It's specifically for environments that do unusual things 
with settrace().  Are you sure settrace() is the best way to accomplish 
your goals? Mocking can also inject errors into running code, and may be 
easier to work with in the long run.

In any case, if you need more help, post more details.

> Thanks,
> Ollie
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20111228/6f6cf9f2/attachment.htm>

More information about the testing-in-python mailing list