Testing & stdout

John Roth newsgroups at jhrothjr.com
Tue Nov 4 11:06:19 EST 2003


"Peter Hansen" <peter at engcorp.com> wrote in message
news:3FA7C976.7AF9749D at engcorp.com...
> Peter Hansen wrote:
> >
> > Miki Tebeka wrote:
> > >
> > > In my test suite I also test some function that output messages to
stdout.
> > > Is there an easy way to temporary divert stdout to another location?
> > >
> > > Currently I'm using:
> > [snip]
> > > This works fine if no one is caching stdout somewhere.
> >
> > "Caching stdout"?  What's that mean?  Who would do such a thing?
>
> Ah, reading John's reply I think I figured out what you meant.
> You mean some of the code being called is using a saved reference
> to sys.stdout, and so it won't dynamically pick your changed version
> of that name.
>
> I encountered the same problem once, and found no solution.

Good point. If he's using unittest, it's a problem in only one case:
where the cached reference is at the module level. Otherwise,
each unit test starts fresh.

If it's at the module level, then the unit test suite has to do something
different. I'd fix the offending module, but possibly he isn't using
XP, so he can't do that.

John Roth

>
> -Peter






More information about the Python-list mailing list