In the unittest docs [ http://docs.python.org/2/library/unittest.html#unittest.main ], I see the following method signature described:
unittest.main([module[, defaultTest[, argv[, testRunner[, testLoader[, exit[, verbosity[, failfast[, catchbreak[, buffer]]]]]]]]]])
The last option is “buffer”. The docs explain the following about this option:
The failfast, catchbreak and buffer parameters have the same effect as the same-name command-line options.
The docs for the command-line options [ http://docs.python.org/2/library/unittest.html#command-line-options ] explain ‘buffer’ as follows:
-b, –buffer
The standard output and standard error streams are buffered during the test run. Output during a passing test is discarded. Output is echoed normally on test fail or error and is added to the failure messages.
I have the following demo code which does not exhibit the behavior that would be expected:
import unittest2
class DemoTest(unittest2.TestCase):
def test_one(self):
self.assertTrue(True)
def test_two(self):
self.assertTrue(True)
if __name__ == '__main__':
test_program = unittest2.main(verbosity=0, buffer=True, exit=False)
The output of this program is:
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
In fact, I get the same output if I chang the last line in my program to:
test_program = unittest2.main(verbosity=0, buffer="hello", exit=False)
What am I doing wrong? (I tried using unittest instead of unittest2, but it made no difference.)
The point is that buffer option affects stdout writing inside your tests, ignoring that of unittest2 behaviour. That is to say, you will see the difference, if you add string like
to any test method, this expression will appear on stdout, if you choose
buffer=False, while it will be suppressed if you set it to True.