Back to index

python3.2  3.2.2
Public Member Functions | Private Attributes
unittest.test.test_result.TestOutputBuffering Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def testBufferOutputOff
def testBufferOutputStartTestAddSuccess
def getStartedResult
def testBufferOutputAddErrorOrFailure
def testBufferSetupClass
def testBufferTearDownClass
def testBufferSetUpModule
def testBufferTearDownModule

Private Attributes

 _real_out
 _real_err

Detailed Description

Definition at line 374 of file test_result.py.


Member Function Documentation

Definition at line 447 of file test_result.py.

00447 
00448     def getStartedResult(self):
00449         result = unittest.TestResult()
00450         result.buffer = True
00451         result.startTest(self)
00452         return result

Here is the caller graph for this function:

Definition at line 376 of file test_result.py.

00376 
00377     def setUp(self):
00378         self._real_out = sys.stdout
00379         self._real_err = sys.stderr

Definition at line 380 of file test_result.py.

00380 
00381     def tearDown(self):
00382         sys.stdout = self._real_out
00383         sys.stderr = self._real_err

Definition at line 453 of file test_result.py.

00453 
00454     def testBufferOutputAddErrorOrFailure(self):
00455         unittest.result.traceback = MockTraceback
00456         self.addCleanup(restore_traceback)
00457 
00458         for message_attr, add_attr, include_error in [
00459             ('errors', 'addError', True),
00460             ('failures', 'addFailure', False),
00461             ('errors', 'addError', True),
00462             ('failures', 'addFailure', False)
00463         ]:
00464             result = self.getStartedResult()
00465             buffered_out = sys.stdout
00466             buffered_err = sys.stderr
00467             result._original_stdout = io.StringIO()
00468             result._original_stderr = io.StringIO()
00469 
00470             print('foo', file=sys.stdout)
00471             if include_error:
00472                 print('bar', file=sys.stderr)
00473 
00474 
00475             addFunction = getattr(result, add_attr)
00476             addFunction(self, (None, None, None))
00477             result.stopTest(self)
00478 
00479             result_list = getattr(result, message_attr)
00480             self.assertEqual(len(result_list), 1)
00481 
00482             test, message = result_list[0]
00483             expectedOutMessage = textwrap.dedent("""
00484                 Stdout:
00485                 foo
00486             """)
00487             expectedErrMessage = ''
00488             if include_error:
00489                 expectedErrMessage = textwrap.dedent("""
00490                 Stderr:
00491                 bar
00492             """)
00493 
00494             expectedFullMessage = 'A traceback%s%s' % (expectedOutMessage, expectedErrMessage)
00495 
00496             self.assertIs(test, self)
00497             self.assertEqual(result._original_stdout.getvalue(), expectedOutMessage)
00498             self.assertEqual(result._original_stderr.getvalue(), expectedErrMessage)
00499             self.assertMultiLineEqual(message, expectedFullMessage)

Here is the call graph for this function:

Definition at line 384 of file test_result.py.

00384 
00385     def testBufferOutputOff(self):
00386         real_out = self._real_out
00387         real_err = self._real_err
00388 
00389         result = unittest.TestResult()
00390         self.assertFalse(result.buffer)
00391 
00392         self.assertIs(real_out, sys.stdout)
00393         self.assertIs(real_err, sys.stderr)
00394 
00395         result.startTest(self)
00396 
00397         self.assertIs(real_out, sys.stdout)
00398         self.assertIs(real_err, sys.stderr)

Here is the call graph for this function:

Definition at line 399 of file test_result.py.

