Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python unittest - Using 'buffer' option to suppress stdout - how do I do it?

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.)

like image 519
tadasajon Avatar asked Jan 09 '13 20:01

tadasajon


People also ask

How do you ignore a test in Python?

The skip() decorator can be used to skip over a test that need not be run at all. skipIf() and skipUnless() can be a useful way to write tests that only apply to certain platforms or Python versions, or which have other dependencies.

What is the purpose of the assertEqual method in the Unittest?

assertEqual() in Python is a unittest library function that is used in unit testing to check the equality of two values. This function will take three parameters as input and return a boolean value depending upon the assert condition.

How do you use assertRaises in Python?

There are two ways you can use assertRaises: using keyword arguments. Just pass the exception, the callable function and the parameters of the callable function as keyword arguments that will elicit the exception. Make a function call that should raise the exception with a context.


1 Answers

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

print "Suppress me!"

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.

like image 149
Mikhail Karavashkin Avatar answered Oct 12 '22 06:10

Mikhail Karavashkin