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

Leahy, Oliver oliver.leahy at hp.com
Thu Dec 29 01:22:20 PST 2011

Thanks for the suggestions, I've tried the -timid flag, hasn't helped. And unfortunately
(for me) it looks like I can't successfully reset the trace function with settrace() because of
the way my trace functions work; I raise exceptions when I reached specified points in the
code, so once the exception is raised I don't get an opportunity to call sys.settrace().

So I'm going to have to use mocks, as you suggest. What I really want is for sys.settrace() to
be able to manage a stack of functions that would be called in sequence :)

Thanks again,

From: Ned Batchelder [mailto:ned at nedbatchelder.com]
Sent: 28 December 2011 21:44
To: Leahy, Oliver
Cc: testing-in-python at lists.idyll.org
Subject: Re: [TIP] Using coverage.py with code that uses sys.settrace()

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.




testing-in-python mailing list

testing-in-python at lists.idyll.org<mailto:testing-in-python at lists.idyll.org>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20111229/93287833/attachment.html>

More information about the testing-in-python mailing list