00399 
00400     def testBufferOutputStartTestAddSuccess(self):
00401         real_out = self._real_out
00402         real_err = self._real_err
00403 
00404         result = unittest.TestResult()
00405         self.assertFalse(result.buffer)
00406 
00407         result.buffer = True
00408 
00409         self.assertIs(real_out, sys.stdout)
00410         self.assertIs(real_err, sys.stderr)
00411 
00412         result.startTest(self)
00413 
00414         self.assertIsNot(real_out, sys.stdout)
00415         self.assertIsNot(real_err, sys.stderr)
00416         self.assertIsInstance(sys.stdout, io.StringIO)
00417         self.assertIsInstance(sys.stderr, io.StringIO)
00418         self.assertIsNot(sys.stdout, sys.stderr)
00419 
00420         out_stream = sys.stdout
00421         err_stream = sys.stderr
00422 
00423         result._original_stdout = io.StringIO()
00424         result._original_stderr = io.StringIO()
00425 
00426         print('foo')
00427         print('bar', file=sys.stderr)
00428 
00429         self.assertEqual(out_stream.getvalue(), 'foo\n')
00430         self.assertEqual(err_stream.getvalue(), 'bar\n')
00431 
00432         self.assertEqual(result._original_stdout.getvalue(), '')
00433         self.assertEqual(result._original_stderr.getvalue(), '')
00434 
00435         result.addSuccess(self)
00436         result.stopTest(self)
00437 
00438         self.assertIs(sys.stdout, result._original_stdout)
00439         self.assertIs(sys.stderr, result._original_stderr)
00440 
00441         self.assertEqual(result._original_stdout.getvalue(), '')
00442         self.assertEqual(result._original_stderr.getvalue(), '')
00443 
00444         self.assertEqual(out_stream.getvalue(), '')
00445         self.assertEqual(err_stream.getvalue(), '')
00446 

Here is the call graph for this function:

Definition at line 500 of file test_result.py.

00500 
00501     def testBufferSetupClass(self):
00502         result = unittest.TestResult()
00503         result.buffer = True
00504 
00505         class Foo(unittest.TestCase):
00506             @classmethod
00507             def setUpClass(cls):
00508                 1/0
00509             def test_foo(self):
00510                 pass
00511         suite = unittest.TestSuite([Foo('test_foo')])
00512         suite(result)
00513         self.assertEqual(len(result.errors), 1)

Definition at line 528 of file test_result.py.

00528 
00529     def testBufferSetUpModule(self):
00530         result = unittest.TestResult()
00531         result.buffer = True
00532 
00533         class Foo(unittest.TestCase):
00534             def test_foo(self):
00535                 pass
00536         class Module(object):
00537             @staticmethod
00538             def setUpModule():
00539                 1/0
00540 
00541         Foo.__module__ = 'Module'
00542         sys.modules['Module'] = Module
00543         self.addCleanup(sys.modules.pop, 'Module')
00544         suite = unittest.TestSuite([Foo('test_foo')])
00545         suite(result)
00546         self.assertEqual(len(result.errors), 1)

Definition at line 514 of file test_result.py.

00514 
00515     def testBufferTearDownClass(self):
00516         result = unittest.TestResult()
00517         result.buffer = True
00518 
00519         class Foo(unittest.TestCase):
00520             @classmethod
00521             def tearDownClass(cls):
00522                 1/0
00523             def test_foo(self):
00524                 pass
00525         suite = unittest.TestSuite([Foo('test_foo')])
00526         suite(result)
00527         self.assertEqual(len(result.errors), 1)

Definition at line 547 of file test_result.py.

00547 
00548     def testBufferTearDownModule(self):
00549         result = unittest.TestResult()
00550         result.buffer = True
00551 
00552         class Foo(unittest.TestCase):
00553             def test_foo(self):
00554                 pass
00555         class Module(object):
00556             @staticmethod
00557             def tearDownModule():
00558                 1/0
00559 
00560         Foo.__module__ = 'Module'
00561         sys.modules['Module'] = Module
00562         self.addCleanup(sys.modules.pop, 'Module')
00563         suite = unittest.TestSuite([Foo('test_foo')])
00564         suite(result)
00565         self.assertEqual(len(result.errors), 1)
00566 


Member Data Documentation

Definition at line 378 of file test_result.py.

Definition at line 377 of file test_result.py.


The documentation for this class was generated from the following file: