test_result.py 24 KB

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