[py-dev] Issue with py.test and stderr

holger krekel hpk at trillke.net
Mon Jan 17 11:53:51 CET 2005


On Mon, Jan 17, 2005 at 10:21 +0000, Armin Rigo wrote:
> On Fri, Jan 14, 2005 at 11:56:48AM -0800, Grig Gheorghiu wrote:
> > I did run into a slight issue though. At some point I was instantiating
> > a class which was redirecting stderr to stdout under some
> > circumstances. The __del__ method of my class was resetting sys.stderr
> > to sys.__stderr__.
> 
> Note that there is support in py.test for clean setup/teardown semantics, i.e.
> instead of using __del__ (which is not guaranteed to run timely) you should
> save and restore sys.stderr in setup/teardown methods; e.g.:
> 
> def setup_module(mod):
>     mod.saved_stderr = sys.stderr
>     sys.stderr = ...something else...
> 
> def teardown_module(mod):
>     sys.stderr = mod.saved_stderr

but then you may want to run with the "-S" or "--nocapture" 
option so that py.test doesn't itself do stdout/stderr mangling. 
There is - unfortunately - no easy way yet to switch to "nocapture" 
just for a specific module, class or method.  Although py.test 
is quite customizable it lacks with respect to programmatically 
modifying the options from cmdline-switches.

cheers, 

    holger



More information about the Pytest-dev mailing list