ยปCore Development>Code coverage>Lib/unittest/test/test_result.py

Python code coverage for Lib/unittest/test/test_result.py

#countcontent
1n/aimport io
2n/aimport sys
3n/aimport textwrap
4n/a
5n/afrom test import support
6n/a
7n/aimport traceback
8n/aimport unittest
9n/a
10n/a
11n/aclass MockTraceback(object):
12n/a class TracebackException:
13n/a def __init__(self, *args, **kwargs):
14n/a self.capture_locals = kwargs.get('capture_locals', False)
15n/a def format(self):
16n/a result = ['A traceback']
17n/a if self.capture_locals:
18n/a result.append('locals')
19n/a return result
20n/a
21n/adef restore_traceback():
22n/a unittest.result.traceback = traceback
23n/a
24n/a
25n/aclass Test_TestResult(unittest.TestCase):
26n/a # Note: there are not separate tests for TestResult.wasSuccessful(),
27n/a # TestResult.errors, TestResult.failures, TestResult.testsRun or
28n/a # TestResult.shouldStop because these only have meaning in terms of
29n/a # other TestResult methods.
30n/a #
31n/a # Accordingly, tests for the aforenamed attributes are incorporated
32n/a # in with the tests for the defining methods.
33n/a ################################################################
34n/a
35n/a def test_init(self):
36n/a result = unittest.TestResult()
37n/a
38n/a self.assertTrue(result.wasSuccessful())
39n/a self.assertEqual(len(result.errors), 0)
40n/a self.assertEqual(len(result.failures), 0)
41n/a self.assertEqual(result.testsRun, 0)
42n/a self.assertEqual(result.shouldStop, False)
43n/a self.assertIsNone(result._stdout_buffer)
44n/a self.assertIsNone(result._stderr_buffer)
45n/a
46n/a # "This method can be called to signal that the set of tests being
47n/a # run should be aborted by setting the TestResult's shouldStop
48n/a # attribute to True."
49n/a def test_stop(self):
50n/a result = unittest.TestResult()
51n/a
52n/a result.stop()
53n/a
54n/a self.assertEqual(result.shouldStop, True)
55n/a
56n/a # "Called when the test case test is about to be run. The default
57n/a # implementation simply increments the instance's testsRun counter."
58n/a def test_startTest(self):
59n/a class Foo(unittest.TestCase):
60n/a def test_1(self):
61n/a pass
62n/a
63n/a test = Foo('test_1')
64n/a
65n/a result = unittest.TestResult()
66n/a
67n/a result.startTest(test)
68n/a
69n/a self.assertTrue(result.wasSuccessful())
70n/a self.assertEqual(len(result.errors), 0)
71n/a self.assertEqual(len(result.failures), 0)
72n/a self.assertEqual(result.testsRun, 1)
73n/a self.assertEqual(result.shouldStop, False)
74n/a
75n/a result.stopTest(test)
76n/a
77n/a # "Called after the test case test has been executed, regardless of
78n/a # the outcome. The default implementation does nothing."
79n/a def test_stopTest(self):
80n/a class Foo(unittest.TestCase):
81n/a def test_1(self):
82n/a pass
83n/a
84n/a test = Foo('test_1')
85n/a
86n/a result = unittest.TestResult()
87n/a
88n/a result.startTest(test)
89n/a
90n/a self.assertTrue(result.wasSuccessful())
91n/a self.assertEqual(len(result.errors), 0)
92n/a self.assertEqual(len(result.failures), 0)
93n/a self.assertEqual(result.testsRun, 1)
94n/a self.assertEqual(result.shouldStop, False)
95n/a
96n/a result.stopTest(test)
97n/a
98n/a # Same tests as above; make sure nothing has changed
99n/a self.assertTrue(result.wasSuccessful())
100n/a self.assertEqual(len(result.errors), 0)
101n/a self.assertEqual(len(result.failures), 0)
102n/a self.assertEqual(result.testsRun, 1)
103n/a self.assertEqual(result.shouldStop, False)
104n/a
105n/a # "Called before and after tests are run. The default implementation does nothing."
106n/a def test_startTestRun_stopTestRun(self):
107n/a result = unittest.TestResult()
108n/a result.startTestRun()
109n/a result.stopTestRun()
110n/a
111n/a # "addSuccess(test)"
112n/a # ...
113n/a # "Called when the test case test succeeds"
114n/a # ...
115n/a # "wasSuccessful() - Returns True if all tests run so far have passed,
116n/a # otherwise returns False"
117n/a # ...
118n/a # "testsRun - The total number of tests run so far."
119n/a # ...
120n/a # "errors - A list containing 2-tuples of TestCase instances and
121n/a # formatted tracebacks. Each tuple represents a test which raised an
122n/a # unexpected exception. Contains formatted
123n/a # tracebacks instead of sys.exc_info() results."
124n/a # ...
125n/a # "failures - A list containing 2-tuples of TestCase instances and
126n/a # formatted tracebacks. Each tuple represents a test where a failure was
127n/a # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
128n/a # methods. Contains formatted tracebacks instead
129n/a # of sys.exc_info() results."
130n/a def test_addSuccess(self):
131n/a class Foo(unittest.TestCase):
132n/a def test_1(self):
133n/a pass
134n/a
135n/a test = Foo('test_1')
136n/a
137n/a result = unittest.TestResult()
138n/a
139n/a result.startTest(test)
140n/a result.addSuccess(test)
141n/a result.stopTest(test)
142n/a
143n/a self.assertTrue(result.wasSuccessful())
144n/a self.assertEqual(len(result.errors), 0)
145n/a self.assertEqual(len(result.failures), 0)
146n/a self.assertEqual(result.testsRun, 1)
147n/a self.assertEqual(result.shouldStop, False)
148n/a
149n/a # "addFailure(test, err)"
150n/a # ...
151n/a # "Called when the test case test signals a failure. err is a tuple of
152n/a # the form returned by sys.exc_info(): (type, value, traceback)"
153n/a # ...
154n/a # "wasSuccessful() - Returns True if all tests run so far have passed,
155n/a # otherwise returns False"
156n/a # ...
157n/a # "testsRun - The total number of tests run so far."
158n/a # ...
159n/a # "errors - A list containing 2-tuples of TestCase instances and
160n/a # formatted tracebacks. Each tuple represents a test which raised an
161n/a # unexpected exception. Contains formatted
162n/a # tracebacks instead of sys.exc_info() results."
163n/a # ...
164n/a # "failures - A list containing 2-tuples of TestCase instances and
165n/a # formatted tracebacks. Each tuple represents a test where a failure was
166n/a # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
167n/a # methods. Contains formatted tracebacks instead
168n/a # of sys.exc_info() results."
169n/a def test_addFailure(self):
170n/a class Foo(unittest.TestCase):
171n/a def test_1(self):
172n/a pass
173n/a
174n/a test = Foo('test_1')
175n/a try:
176n/a test.fail("foo")
177n/a except:
178n/a exc_info_tuple = sys.exc_info()
179n/a
180n/a result = unittest.TestResult()
181n/a
182n/a result.startTest(test)
183n/a result.addFailure(test, exc_info_tuple)
184n/a result.stopTest(test)
185n/a
186n/a self.assertFalse(result.wasSuccessful())
187n/a self.assertEqual(len(result.errors), 0)
188n/a self.assertEqual(len(result.failures), 1)
189n/a self.assertEqual(result.testsRun, 1)
190n/a self.assertEqual(result.shouldStop, False)
191n/a
192n/a test_case, formatted_exc = result.failures[0]
193n/a self.assertIs(test_case, test)
194n/a self.assertIsInstance(formatted_exc, str)
195n/a
196n/a # "addError(test, err)"
197n/a # ...
198n/a # "Called when the test case test raises an unexpected exception err
199n/a # is a tuple of the form returned by sys.exc_info():
200n/a # (type, value, traceback)"
201n/a # ...
202n/a # "wasSuccessful() - Returns True if all tests run so far have passed,
203n/a # otherwise returns False"
204n/a # ...
205n/a # "testsRun - The total number of tests run so far."
206n/a # ...
207n/a # "errors - A list containing 2-tuples of TestCase instances and
208n/a # formatted tracebacks. Each tuple represents a test which raised an
209n/a # unexpected exception. Contains formatted
210n/a # tracebacks instead of sys.exc_info() results."
211n/a # ...
212n/a # "failures - A list containing 2-tuples of TestCase instances and
213n/a # formatted tracebacks. Each tuple represents a test where a failure was
214n/a # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
215n/a # methods. Contains formatted tracebacks instead
216n/a # of sys.exc_info() results."
217n/a def test_addError(self):
218n/a class Foo(unittest.TestCase):
219n/a def test_1(self):
220n/a pass
221n/a
222n/a test = Foo('test_1')
223n/a try:
224n/a raise TypeError()
225n/a except:
226n/a exc_info_tuple = sys.exc_info()
227n/a
228n/a result = unittest.TestResult()
229n/a
230n/a result.startTest(test)
231n/a result.addError(test, exc_info_tuple)
232n/a result.stopTest(test)
233n/a
234n/a self.assertFalse(result.wasSuccessful())
235n/a self.assertEqual(len(result.errors), 1)
236n/a self.assertEqual(len(result.failures), 0)
237n/a self.assertEqual(result.testsRun, 1)
238n/a self.assertEqual(result.shouldStop, False)
239n/a
240n/a test_case, formatted_exc = result.errors[0]
241n/a self.assertIs(test_case, test)
242n/a self.assertIsInstance(formatted_exc, str)
243n/a
244n/a def test_addError_locals(self):
245n/a class Foo(unittest.TestCase):
246n/a def test_1(self):
247n/a 1/0
248n/a
249n/a test = Foo('test_1')
250n/a result = unittest.TestResult()
251n/a result.tb_locals = True
252n/a
253n/a unittest.result.traceback = MockTraceback
254n/a self.addCleanup(restore_traceback)
255n/a result.startTestRun()
256n/a test.run(result)
257n/a result.stopTestRun()
258n/a
259n/a self.assertEqual(len(result.errors), 1)
260n/a test_case, formatted_exc = result.errors[0]
261n/a self.assertEqual('A tracebacklocals', formatted_exc)
262n/a
263n/a def test_addSubTest(self):
264n/a class Foo(unittest.TestCase):
265n/a def test_1(self):
266n/a nonlocal subtest
267n/a with self.subTest(foo=1):
268n/a subtest = self._subtest
269n/a try:
270n/a 1/0
271n/a except ZeroDivisionError:
272n/a exc_info_tuple = sys.exc_info()
273n/a # Register an error by hand (to check the API)
274n/a result.addSubTest(test, subtest, exc_info_tuple)
275n/a # Now trigger a failure
276n/a self.fail("some recognizable failure")
277n/a
278n/a subtest = None
279n/a test = Foo('test_1')
280n/a result = unittest.TestResult()
281n/a
282n/a test.run(result)
283n/a
284n/a self.assertFalse(result.wasSuccessful())
285n/a self.assertEqual(len(result.errors), 1)
286n/a self.assertEqual(len(result.failures), 1)
287n/a self.assertEqual(result.testsRun, 1)
288n/a self.assertEqual(result.shouldStop, False)
289n/a
290n/a test_case, formatted_exc = result.errors[0]
291n/a self.assertIs(test_case, subtest)
292n/a self.assertIn("ZeroDivisionError", formatted_exc)
293n/a test_case, formatted_exc = result.failures[0]
294n/a self.assertIs(test_case, subtest)
295n/a self.assertIn("some recognizable failure", formatted_exc)
296n/a
297n/a def testGetDescriptionWithoutDocstring(self):
298n/a result = unittest.TextTestResult(None, True, 1)
299n/a self.assertEqual(
300n/a result.getDescription(self),
301n/a 'testGetDescriptionWithoutDocstring (' + __name__ +
302n/a '.Test_TestResult)')
303n/a
304n/a def testGetSubTestDescriptionWithoutDocstring(self):
305n/a with self.subTest(foo=1, bar=2):
306n/a result = unittest.TextTestResult(None, True, 1)
307n/a self.assertEqual(
308n/a result.getDescription(self._subtest),
309n/a 'testGetSubTestDescriptionWithoutDocstring (' + __name__ +
310n/a '.Test_TestResult) (bar=2, foo=1)')
311n/a with self.subTest('some message'):
312n/a result = unittest.TextTestResult(None, True, 1)
313n/a self.assertEqual(
314n/a result.getDescription(self._subtest),
315n/a 'testGetSubTestDescriptionWithoutDocstring (' + __name__ +
316n/a '.Test_TestResult) [some message]')
317n/a
318n/a def testGetSubTestDescriptionWithoutDocstringAndParams(self):
319n/a with self.subTest():
320n/a result = unittest.TextTestResult(None, True, 1)
321n/a self.assertEqual(
322n/a result.getDescription(self._subtest),
323n/a 'testGetSubTestDescriptionWithoutDocstringAndParams '
324n/a '(' + __name__ + '.Test_TestResult) (<subtest>)')
325n/a
326n/a def testGetSubTestDescriptionForFalsyValues(self):
327n/a expected = 'testGetSubTestDescriptionForFalsyValues (%s.Test_TestResult) [%s]'
328n/a result = unittest.TextTestResult(None, True, 1)
329n/a for arg in [0, None, []]:
330n/a with self.subTest(arg):
331n/a self.assertEqual(
332n/a result.getDescription(self._subtest),
333n/a expected % (__name__, arg)
334n/a )
335n/a
336n/a def testGetNestedSubTestDescriptionWithoutDocstring(self):
337n/a with self.subTest(foo=1):
338n/a with self.subTest(bar=2):
339n/a result = unittest.TextTestResult(None, True, 1)
340n/a self.assertEqual(
341n/a result.getDescription(self._subtest),
342n/a 'testGetNestedSubTestDescriptionWithoutDocstring '
343n/a '(' + __name__ + '.Test_TestResult) (bar=2, foo=1)')
344n/a
345n/a @unittest.skipIf(sys.flags.optimize >= 2,
346n/a "Docstrings are omitted with -O2 and above")
347n/a def testGetDescriptionWithOneLineDocstring(self):
348n/a """Tests getDescription() for a method with a docstring."""
349n/a result = unittest.TextTestResult(None, True, 1)
350n/a self.assertEqual(
351n/a result.getDescription(self),
352n/a ('testGetDescriptionWithOneLineDocstring '
353n/a '(' + __name__ + '.Test_TestResult)\n'
354n/a 'Tests getDescription() for a method with a docstring.'))
355n/a
356n/a @unittest.skipIf(sys.flags.optimize >= 2,
357n/a "Docstrings are omitted with -O2 and above")
358n/a def testGetSubTestDescriptionWithOneLineDocstring(self):
359n/a """Tests getDescription() for a method with a docstring."""
360n/a result = unittest.TextTestResult(None, True, 1)
361n/a with self.subTest(foo=1, bar=2):
362n/a self.assertEqual(
363n/a result.getDescription(self._subtest),
364n/a ('testGetSubTestDescriptionWithOneLineDocstring '
365n/a '(' + __name__ + '.Test_TestResult) (bar=2, foo=1)\n'
366n/a 'Tests getDescription() for a method with a docstring.'))
367n/a
368n/a @unittest.skipIf(sys.flags.optimize >= 2,
369n/a "Docstrings are omitted with -O2 and above")
370n/a def testGetDescriptionWithMultiLineDocstring(self):
371n/a """Tests getDescription() for a method with a longer docstring.
372n/a The second line of the docstring.
373n/a """
374n/a result = unittest.TextTestResult(None, True, 1)
375n/a self.assertEqual(
376n/a result.getDescription(self),
377n/a ('testGetDescriptionWithMultiLineDocstring '
378n/a '(' + __name__ + '.Test_TestResult)\n'
379n/a 'Tests getDescription() for a method with a longer '
380n/a 'docstring.'))
381n/a
382n/a @unittest.skipIf(sys.flags.optimize >= 2,
383n/a "Docstrings are omitted with -O2 and above")
384n/a def testGetSubTestDescriptionWithMultiLineDocstring(self):
385n/a """Tests getDescription() for a method with a longer docstring.
386n/a The second line of the docstring.
387n/a """
388n/a result = unittest.TextTestResult(None, True, 1)
389n/a with self.subTest(foo=1, bar=2):
390n/a self.assertEqual(
391n/a result.getDescription(self._subtest),
392n/a ('testGetSubTestDescriptionWithMultiLineDocstring '
393n/a '(' + __name__ + '.Test_TestResult) (bar=2, foo=1)\n'
394n/a 'Tests getDescription() for a method with a longer '
395n/a 'docstring.'))
396n/a
397n/a def testStackFrameTrimming(self):
398n/a class Frame(object):
399n/a class tb_frame(object):
400n/a f_globals = {}
401n/a result = unittest.TestResult()
402n/a self.assertFalse(result._is_relevant_tb_level(Frame))
403n/a
404n/a Frame.tb_frame.f_globals['__unittest'] = True
405n/a self.assertTrue(result._is_relevant_tb_level(Frame))
406n/a
407n/a def testFailFast(self):
408n/a result = unittest.TestResult()
409n/a result._exc_info_to_string = lambda *_: ''
410n/a result.failfast = True
411n/a result.addError(None, None)
412n/a self.assertTrue(result.shouldStop)
413n/a
414n/a result = unittest.TestResult()
415n/a result._exc_info_to_string = lambda *_: ''
416n/a result.failfast = True
417n/a result.addFailure(None, None)
418n/a self.assertTrue(result.shouldStop)
419n/a
420n/a result = unittest.TestResult()
421n/a result._exc_info_to_string = lambda *_: ''
422n/a result.failfast = True
423n/a result.addUnexpectedSuccess(None)
424n/a self.assertTrue(result.shouldStop)
425n/a
426n/a def testFailFastSetByRunner(self):
427n/a runner = unittest.TextTestRunner(stream=io.StringIO(), failfast=True)
428n/a def test(result):
429n/a self.assertTrue(result.failfast)
430n/a result = runner.run(test)
431n/a
432n/a
433n/aclassDict = dict(unittest.TestResult.__dict__)
434n/afor m in ('addSkip', 'addExpectedFailure', 'addUnexpectedSuccess',
435n/a '__init__'):
436n/a del classDict[m]
437n/a
438n/adef __init__(self, stream=None, descriptions=None, verbosity=None):
439n/a self.failures = []
440n/a self.errors = []
441n/a self.testsRun = 0
442n/a self.shouldStop = False
443n/a self.buffer = False
444n/a self.tb_locals = False
445n/a
446n/aclassDict['__init__'] = __init__
447n/aOldResult = type('OldResult', (object,), classDict)
448n/a
449n/aclass Test_OldTestResult(unittest.TestCase):
450n/a
451n/a def assertOldResultWarning(self, test, failures):
452n/a with support.check_warnings(("TestResult has no add.+ method,",
453n/a RuntimeWarning)):
454n/a result = OldResult()
455n/a test.run(result)
456n/a self.assertEqual(len(result.failures), failures)
457n/a
458n/a def testOldTestResult(self):
459n/a class Test(unittest.TestCase):
460n/a def testSkip(self):
461n/a self.skipTest('foobar')
462n/a @unittest.expectedFailure
463n/a def testExpectedFail(self):
464n/a raise TypeError
465n/a @unittest.expectedFailure
466n/a def testUnexpectedSuccess(self):
467n/a pass
468n/a
469n/a for test_name, should_pass in (('testSkip', True),
470n/a ('testExpectedFail', True),
471n/a ('testUnexpectedSuccess', False)):
472n/a test = Test(test_name)
473n/a self.assertOldResultWarning(test, int(not should_pass))
474n/a
475n/a def testOldTestTesultSetup(self):
476n/a class Test(unittest.TestCase):
477n/a def setUp(self):
478n/a self.skipTest('no reason')
479n/a def testFoo(self):
480n/a pass
481n/a self.assertOldResultWarning(Test('testFoo'), 0)
482n/a
483n/a def testOldTestResultClass(self):
484n/a @unittest.skip('no reason')
485n/a class Test(unittest.TestCase):
486n/a def testFoo(self):
487n/a pass
488n/a self.assertOldResultWarning(Test('testFoo'), 0)
489n/a
490n/a def testOldResultWithRunner(self):
491n/a class Test(unittest.TestCase):
492n/a def testFoo(self):
493n/a pass
494n/a runner = unittest.TextTestRunner(resultclass=OldResult,
495n/a stream=io.StringIO())
496n/a # This will raise an exception if TextTestRunner can't handle old
497n/a # test result objects
498n/a runner.run(Test('testFoo'))
499n/a
500n/a
501n/aclass TestOutputBuffering(unittest.TestCase):
502n/a
503n/a def setUp(self):
504n/a self._real_out = sys.stdout
505n/a self._real_err = sys.stderr
506n/a
507n/a def tearDown(self):
508n/a sys.stdout = self._real_out
509n/a sys.stderr = self._real_err
510n/a
511n/a def testBufferOutputOff(self):
512n/a real_out = self._real_out
513n/a real_err = self._real_err
514n/a
515n/a result = unittest.TestResult()
516n/a self.assertFalse(result.buffer)
517n/a
518n/a self.assertIs(real_out, sys.stdout)
519n/a self.assertIs(real_err, sys.stderr)
520n/a
521n/a result.startTest(self)
522n/a
523n/a self.assertIs(real_out, sys.stdout)
524n/a self.assertIs(real_err, sys.stderr)
525n/a
526n/a def testBufferOutputStartTestAddSuccess(self):
527n/a real_out = self._real_out
528n/a real_err = self._real_err
529n/a
530n/a result = unittest.TestResult()
531n/a self.assertFalse(result.buffer)
532n/a
533n/a result.buffer = True
534n/a
535n/a self.assertIs(real_out, sys.stdout)
536n/a self.assertIs(real_err, sys.stderr)
537n/a
538n/a result.startTest(self)
539n/a
540n/a self.assertIsNot(real_out, sys.stdout)
541n/a self.assertIsNot(real_err, sys.stderr)
542n/a self.assertIsInstance(sys.stdout, io.StringIO)
543n/a self.assertIsInstance(sys.stderr, io.StringIO)
544n/a self.assertIsNot(sys.stdout, sys.stderr)
545n/a
546n/a out_stream = sys.stdout
547n/a err_stream = sys.stderr
548n/a
549n/a result._original_stdout = io.StringIO()
550n/a result._original_stderr = io.StringIO()
551n/a
552n/a print('foo')
553n/a print('bar', file=sys.stderr)
554n/a
555n/a self.assertEqual(out_stream.getvalue(), 'foo\n')
556n/a self.assertEqual(err_stream.getvalue(), 'bar\n')
557n/a
558n/a self.assertEqual(result._original_stdout.getvalue(), '')
559n/a self.assertEqual(result._original_stderr.getvalue(), '')
560n/a
561n/a result.addSuccess(self)
562n/a result.stopTest(self)
563n/a
564n/a self.assertIs(sys.stdout, result._original_stdout)
565n/a self.assertIs(sys.stderr, result._original_stderr)
566n/a
567n/a self.assertEqual(result._original_stdout.getvalue(), '')
568n/a self.assertEqual(result._original_stderr.getvalue(), '')
569n/a
570n/a self.assertEqual(out_stream.getvalue(), '')
571n/a self.assertEqual(err_stream.getvalue(), '')
572n/a
573n/a
574n/a def getStartedResult(self):
575n/a result = unittest.TestResult()
576n/a result.buffer = True
577n/a result.startTest(self)
578n/a return result
579n/a
580n/a def testBufferOutputAddErrorOrFailure(self):
581n/a unittest.result.traceback = MockTraceback
582n/a self.addCleanup(restore_traceback)
583n/a
584n/a for message_attr, add_attr, include_error in [
585n/a ('errors', 'addError', True),
586n/a ('failures', 'addFailure', False),
587n/a ('errors', 'addError', True),
588n/a ('failures', 'addFailure', False)
589n/a ]:
590n/a result = self.getStartedResult()
591n/a buffered_out = sys.stdout
592n/a buffered_err = sys.stderr
593n/a result._original_stdout = io.StringIO()
594n/a result._original_stderr = io.StringIO()
595n/a
596n/a print('foo', file=sys.stdout)
597n/a if include_error:
598n/a print('bar', file=sys.stderr)
599n/a
600n/a
601n/a addFunction = getattr(result, add_attr)
602n/a addFunction(self, (None, None, None))
603n/a result.stopTest(self)
604n/a
605n/a result_list = getattr(result, message_attr)
606n/a self.assertEqual(len(result_list), 1)
607n/a
608n/a test, message = result_list[0]
609n/a expectedOutMessage = textwrap.dedent("""
610n/a Stdout:
611n/a foo
612n/a """)
613n/a expectedErrMessage = ''
614n/a if include_error:
615n/a expectedErrMessage = textwrap.dedent("""
616n/a Stderr:
617n/a bar
618n/a """)
619n/a
620n/a expectedFullMessage = 'A traceback%s%s' % (expectedOutMessage, expectedErrMessage)
621n/a
622n/a self.assertIs(test, self)
623n/a self.assertEqual(result._original_stdout.getvalue(), expectedOutMessage)
624n/a self.assertEqual(result._original_stderr.getvalue(), expectedErrMessage)
625n/a self.assertMultiLineEqual(message, expectedFullMessage)
626n/a
627n/a def testBufferSetupClass(self):
628n/a result = unittest.TestResult()
629n/a result.buffer = True
630n/a
631n/a class Foo(unittest.TestCase):
632n/a @classmethod
633n/a def setUpClass(cls):
634n/a 1/0
635n/a def test_foo(self):
636n/a pass
637n/a suite = unittest.TestSuite([Foo('test_foo')])
638n/a suite(result)
639n/a self.assertEqual(len(result.errors), 1)
640n/a
641n/a def testBufferTearDownClass(self):
642n/a result = unittest.TestResult()
643n/a result.buffer = True
644n/a
645n/a class Foo(unittest.TestCase):
646n/a @classmethod
647n/a def tearDownClass(cls):
648n/a 1/0
649n/a def test_foo(self):
650n/a pass
651n/a suite = unittest.TestSuite([Foo('test_foo')])
652n/a suite(result)
653n/a self.assertEqual(len(result.errors), 1)
654n/a
655n/a def testBufferSetUpModule(self):
656n/a result = unittest.TestResult()
657n/a result.buffer = True
658n/a
659n/a class Foo(unittest.TestCase):
660n/a def test_foo(self):
661n/a pass
662n/a class Module(object):
663n/a @staticmethod
664n/a def setUpModule():
665n/a 1/0
666n/a
667n/a Foo.__module__ = 'Module'
668n/a sys.modules['Module'] = Module
669n/a self.addCleanup(sys.modules.pop, 'Module')
670n/a suite = unittest.TestSuite([Foo('test_foo')])
671n/a suite(result)
672n/a self.assertEqual(len(result.errors), 1)
673n/a
674n/a def testBufferTearDownModule(self):
675n/a result = unittest.TestResult()
676n/a result.buffer = True
677n/a
678n/a class Foo(unittest.TestCase):
679n/a def test_foo(self):
680n/a pass
681n/a class Module(object):
682n/a @staticmethod
683n/a def tearDownModule():
684n/a 1/0
685n/a
686n/a Foo.__module__ = 'Module'
687n/a sys.modules['Module'] = Module
688n/a self.addCleanup(sys.modules.pop, 'Module')
689n/a suite = unittest.TestSuite([Foo('test_foo')])
690n/a suite(result)
691n/a self.assertEqual(len(result.errors), 1)
692n/a
693n/a
694n/aif __name__ == '__main__':
695n/a unittest.main()