I have this sample code ( test_it.py ):
import sys def give_me_5(): print >>sys.stdout, "STDOUT" print >>sys.stderr, "STDERR" return 6 import unittest class TestMe(unittest.TestCase): def setUp(self): pass def test_give_me_5(self): self.assertEqual(give_me_5(), 5) if __name__ == '__main__': unittest.main()
Which gives me the following result:
Β» python -m unittest test_it A long annoying output message A long annoying error message F ====================================================================== FAIL: test_give_me_5 (__main__.TestMe) ---------------------------------------------------------------------- Traceback (most recent call last): File "xx.py", line 17, in test_give_me_5 self.assertEqual(give_me_5(), 5) AssertionError: 6 != 5 ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (failures=1)
But because of the long, annoying messages created by the program under test (the real program produces LOTS of output), I can not see the unittest output. I would like to get rid of the stdout / stderr of the test function ( give_me_5 ), but I still want to see stdout / stderr from unittest . I would like to get this result:
Β» python -m unittest test_it F ====================================================================== FAIL: test_give_me_5 (__main__.TestMe) ---------------------------------------------------------------------- Traceback (most recent call last): File "xx.py", line 17, in test_give_me_5 self.assertEqual(give_me_5(), 5) AssertionError: 6 != 5 ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (failures=1)
Thus, the result created by the program under test (both stdout and stderr) is filtered using unittest , but the output generated by unittest is saved . I do not want to change the tested code (without redirection in the code itself). I just want to tell unittest that all the output to stdout / stderr generated by the test code should be discarded.
Is it possible?
source share