Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You just need to use TextTestRunner buffer option:</p> <pre><code>import unittest2 class DemoTest(unittest2.TestCase): def test_one(self): print "'grimelsome' attribute of 'smoxy' was set to 'blimpy'" self.assertTrue(True) def test_two(self): print "'smithereen' attribute of 'brouhaha' was set to 'False'" self.assertTrue(False) if __name__ == '__main__': suite = unittest2.TestLoader().loadTestsFromTestCase(DemoTest) result = unittest2.TextTestRunner(verbosity=2, buffer=True).run(suite) </code></pre> <p>Before each test, <code>TextTestResult</code>, used by the runner, will replace <code>sys.stderr</code> and <code>sys.stdout</code> with its own streams and only send the content to the original streams if the test fails or discards it otherwise.</p> <p>Note that because the fake <code>sys.std*</code> streams changes after each test, if you wanted to to do the same thing with logging output, you would have to add the logging handler after the sys.sdt* stream have been replaced, before each test or implement your own handler.</p> <p>Here's an example subclassing <code>logging.StreamHandler</code>:</p> <pre><code>import logging import sys import unittest2 class DemoTest(unittest2.TestCase): logger = logging.getLogger('DemoTest') def setUp(self): self.logger.debug("setting up stuff and logging it...") def teardown(self): self.logger.debug("You won't see me") print "me neither" def test_one(self): self.logger.debug("'grimelsome' attribute of 'smoxy' was set to 'blimpy'") self.assertTrue(True) def test_two(self): self.logger.debug("'smithereen' attribute of 'brouhaha' was set to 'False'") self.assertTrue(False) class TestHandler(logging.StreamHandler): def __init__(self): logging.Handler.__init__(self) @property def stream(self): """Use which ever stream sys.stderr is referencing.""" return sys.stderr if __name__ == '__main__': formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s') logger_handler = TestHandler() logger_handler.setFormatter(formatter) logger = logging.getLogger() logger.setLevel(logging.DEBUG) logger.addHandler(logger_handler) suite = unittest2.TestLoader().loadTestsFromTestCase(DemoTest) result = unittest2.TextTestRunner( verbosity=2, buffer=True, resultclass=TestResult ).run(suite) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload