Adventures in Nose: nosetests and unbuffered stdout.
March 6, 2009
In some of our server code, we like to insure we get unbuffered output, like in perl. In Python, this is easy to do:
import sys sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
However, using the wonderful nosetest for testing will barf on this code, because it reassigns stdout to a cStringIO for capturing.
This is a workaround:
import sys import os try: # get unbuffered output sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) except AttributeError, exc: # under nose, sys.stdout is reassigned to a string buffer pass def test(): assert 1
Alternately, run nosetests -s, which disables the output capture feature.
May 3, 2009 at 6:55 pm
Surely, you like to *insure* you get buffered output? It’s easy to be unsure!
May 3, 2009 at 6:56 pm
I’m a big fan of non-deterministic computing, acutally, so it’s more exciting when I’m *unsure* about things. Correctness through approximation.