2010-03-27 09:34:21 -03:00
|
|
|
import io
|
2010-11-30 20:56:10 -04:00
|
|
|
import os
|
|
|
|
import sys
|
2010-03-27 09:34:21 -03:00
|
|
|
import pickle
|
2010-11-30 20:56:10 -04:00
|
|
|
import subprocess
|
2022-03-22 07:04:36 -03:00
|
|
|
from test import support
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
import unittest
|
2013-03-20 16:16:47 -03:00
|
|
|
from unittest.case import _Outcome
|
2010-03-27 09:34:21 -03:00
|
|
|
|
2023-04-02 19:12:51 -03:00
|
|
|
from test.test_unittest.support import (
|
|
|
|
BufferedWriter,
|
|
|
|
LoggingResult,
|
|
|
|
ResultWithNoStartTestRunStopTestRun,
|
|
|
|
)
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
|
2018-11-08 22:34:33 -04:00
|
|
|
def resultFactory(*_):
|
|
|
|
return unittest.TestResult()
|
|
|
|
|
|
|
|
|
|
|
|
def getRunner():
|
|
|
|
return unittest.TextTestRunner(resultclass=resultFactory,
|
|
|
|
stream=io.StringIO())
|
|
|
|
|
|
|
|
|
|
|
|
def runTests(*cases):
|
|
|
|
suite = unittest.TestSuite()
|
|
|
|
for case in cases:
|
|
|
|
tests = unittest.defaultTestLoader.loadTestsFromTestCase(case)
|
|
|
|
suite.addTests(tests)
|
|
|
|
|
|
|
|
runner = getRunner()
|
|
|
|
|
|
|
|
# creating a nested suite exposes some potential bugs
|
|
|
|
realSuite = unittest.TestSuite()
|
|
|
|
realSuite.addTest(suite)
|
|
|
|
# adding empty suites to the end exposes potential bugs
|
|
|
|
suite.addTest(unittest.TestSuite())
|
|
|
|
realSuite.addTest(unittest.TestSuite())
|
|
|
|
return runner.run(realSuite)
|
|
|
|
|
|
|
|
|
|
|
|
def cleanup(ordering, blowUp=False):
|
|
|
|
if not blowUp:
|
|
|
|
ordering.append('cleanup_good')
|
|
|
|
else:
|
|
|
|
ordering.append('cleanup_exc')
|
|
|
|
raise Exception('CleanUpExc')
|
|
|
|
|
2010-03-27 09:34:21 -03:00
|
|
|
|
2022-05-08 11:49:09 -03:00
|
|
|
class TestCM:
|
|
|
|
def __init__(self, ordering, enter_result=None):
|
|
|
|
self.ordering = ordering
|
|
|
|
self.enter_result = enter_result
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.ordering.append('enter')
|
|
|
|
return self.enter_result
|
|
|
|
|
|
|
|
def __exit__(self, *exc_info):
|
|
|
|
self.ordering.append('exit')
|
|
|
|
|
|
|
|
|
|
|
|
class LacksEnterAndExit:
|
|
|
|
pass
|
|
|
|
class LacksEnter:
|
|
|
|
def __exit__(self, *exc_info):
|
|
|
|
pass
|
|
|
|
class LacksExit:
|
|
|
|
def __enter__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-11-08 22:34:33 -04:00
|
|
|
class TestCleanUp(unittest.TestCase):
|
2010-03-27 09:34:21 -03:00
|
|
|
def testCleanUp(self):
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def testNothing(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
test = TestableTest('testNothing')
|
|
|
|
self.assertEqual(test._cleanups, [])
|
|
|
|
|
|
|
|
cleanups = []
|
|
|
|
|
|
|
|
def cleanup1(*args, **kwargs):
|
|
|
|
cleanups.append((1, args, kwargs))
|
|
|
|
|
|
|
|
def cleanup2(*args, **kwargs):
|
|
|
|
cleanups.append((2, args, kwargs))
|
|
|
|
|
|
|
|
test.addCleanup(cleanup1, 1, 2, 3, four='hello', five='goodbye')
|
|
|
|
test.addCleanup(cleanup2)
|
|
|
|
|
|
|
|
self.assertEqual(test._cleanups,
|
|
|
|
[(cleanup1, (1, 2, 3), dict(four='hello', five='goodbye')),
|
|
|
|
(cleanup2, (), {})])
|
|
|
|
|
2010-12-18 23:19:47 -04:00
|
|
|
self.assertTrue(test.doCleanups())
|
2010-03-27 09:34:21 -03:00
|
|
|
self.assertEqual(cleanups, [(2, (), {}), (1, (1, 2, 3), dict(four='hello', five='goodbye'))])
|
|
|
|
|
|
|
|
def testCleanUpWithErrors(self):
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def testNothing(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
test = TestableTest('testNothing')
|
2021-09-19 09:24:38 -03:00
|
|
|
result = unittest.TestResult()
|
|
|
|
outcome = test._outcome = _Outcome(result=result)
|
2010-03-27 09:34:21 -03:00
|
|
|
|
2018-11-08 22:34:33 -04:00
|
|
|
CleanUpExc = Exception('foo')
|
2010-03-27 09:34:21 -03:00
|
|
|
exc2 = Exception('bar')
|
|
|
|
def cleanup1():
|
2018-11-08 22:34:33 -04:00
|
|
|
raise CleanUpExc
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
def cleanup2():
|
|
|
|
raise exc2
|
|
|
|
|
|
|
|
test.addCleanup(cleanup1)
|
|
|
|
test.addCleanup(cleanup2)
|
|
|
|
|
|
|
|
self.assertFalse(test.doCleanups())
|
2013-03-20 16:16:47 -03:00
|
|
|
self.assertFalse(outcome.success)
|
2010-03-27 09:34:21 -03:00
|
|
|
|
2021-09-19 09:24:38 -03:00
|
|
|
(_, msg2), (_, msg1) = result.errors
|
|
|
|
self.assertIn('in cleanup1', msg1)
|
|
|
|
self.assertIn('raise CleanUpExc', msg1)
|
|
|
|
self.assertIn('Exception: foo', msg1)
|
|
|
|
self.assertIn('in cleanup2', msg2)
|
|
|
|
self.assertIn('raise exc2', msg2)
|
|
|
|
self.assertIn('Exception: bar', msg2)
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
def testCleanupInRun(self):
|
|
|
|
blowUp = False
|
|
|
|
ordering = []
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
ordering.append('setUp')
|
2022-11-21 07:57:30 -04:00
|
|
|
test.addCleanup(cleanup2)
|
2010-03-27 09:34:21 -03:00
|
|
|
if blowUp:
|
|
|
|
raise Exception('foo')
|
|
|
|
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
2022-11-21 07:57:30 -04:00
|
|
|
test.addCleanup(cleanup3)
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
ordering.append('tearDown')
|
|
|
|
|
|
|
|
test = TestableTest('testNothing')
|
|
|
|
|
|
|
|
def cleanup1():
|
|
|
|
ordering.append('cleanup1')
|
|
|
|
def cleanup2():
|
|
|
|
ordering.append('cleanup2')
|
2022-11-21 07:57:30 -04:00
|
|
|
def cleanup3():
|
|
|
|
ordering.append('cleanup3')
|
2010-03-27 09:34:21 -03:00
|
|
|
test.addCleanup(cleanup1)
|
|
|
|
|
|
|
|
def success(some_test):
|
|
|
|
self.assertEqual(some_test, test)
|
|
|
|
ordering.append('success')
|
|
|
|
|
|
|
|
result = unittest.TestResult()
|
|
|
|
result.addSuccess = success
|
|
|
|
|
|
|
|
test.run(result)
|
2022-11-21 07:57:30 -04:00
|
|
|
self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup3',
|
2010-03-27 09:34:21 -03:00
|
|
|
'cleanup2', 'cleanup1', 'success'])
|
|
|
|
|
|
|
|
blowUp = True
|
|
|
|
ordering = []
|
|
|
|
test = TestableTest('testNothing')
|
|
|
|
test.addCleanup(cleanup1)
|
|
|
|
test.run(result)
|
2022-11-21 07:57:30 -04:00
|
|
|
self.assertEqual(ordering, ['setUp', 'cleanup2', 'cleanup1'])
|
2010-03-27 09:34:21 -03:00
|
|
|
|
2010-06-10 13:16:08 -03:00
|
|
|
def testTestCaseDebugExecutesCleanups(self):
|
|
|
|
ordering = []
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
ordering.append('setUp')
|
|
|
|
self.addCleanup(cleanup1)
|
|
|
|
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
2022-11-21 07:57:30 -04:00
|
|
|
self.addCleanup(cleanup3)
|
2010-06-10 13:16:08 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
ordering.append('tearDown')
|
2022-11-21 07:57:30 -04:00
|
|
|
test.addCleanup(cleanup4)
|
2010-06-10 13:16:08 -03:00
|
|
|
|
|
|
|
test = TestableTest('testNothing')
|
|
|
|
|
|
|
|
def cleanup1():
|
|
|
|
ordering.append('cleanup1')
|
|
|
|
test.addCleanup(cleanup2)
|
|
|
|
def cleanup2():
|
|
|
|
ordering.append('cleanup2')
|
2022-11-21 07:57:30 -04:00
|
|
|
def cleanup3():
|
|
|
|
ordering.append('cleanup3')
|
|
|
|
def cleanup4():
|
|
|
|
ordering.append('cleanup4')
|
2010-06-10 13:16:08 -03:00
|
|
|
|
|
|
|
test.debug()
|
2022-11-21 07:57:30 -04:00
|
|
|
self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup4',
|
|
|
|
'cleanup3', 'cleanup1', 'cleanup2'])
|
2018-11-08 22:34:33 -04:00
|
|
|
|
|
|
|
|
2022-05-08 11:49:09 -03:00
|
|
|
def test_enterContext(self):
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def testNothing(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
test = TestableTest('testNothing')
|
|
|
|
cleanups = []
|
|
|
|
|
|
|
|
test.addCleanup(cleanups.append, 'cleanup1')
|
|
|
|
cm = TestCM(cleanups, 42)
|
|
|
|
self.assertEqual(test.enterContext(cm), 42)
|
|
|
|
test.addCleanup(cleanups.append, 'cleanup2')
|
|
|
|
|
|
|
|
self.assertTrue(test.doCleanups())
|
|
|
|
self.assertEqual(cleanups, ['enter', 'cleanup2', 'exit', 'cleanup1'])
|
|
|
|
|
|
|
|
def test_enterContext_arg_errors(self):
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def testNothing(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
test = TestableTest('testNothing')
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(TypeError, 'the context manager'):
|
|
|
|
test.enterContext(LacksEnterAndExit())
|
|
|
|
with self.assertRaisesRegex(TypeError, 'the context manager'):
|
|
|
|
test.enterContext(LacksEnter())
|
|
|
|
with self.assertRaisesRegex(TypeError, 'the context manager'):
|
|
|
|
test.enterContext(LacksExit())
|
|
|
|
|
|
|
|
self.assertEqual(test._cleanups, [])
|
|
|
|
|
|
|
|
|
2018-11-08 22:34:33 -04:00
|
|
|
class TestClassCleanup(unittest.TestCase):
|
|
|
|
def test_addClassCleanUp(self):
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def testNothing(self):
|
|
|
|
pass
|
|
|
|
test = TestableTest('testNothing')
|
|
|
|
self.assertEqual(test._class_cleanups, [])
|
|
|
|
class_cleanups = []
|
|
|
|
|
|
|
|
def class_cleanup1(*args, **kwargs):
|
|
|
|
class_cleanups.append((3, args, kwargs))
|
|
|
|
|
|
|
|
def class_cleanup2(*args, **kwargs):
|
|
|
|
class_cleanups.append((4, args, kwargs))
|
|
|
|
|
|
|
|
TestableTest.addClassCleanup(class_cleanup1, 1, 2, 3,
|
|
|
|
four='hello', five='goodbye')
|
|
|
|
TestableTest.addClassCleanup(class_cleanup2)
|
|
|
|
|
|
|
|
self.assertEqual(test._class_cleanups,
|
|
|
|
[(class_cleanup1, (1, 2, 3),
|
|
|
|
dict(four='hello', five='goodbye')),
|
|
|
|
(class_cleanup2, (), {})])
|
|
|
|
|
|
|
|
TestableTest.doClassCleanups()
|
|
|
|
self.assertEqual(class_cleanups, [(4, (), {}), (3, (1, 2, 3),
|
|
|
|
dict(four='hello', five='goodbye'))])
|
|
|
|
|
|
|
|
def test_run_class_cleanUp(self):
|
|
|
|
ordering = []
|
|
|
|
blowUp = True
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
cls.addClassCleanup(cleanup, ordering)
|
|
|
|
if blowUp:
|
|
|
|
raise Exception()
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
|
|
|
|
runTests(TestableTest)
|
|
|
|
self.assertEqual(ordering, ['setUpClass', 'cleanup_good'])
|
|
|
|
|
|
|
|
ordering = []
|
|
|
|
blowUp = False
|
|
|
|
runTests(TestableTest)
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpClass', 'test', 'tearDownClass', 'cleanup_good'])
|
|
|
|
|
2021-08-30 13:25:59 -03:00
|
|
|
def test_run_class_cleanUp_without_tearDownClass(self):
|
2018-11-08 22:34:33 -04:00
|
|
|
ordering = []
|
2021-08-30 13:25:59 -03:00
|
|
|
blowUp = True
|
2018-11-08 22:34:33 -04:00
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
cls.addClassCleanup(cleanup, ordering)
|
2021-08-30 13:25:59 -03:00
|
|
|
if blowUp:
|
|
|
|
raise Exception()
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
@property
|
|
|
|
def tearDownClass(cls):
|
|
|
|
raise AttributeError
|
|
|
|
|
|
|
|
runTests(TestableTest)
|
|
|
|
self.assertEqual(ordering, ['setUpClass', 'cleanup_good'])
|
|
|
|
|
|
|
|
ordering = []
|
|
|
|
blowUp = False
|
|
|
|
runTests(TestableTest)
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpClass', 'test', 'cleanup_good'])
|
|
|
|
|
|
|
|
def test_debug_executes_classCleanUp(self):
|
|
|
|
ordering = []
|
|
|
|
blowUp = False
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
cls.addClassCleanup(cleanup, ordering, blowUp=blowUp)
|
2018-11-08 22:34:33 -04:00
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
|
|
|
|
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
|
|
|
|
suite.debug()
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpClass', 'test', 'tearDownClass', 'cleanup_good'])
|
|
|
|
|
2021-08-30 13:25:59 -03:00
|
|
|
ordering = []
|
|
|
|
blowUp = True
|
|
|
|
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
|
|
|
|
with self.assertRaises(Exception) as cm:
|
|
|
|
suite.debug()
|
|
|
|
self.assertEqual(str(cm.exception), 'CleanUpExc')
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpClass', 'test', 'tearDownClass', 'cleanup_exc'])
|
|
|
|
|
|
|
|
def test_debug_executes_classCleanUp_when_teardown_exception(self):
|
|
|
|
ordering = []
|
|
|
|
blowUp = False
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
cls.addClassCleanup(cleanup, ordering, blowUp=blowUp)
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2022-11-21 07:57:30 -04:00
|
|
|
ordering.append('tearDownClass')
|
2021-08-30 13:25:59 -03:00
|
|
|
raise Exception('TearDownClassExc')
|
|
|
|
|
|
|
|
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
|
|
|
|
with self.assertRaises(Exception) as cm:
|
|
|
|
suite.debug()
|
|
|
|
self.assertEqual(str(cm.exception), 'TearDownClassExc')
|
2022-11-21 07:57:30 -04:00
|
|
|
self.assertEqual(ordering, ['setUpClass', 'test', 'tearDownClass'])
|
2021-08-30 13:25:59 -03:00
|
|
|
self.assertTrue(TestableTest._class_cleanups)
|
|
|
|
TestableTest._class_cleanups.clear()
|
|
|
|
|
|
|
|
ordering = []
|
|
|
|
blowUp = True
|
|
|
|
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
|
|
|
|
with self.assertRaises(Exception) as cm:
|
|
|
|
suite.debug()
|
|
|
|
self.assertEqual(str(cm.exception), 'TearDownClassExc')
|
2022-11-21 07:57:30 -04:00
|
|
|
self.assertEqual(ordering, ['setUpClass', 'test', 'tearDownClass'])
|
2021-08-30 13:25:59 -03:00
|
|
|
self.assertTrue(TestableTest._class_cleanups)
|
|
|
|
TestableTest._class_cleanups.clear()
|
|
|
|
|
2018-11-08 22:34:33 -04:00
|
|
|
def test_doClassCleanups_with_errors_addClassCleanUp(self):
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def testNothing(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def cleanup1():
|
|
|
|
raise Exception('cleanup1')
|
|
|
|
|
|
|
|
def cleanup2():
|
|
|
|
raise Exception('cleanup2')
|
|
|
|
|
|
|
|
TestableTest.addClassCleanup(cleanup1)
|
|
|
|
TestableTest.addClassCleanup(cleanup2)
|
|
|
|
with self.assertRaises(Exception) as e:
|
|
|
|
TestableTest.doClassCleanups()
|
2018-11-29 09:07:00 -04:00
|
|
|
self.assertEqual(e, 'cleanup1')
|
2018-11-08 22:34:33 -04:00
|
|
|
|
|
|
|
def test_with_errors_addCleanUp(self):
|
|
|
|
ordering = []
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
cls.addClassCleanup(cleanup, ordering)
|
|
|
|
def setUp(self):
|
|
|
|
ordering.append('setUp')
|
|
|
|
self.addCleanup(cleanup, ordering, blowUp=True)
|
|
|
|
def testNothing(self):
|
|
|
|
pass
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: CleanUpExc')
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpClass', 'setUp', 'cleanup_exc',
|
|
|
|
'tearDownClass', 'cleanup_good'])
|
|
|
|
|
|
|
|
def test_run_with_errors_addClassCleanUp(self):
|
|
|
|
ordering = []
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
cls.addClassCleanup(cleanup, ordering, blowUp=True)
|
|
|
|
def setUp(self):
|
|
|
|
ordering.append('setUp')
|
|
|
|
self.addCleanup(cleanup, ordering)
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: CleanUpExc')
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpClass', 'setUp', 'test', 'cleanup_good',
|
|
|
|
'tearDownClass', 'cleanup_exc'])
|
|
|
|
|
|
|
|
def test_with_errors_in_addClassCleanup_and_setUps(self):
|
|
|
|
ordering = []
|
|
|
|
class_blow_up = False
|
|
|
|
method_blow_up = False
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
cls.addClassCleanup(cleanup, ordering, blowUp=True)
|
|
|
|
if class_blow_up:
|
|
|
|
raise Exception('ClassExc')
|
|
|
|
def setUp(self):
|
|
|
|
ordering.append('setUp')
|
|
|
|
if method_blow_up:
|
|
|
|
raise Exception('MethodExc')
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: CleanUpExc')
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpClass', 'setUp', 'test',
|
|
|
|
'tearDownClass', 'cleanup_exc'])
|
2021-08-30 13:25:59 -03:00
|
|
|
|
2018-11-08 22:34:33 -04:00
|
|
|
ordering = []
|
|
|
|
class_blow_up = True
|
|
|
|
method_blow_up = False
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: ClassExc')
|
|
|
|
self.assertEqual(result.errors[1][1].splitlines()[-1],
|
|
|
|
'Exception: CleanUpExc')
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpClass', 'cleanup_exc'])
|
|
|
|
|
|
|
|
ordering = []
|
|
|
|
class_blow_up = False
|
|
|
|
method_blow_up = True
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: MethodExc')
|
|
|
|
self.assertEqual(result.errors[1][1].splitlines()[-1],
|
|
|
|
'Exception: CleanUpExc')
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpClass', 'setUp', 'tearDownClass',
|
|
|
|
'cleanup_exc'])
|
|
|
|
|
2021-08-30 13:25:59 -03:00
|
|
|
def test_with_errors_in_tearDownClass(self):
|
|
|
|
ordering = []
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
cls.addClassCleanup(cleanup, ordering)
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
raise Exception('TearDownExc')
|
|
|
|
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: TearDownExc')
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpClass', 'test', 'tearDownClass', 'cleanup_good'])
|
|
|
|
|
2022-05-08 11:49:09 -03:00
|
|
|
def test_enterClassContext(self):
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def testNothing(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
cleanups = []
|
|
|
|
|
|
|
|
TestableTest.addClassCleanup(cleanups.append, 'cleanup1')
|
|
|
|
cm = TestCM(cleanups, 42)
|
|
|
|
self.assertEqual(TestableTest.enterClassContext(cm), 42)
|
|
|
|
TestableTest.addClassCleanup(cleanups.append, 'cleanup2')
|
|
|
|
|
|
|
|
TestableTest.doClassCleanups()
|
|
|
|
self.assertEqual(cleanups, ['enter', 'cleanup2', 'exit', 'cleanup1'])
|
|
|
|
|
|
|
|
def test_enterClassContext_arg_errors(self):
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def testNothing(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(TypeError, 'the context manager'):
|
|
|
|
TestableTest.enterClassContext(LacksEnterAndExit())
|
|
|
|
with self.assertRaisesRegex(TypeError, 'the context manager'):
|
|
|
|
TestableTest.enterClassContext(LacksEnter())
|
|
|
|
with self.assertRaisesRegex(TypeError, 'the context manager'):
|
|
|
|
TestableTest.enterClassContext(LacksExit())
|
|
|
|
|
|
|
|
self.assertEqual(TestableTest._class_cleanups, [])
|
|
|
|
|
2022-11-22 11:49:37 -04:00
|
|
|
def test_run_nested_test(self):
|
|
|
|
ordering = []
|
|
|
|
|
|
|
|
class InnerTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('inner setup')
|
|
|
|
cls.addClassCleanup(ordering.append, 'inner cleanup')
|
|
|
|
def test(self):
|
|
|
|
ordering.append('inner test')
|
|
|
|
|
|
|
|
class OuterTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('outer setup')
|
|
|
|
cls.addClassCleanup(ordering.append, 'outer cleanup')
|
|
|
|
def test(self):
|
|
|
|
ordering.append('start outer test')
|
|
|
|
runTests(InnerTest)
|
|
|
|
ordering.append('end outer test')
|
|
|
|
|
|
|
|
runTests(OuterTest)
|
|
|
|
self.assertEqual(ordering, [
|
|
|
|
'outer setup', 'start outer test',
|
|
|
|
'inner setup', 'inner test', 'inner cleanup',
|
|
|
|
'end outer test', 'outer cleanup'])
|
|
|
|
|
2023-04-26 22:28:46 -03:00
|
|
|
def test_run_empty_suite_error_message(self):
|
|
|
|
class EmptyTest(unittest.TestCase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
suite = unittest.defaultTestLoader.loadTestsFromTestCase(EmptyTest)
|
|
|
|
runner = getRunner()
|
|
|
|
runner.run(suite)
|
|
|
|
|
|
|
|
self.assertIn("\nNO TESTS RAN\n", runner.stream.getvalue())
|
|
|
|
|
2018-11-08 22:34:33 -04:00
|
|
|
|
|
|
|
class TestModuleCleanUp(unittest.TestCase):
|
|
|
|
def test_add_and_do_ModuleCleanup(self):
|
|
|
|
module_cleanups = []
|
|
|
|
|
|
|
|
def module_cleanup1(*args, **kwargs):
|
|
|
|
module_cleanups.append((3, args, kwargs))
|
|
|
|
|
|
|
|
def module_cleanup2(*args, **kwargs):
|
|
|
|
module_cleanups.append((4, args, kwargs))
|
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
unittest.addModuleCleanup(module_cleanup1, 1, 2, 3,
|
|
|
|
four='hello', five='goodbye')
|
|
|
|
unittest.addModuleCleanup(module_cleanup2)
|
|
|
|
|
|
|
|
self.assertEqual(unittest.case._module_cleanups,
|
|
|
|
[(module_cleanup1, (1, 2, 3),
|
|
|
|
dict(four='hello', five='goodbye')),
|
|
|
|
(module_cleanup2, (), {})])
|
|
|
|
|
|
|
|
unittest.case.doModuleCleanups()
|
|
|
|
self.assertEqual(module_cleanups, [(4, (), {}), (3, (1, 2, 3),
|
|
|
|
dict(four='hello', five='goodbye'))])
|
|
|
|
self.assertEqual(unittest.case._module_cleanups, [])
|
|
|
|
|
|
|
|
def test_doModuleCleanup_with_errors_in_addModuleCleanup(self):
|
|
|
|
module_cleanups = []
|
|
|
|
|
|
|
|
def module_cleanup_good(*args, **kwargs):
|
|
|
|
module_cleanups.append((3, args, kwargs))
|
|
|
|
|
|
|
|
def module_cleanup_bad(*args, **kwargs):
|
|
|
|
raise Exception('CleanUpExc')
|
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
unittest.addModuleCleanup(module_cleanup_good, 1, 2, 3,
|
|
|
|
four='hello', five='goodbye')
|
|
|
|
unittest.addModuleCleanup(module_cleanup_bad)
|
|
|
|
self.assertEqual(unittest.case._module_cleanups,
|
|
|
|
[(module_cleanup_good, (1, 2, 3),
|
|
|
|
dict(four='hello', five='goodbye')),
|
|
|
|
(module_cleanup_bad, (), {})])
|
|
|
|
with self.assertRaises(Exception) as e:
|
|
|
|
unittest.case.doModuleCleanups()
|
|
|
|
self.assertEqual(str(e.exception), 'CleanUpExc')
|
|
|
|
self.assertEqual(unittest.case._module_cleanups, [])
|
|
|
|
|
2019-04-01 03:16:35 -03:00
|
|
|
def test_addModuleCleanup_arg_errors(self):
|
|
|
|
cleanups = []
|
|
|
|
def cleanup(*args, **kwargs):
|
|
|
|
cleanups.append((args, kwargs))
|
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
unittest.addModuleCleanup(cleanup, 1, 2, function='hello')
|
2019-06-01 05:00:15 -03:00
|
|
|
with self.assertRaises(TypeError):
|
2019-04-01 03:16:35 -03:00
|
|
|
unittest.addModuleCleanup(function=cleanup, arg='hello')
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
unittest.addModuleCleanup()
|
|
|
|
unittest.case.doModuleCleanups()
|
|
|
|
self.assertEqual(cleanups,
|
2019-06-01 05:00:15 -03:00
|
|
|
[((1, 2), {'function': 'hello'})])
|
2019-04-01 03:16:35 -03:00
|
|
|
|
2018-11-08 22:34:33 -04:00
|
|
|
def test_run_module_cleanUp(self):
|
|
|
|
blowUp = True
|
|
|
|
ordering = []
|
|
|
|
class Module(object):
|
|
|
|
@staticmethod
|
|
|
|
def setUpModule():
|
|
|
|
ordering.append('setUpModule')
|
|
|
|
unittest.addModuleCleanup(cleanup, ordering)
|
|
|
|
if blowUp:
|
|
|
|
raise Exception('setUpModule Exc')
|
|
|
|
@staticmethod
|
|
|
|
def tearDownModule():
|
|
|
|
ordering.append('tearDownModule')
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
|
|
|
|
TestableTest.__module__ = 'Module'
|
|
|
|
sys.modules['Module'] = Module
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(ordering, ['setUpModule', 'cleanup_good'])
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: setUpModule Exc')
|
|
|
|
|
|
|
|
ordering = []
|
|
|
|
blowUp = False
|
|
|
|
runTests(TestableTest)
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpModule', 'setUpClass', 'test', 'tearDownClass',
|
|
|
|
'tearDownModule', 'cleanup_good'])
|
|
|
|
self.assertEqual(unittest.case._module_cleanups, [])
|
|
|
|
|
|
|
|
def test_run_multiple_module_cleanUp(self):
|
|
|
|
blowUp = True
|
|
|
|
blowUp2 = False
|
|
|
|
ordering = []
|
|
|
|
class Module1(object):
|
|
|
|
@staticmethod
|
|
|
|
def setUpModule():
|
|
|
|
ordering.append('setUpModule')
|
|
|
|
unittest.addModuleCleanup(cleanup, ordering)
|
|
|
|
if blowUp:
|
|
|
|
raise Exception()
|
|
|
|
@staticmethod
|
|
|
|
def tearDownModule():
|
|
|
|
ordering.append('tearDownModule')
|
|
|
|
|
|
|
|
class Module2(object):
|
|
|
|
@staticmethod
|
|
|
|
def setUpModule():
|
|
|
|
ordering.append('setUpModule2')
|
|
|
|
unittest.addModuleCleanup(cleanup, ordering)
|
|
|
|
if blowUp2:
|
|
|
|
raise Exception()
|
|
|
|
@staticmethod
|
|
|
|
def tearDownModule():
|
|
|
|
ordering.append('tearDownModule2')
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
|
|
|
|
class TestableTest2(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass2')
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test2')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass2')
|
|
|
|
|
|
|
|
TestableTest.__module__ = 'Module1'
|
|
|
|
sys.modules['Module1'] = Module1
|
|
|
|
TestableTest2.__module__ = 'Module2'
|
|
|
|
sys.modules['Module2'] = Module2
|
|
|
|
runTests(TestableTest, TestableTest2)
|
|
|
|
self.assertEqual(ordering, ['setUpModule', 'cleanup_good',
|
|
|
|
'setUpModule2', 'setUpClass2', 'test2',
|
|
|
|
'tearDownClass2', 'tearDownModule2',
|
|
|
|
'cleanup_good'])
|
|
|
|
ordering = []
|
|
|
|
blowUp = False
|
|
|
|
blowUp2 = True
|
|
|
|
runTests(TestableTest, TestableTest2)
|
|
|
|
self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
|
|
|
|
'tearDownClass', 'tearDownModule',
|
|
|
|
'cleanup_good', 'setUpModule2',
|
|
|
|
'cleanup_good'])
|
|
|
|
|
|
|
|
ordering = []
|
|
|
|
blowUp = False
|
|
|
|
blowUp2 = False
|
|
|
|
runTests(TestableTest, TestableTest2)
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpModule', 'setUpClass', 'test', 'tearDownClass',
|
|
|
|
'tearDownModule', 'cleanup_good', 'setUpModule2',
|
|
|
|
'setUpClass2', 'test2', 'tearDownClass2',
|
|
|
|
'tearDownModule2', 'cleanup_good'])
|
|
|
|
self.assertEqual(unittest.case._module_cleanups, [])
|
|
|
|
|
2021-08-30 13:25:59 -03:00
|
|
|
def test_run_module_cleanUp_without_teardown(self):
|
2018-11-08 22:34:33 -04:00
|
|
|
ordering = []
|
|
|
|
class Module(object):
|
|
|
|
@staticmethod
|
|
|
|
def setUpModule():
|
|
|
|
ordering.append('setUpModule')
|
|
|
|
unittest.addModuleCleanup(cleanup, ordering)
|
2021-08-30 13:25:59 -03:00
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
|
|
|
|
TestableTest.__module__ = 'Module'
|
|
|
|
sys.modules['Module'] = Module
|
|
|
|
runTests(TestableTest)
|
|
|
|
self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
|
|
|
|
'tearDownClass', 'cleanup_good'])
|
|
|
|
self.assertEqual(unittest.case._module_cleanups, [])
|
|
|
|
|
|
|
|
def test_run_module_cleanUp_when_teardown_exception(self):
|
|
|
|
ordering = []
|
|
|
|
class Module(object):
|
|
|
|
@staticmethod
|
|
|
|
def setUpModule():
|
|
|
|
ordering.append('setUpModule')
|
|
|
|
unittest.addModuleCleanup(cleanup, ordering)
|
|
|
|
@staticmethod
|
|
|
|
def tearDownModule():
|
2022-11-21 07:57:30 -04:00
|
|
|
ordering.append('tearDownModule')
|
2021-08-30 13:25:59 -03:00
|
|
|
raise Exception('CleanUpExc')
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
|
|
|
|
TestableTest.__module__ = 'Module'
|
|
|
|
sys.modules['Module'] = Module
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: CleanUpExc')
|
|
|
|
self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
|
2022-11-21 07:57:30 -04:00
|
|
|
'tearDownClass', 'tearDownModule',
|
|
|
|
'cleanup_good'])
|
2021-08-30 13:25:59 -03:00
|
|
|
self.assertEqual(unittest.case._module_cleanups, [])
|
|
|
|
|
|
|
|
def test_debug_module_executes_cleanUp(self):
|
|
|
|
ordering = []
|
|
|
|
blowUp = False
|
|
|
|
class Module(object):
|
|
|
|
@staticmethod
|
|
|
|
def setUpModule():
|
|
|
|
ordering.append('setUpModule')
|
|
|
|
unittest.addModuleCleanup(cleanup, ordering, blowUp=blowUp)
|
2018-11-08 22:34:33 -04:00
|
|
|
@staticmethod
|
|
|
|
def tearDownModule():
|
|
|
|
ordering.append('tearDownModule')
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
|
|
|
|
TestableTest.__module__ = 'Module'
|
|
|
|
sys.modules['Module'] = Module
|
|
|
|
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
|
|
|
|
suite.debug()
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpModule', 'setUpClass', 'test', 'tearDownClass',
|
|
|
|
'tearDownModule', 'cleanup_good'])
|
|
|
|
self.assertEqual(unittest.case._module_cleanups, [])
|
|
|
|
|
2021-08-30 13:25:59 -03:00
|
|
|
ordering = []
|
|
|
|
blowUp = True
|
|
|
|
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
|
|
|
|
with self.assertRaises(Exception) as cm:
|
|
|
|
suite.debug()
|
|
|
|
self.assertEqual(str(cm.exception), 'CleanUpExc')
|
|
|
|
self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
|
|
|
|
'tearDownClass', 'tearDownModule', 'cleanup_exc'])
|
|
|
|
self.assertEqual(unittest.case._module_cleanups, [])
|
|
|
|
|
|
|
|
def test_debug_module_cleanUp_when_teardown_exception(self):
|
|
|
|
ordering = []
|
|
|
|
blowUp = False
|
|
|
|
class Module(object):
|
|
|
|
@staticmethod
|
|
|
|
def setUpModule():
|
|
|
|
ordering.append('setUpModule')
|
|
|
|
unittest.addModuleCleanup(cleanup, ordering, blowUp=blowUp)
|
|
|
|
@staticmethod
|
|
|
|
def tearDownModule():
|
2022-11-21 07:57:30 -04:00
|
|
|
ordering.append('tearDownModule')
|
2021-08-30 13:25:59 -03:00
|
|
|
raise Exception('TearDownModuleExc')
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
|
|
|
|
TestableTest.__module__ = 'Module'
|
|
|
|
sys.modules['Module'] = Module
|
|
|
|
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
|
|
|
|
with self.assertRaises(Exception) as cm:
|
|
|
|
suite.debug()
|
|
|
|
self.assertEqual(str(cm.exception), 'TearDownModuleExc')
|
|
|
|
self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
|
2022-11-21 07:57:30 -04:00
|
|
|
'tearDownClass', 'tearDownModule'])
|
2021-08-30 13:25:59 -03:00
|
|
|
self.assertTrue(unittest.case._module_cleanups)
|
|
|
|
unittest.case._module_cleanups.clear()
|
|
|
|
|
|
|
|
ordering = []
|
|
|
|
blowUp = True
|
|
|
|
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
|
|
|
|
with self.assertRaises(Exception) as cm:
|
|
|
|
suite.debug()
|
|
|
|
self.assertEqual(str(cm.exception), 'TearDownModuleExc')
|
|
|
|
self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
|
2022-11-21 07:57:30 -04:00
|
|
|
'tearDownClass', 'tearDownModule'])
|
2021-08-30 13:25:59 -03:00
|
|
|
self.assertTrue(unittest.case._module_cleanups)
|
|
|
|
unittest.case._module_cleanups.clear()
|
|
|
|
|
2019-04-01 03:16:35 -03:00
|
|
|
def test_addClassCleanup_arg_errors(self):
|
|
|
|
cleanups = []
|
|
|
|
def cleanup(*args, **kwargs):
|
|
|
|
cleanups.append((args, kwargs))
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.addClassCleanup(cleanup, 1, 2, function=3, cls=4)
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
cls.addClassCleanup(function=cleanup, arg='hello')
|
|
|
|
def testNothing(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
TestableTest.addClassCleanup()
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
unittest.TestCase.addCleanup(cls=TestableTest(), function=cleanup)
|
|
|
|
runTests(TestableTest)
|
|
|
|
self.assertEqual(cleanups,
|
|
|
|
[((1, 2), {'function': 3, 'cls': 4})])
|
|
|
|
|
|
|
|
def test_addCleanup_arg_errors(self):
|
|
|
|
cleanups = []
|
|
|
|
def cleanup(*args, **kwargs):
|
|
|
|
cleanups.append((args, kwargs))
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def setUp(self2):
|
|
|
|
self2.addCleanup(cleanup, 1, 2, function=3, self=4)
|
2019-06-05 12:22:31 -03:00
|
|
|
with self.assertRaises(TypeError):
|
2019-04-01 03:16:35 -03:00
|
|
|
self2.addCleanup(function=cleanup, arg='hello')
|
|
|
|
def testNothing(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
TestableTest().addCleanup()
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
unittest.TestCase.addCleanup(self=TestableTest(), function=cleanup)
|
|
|
|
runTests(TestableTest)
|
|
|
|
self.assertEqual(cleanups,
|
2019-06-05 12:22:31 -03:00
|
|
|
[((1, 2), {'function': 3, 'self': 4})])
|
2019-04-01 03:16:35 -03:00
|
|
|
|
2018-11-08 22:34:33 -04:00
|
|
|
def test_with_errors_in_addClassCleanup(self):
|
|
|
|
ordering = []
|
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
@staticmethod
|
|
|
|
def setUpModule():
|
|
|
|
ordering.append('setUpModule')
|
|
|
|
unittest.addModuleCleanup(cleanup, ordering)
|
|
|
|
@staticmethod
|
|
|
|
def tearDownModule():
|
|
|
|
ordering.append('tearDownModule')
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
cls.addClassCleanup(cleanup, ordering, blowUp=True)
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
|
|
|
|
TestableTest.__module__ = 'Module'
|
|
|
|
sys.modules['Module'] = Module
|
|
|
|
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: CleanUpExc')
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpModule', 'setUpClass', 'test', 'tearDownClass',
|
|
|
|
'cleanup_exc', 'tearDownModule', 'cleanup_good'])
|
|
|
|
|
|
|
|
def test_with_errors_in_addCleanup(self):
|
|
|
|
ordering = []
|
|
|
|
class Module(object):
|
|
|
|
@staticmethod
|
|
|
|
def setUpModule():
|
|
|
|
ordering.append('setUpModule')
|
|
|
|
unittest.addModuleCleanup(cleanup, ordering)
|
|
|
|
@staticmethod
|
|
|
|
def tearDownModule():
|
|
|
|
ordering.append('tearDownModule')
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
ordering.append('setUp')
|
|
|
|
self.addCleanup(cleanup, ordering, blowUp=True)
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
def tearDown(self):
|
|
|
|
ordering.append('tearDown')
|
|
|
|
|
|
|
|
TestableTest.__module__ = 'Module'
|
|
|
|
sys.modules['Module'] = Module
|
|
|
|
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: CleanUpExc')
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpModule', 'setUp', 'test', 'tearDown',
|
|
|
|
'cleanup_exc', 'tearDownModule', 'cleanup_good'])
|
|
|
|
|
|
|
|
def test_with_errors_in_addModuleCleanup_and_setUps(self):
|
|
|
|
ordering = []
|
|
|
|
module_blow_up = False
|
|
|
|
class_blow_up = False
|
|
|
|
method_blow_up = False
|
|
|
|
class Module(object):
|
|
|
|
@staticmethod
|
|
|
|
def setUpModule():
|
|
|
|
ordering.append('setUpModule')
|
|
|
|
unittest.addModuleCleanup(cleanup, ordering, blowUp=True)
|
|
|
|
if module_blow_up:
|
|
|
|
raise Exception('ModuleExc')
|
|
|
|
@staticmethod
|
|
|
|
def tearDownModule():
|
|
|
|
ordering.append('tearDownModule')
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
ordering.append('setUpClass')
|
|
|
|
if class_blow_up:
|
|
|
|
raise Exception('ClassExc')
|
|
|
|
def setUp(self):
|
|
|
|
ordering.append('setUp')
|
|
|
|
if method_blow_up:
|
|
|
|
raise Exception('MethodExc')
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
ordering.append('tearDownClass')
|
|
|
|
|
|
|
|
TestableTest.__module__ = 'Module'
|
|
|
|
sys.modules['Module'] = Module
|
|
|
|
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: CleanUpExc')
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpModule', 'setUpClass', 'setUp', 'test',
|
|
|
|
'tearDownClass', 'tearDownModule',
|
|
|
|
'cleanup_exc'])
|
|
|
|
|
|
|
|
ordering = []
|
|
|
|
module_blow_up = True
|
|
|
|
class_blow_up = False
|
|
|
|
method_blow_up = False
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: ModuleExc')
|
2021-08-30 13:25:59 -03:00
|
|
|
self.assertEqual(result.errors[1][1].splitlines()[-1],
|
|
|
|
'Exception: CleanUpExc')
|
2018-11-08 22:34:33 -04:00
|
|
|
self.assertEqual(ordering, ['setUpModule', 'cleanup_exc'])
|
|
|
|
|
|
|
|
ordering = []
|
|
|
|
module_blow_up = False
|
|
|
|
class_blow_up = True
|
|
|
|
method_blow_up = False
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: ClassExc')
|
|
|
|
self.assertEqual(result.errors[1][1].splitlines()[-1],
|
|
|
|
'Exception: CleanUpExc')
|
|
|
|
self.assertEqual(ordering, ['setUpModule', 'setUpClass',
|
|
|
|
'tearDownModule', 'cleanup_exc'])
|
|
|
|
|
|
|
|
ordering = []
|
|
|
|
module_blow_up = False
|
|
|
|
class_blow_up = False
|
|
|
|
method_blow_up = True
|
|
|
|
result = runTests(TestableTest)
|
|
|
|
self.assertEqual(result.errors[0][1].splitlines()[-1],
|
|
|
|
'Exception: MethodExc')
|
|
|
|
self.assertEqual(result.errors[1][1].splitlines()[-1],
|
|
|
|
'Exception: CleanUpExc')
|
|
|
|
self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'setUp',
|
|
|
|
'tearDownClass', 'tearDownModule',
|
|
|
|
'cleanup_exc'])
|
|
|
|
|
|
|
|
def test_module_cleanUp_with_multiple_classes(self):
|
|
|
|
ordering =[]
|
|
|
|
def cleanup1():
|
|
|
|
ordering.append('cleanup1')
|
|
|
|
|
|
|
|
def cleanup2():
|
|
|
|
ordering.append('cleanup2')
|
|
|
|
|
|
|
|
def cleanup3():
|
|
|
|
ordering.append('cleanup3')
|
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
@staticmethod
|
|
|
|
def setUpModule():
|
|
|
|
ordering.append('setUpModule')
|
|
|
|
unittest.addModuleCleanup(cleanup1)
|
|
|
|
@staticmethod
|
|
|
|
def tearDownModule():
|
|
|
|
ordering.append('tearDownModule')
|
|
|
|
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
ordering.append('setUp')
|
|
|
|
self.addCleanup(cleanup2)
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test')
|
|
|
|
def tearDown(self):
|
|
|
|
ordering.append('tearDown')
|
|
|
|
|
|
|
|
class OtherTestableTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
ordering.append('setUp2')
|
|
|
|
self.addCleanup(cleanup3)
|
|
|
|
def testNothing(self):
|
|
|
|
ordering.append('test2')
|
|
|
|
def tearDown(self):
|
|
|
|
ordering.append('tearDown2')
|
|
|
|
|
|
|
|
TestableTest.__module__ = 'Module'
|
|
|
|
OtherTestableTest.__module__ = 'Module'
|
|
|
|
sys.modules['Module'] = Module
|
|
|
|
runTests(TestableTest, OtherTestableTest)
|
|
|
|
self.assertEqual(ordering,
|
|
|
|
['setUpModule', 'setUp', 'test', 'tearDown',
|
|
|
|
'cleanup2', 'setUp2', 'test2', 'tearDown2',
|
|
|
|
'cleanup3', 'tearDownModule', 'cleanup1'])
|
2010-06-10 13:16:08 -03:00
|
|
|
|
2022-05-08 11:49:09 -03:00
|
|
|
def test_enterModuleContext(self):
|
|
|
|
cleanups = []
|
|
|
|
|
|
|
|
unittest.addModuleCleanup(cleanups.append, 'cleanup1')
|
|
|
|
cm = TestCM(cleanups, 42)
|
|
|
|
self.assertEqual(unittest.enterModuleContext(cm), 42)
|
|
|
|
unittest.addModuleCleanup(cleanups.append, 'cleanup2')
|
|
|
|
|
|
|
|
unittest.case.doModuleCleanups()
|
|
|
|
self.assertEqual(cleanups, ['enter', 'cleanup2', 'exit', 'cleanup1'])
|
|
|
|
|
|
|
|
def test_enterModuleContext_arg_errors(self):
|
|
|
|
class TestableTest(unittest.TestCase):
|
|
|
|
def testNothing(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(TypeError, 'the context manager'):
|
|
|
|
unittest.enterModuleContext(LacksEnterAndExit())
|
|
|
|
with self.assertRaisesRegex(TypeError, 'the context manager'):
|
|
|
|
unittest.enterModuleContext(LacksEnter())
|
|
|
|
with self.assertRaisesRegex(TypeError, 'the context manager'):
|
|
|
|
unittest.enterModuleContext(LacksExit())
|
|
|
|
|
|
|
|
self.assertEqual(unittest.case._module_cleanups, [])
|
|
|
|
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
class Test_TextTestRunner(unittest.TestCase):
|
|
|
|
"""Tests for TextTestRunner."""
|
|
|
|
|
2013-04-23 20:49:52 -03:00
|
|
|
def setUp(self):
|
|
|
|
# clean the environment from pre-existing PYTHONWARNINGS to make
|
|
|
|
# test_warnings results consistent
|
|
|
|
self.pythonwarnings = os.environ.get('PYTHONWARNINGS')
|
|
|
|
if self.pythonwarnings:
|
|
|
|
del os.environ['PYTHONWARNINGS']
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# bring back pre-existing PYTHONWARNINGS if present
|
|
|
|
if self.pythonwarnings:
|
|
|
|
os.environ['PYTHONWARNINGS'] = self.pythonwarnings
|
|
|
|
|
2010-05-07 13:00:30 -03:00
|
|
|
def test_init(self):
|
|
|
|
runner = unittest.TextTestRunner()
|
|
|
|
self.assertFalse(runner.failfast)
|
|
|
|
self.assertFalse(runner.buffer)
|
|
|
|
self.assertEqual(runner.verbosity, 1)
|
2010-11-30 20:56:10 -04:00
|
|
|
self.assertEqual(runner.warnings, None)
|
2010-05-07 13:00:30 -03:00
|
|
|
self.assertTrue(runner.descriptions)
|
|
|
|
self.assertEqual(runner.resultclass, unittest.TextTestResult)
|
2015-03-05 20:46:35 -04:00
|
|
|
self.assertFalse(runner.tb_locals)
|
2023-04-02 19:12:51 -03:00
|
|
|
self.assertIsNone(runner.durations)
|
2010-05-07 13:00:30 -03:00
|
|
|
|
2012-09-28 10:14:03 -03:00
|
|
|
def test_multiple_inheritance(self):
|
|
|
|
class AResult(unittest.TestResult):
|
|
|
|
def __init__(self, stream, descriptions, verbosity):
|
|
|
|
super(AResult, self).__init__(stream, descriptions, verbosity)
|
|
|
|
|
|
|
|
class ATextResult(unittest.TextTestResult, AResult):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# This used to raise an exception due to TextTestResult not passing
|
|
|
|
# on arguments in its __init__ super call
|
|
|
|
ATextResult(None, None, 1)
|
|
|
|
|
2010-05-07 13:00:30 -03:00
|
|
|
def testBufferAndFailfast(self):
|
|
|
|
class Test(unittest.TestCase):
|
|
|
|
def testFoo(self):
|
|
|
|
pass
|
|
|
|
result = unittest.TestResult()
|
|
|
|
runner = unittest.TextTestRunner(stream=io.StringIO(), failfast=True,
|
2015-03-05 20:46:35 -04:00
|
|
|
buffer=True)
|
2010-05-07 13:00:30 -03:00
|
|
|
# Use our result object
|
|
|
|
runner._makeResult = lambda: result
|
|
|
|
runner.run(Test('testFoo'))
|
|
|
|
|
|
|
|
self.assertTrue(result.failfast)
|
|
|
|
self.assertTrue(result.buffer)
|
|
|
|
|
2015-03-05 20:46:35 -04:00
|
|
|
def test_locals(self):
|
|
|
|
runner = unittest.TextTestRunner(stream=io.StringIO(), tb_locals=True)
|
|
|
|
result = runner.run(unittest.TestSuite())
|
|
|
|
self.assertEqual(True, result.tb_locals)
|
|
|
|
|
2010-05-07 13:00:30 -03:00
|
|
|
def testRunnerRegistersResult(self):
|
|
|
|
class Test(unittest.TestCase):
|
|
|
|
def testFoo(self):
|
|
|
|
pass
|
|
|
|
originalRegisterResult = unittest.runner.registerResult
|
|
|
|
def cleanup():
|
|
|
|
unittest.runner.registerResult = originalRegisterResult
|
|
|
|
self.addCleanup(cleanup)
|
|
|
|
|
|
|
|
result = unittest.TestResult()
|
|
|
|
runner = unittest.TextTestRunner(stream=io.StringIO())
|
|
|
|
# Use our result object
|
|
|
|
runner._makeResult = lambda: result
|
|
|
|
|
|
|
|
self.wasRegistered = 0
|
|
|
|
def fakeRegisterResult(thisResult):
|
|
|
|
self.wasRegistered += 1
|
|
|
|
self.assertEqual(thisResult, result)
|
|
|
|
unittest.runner.registerResult = fakeRegisterResult
|
|
|
|
|
|
|
|
runner.run(unittest.TestSuite())
|
|
|
|
self.assertEqual(self.wasRegistered, 1)
|
|
|
|
|
2010-03-27 09:34:21 -03:00
|
|
|
def test_works_with_result_without_startTestRun_stopTestRun(self):
|
|
|
|
class OldTextResult(ResultWithNoStartTestRunStopTestRun):
|
|
|
|
separator2 = ''
|
|
|
|
def printErrors(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Runner(unittest.TextTestRunner):
|
|
|
|
def __init__(self):
|
|
|
|
super(Runner, self).__init__(io.StringIO())
|
|
|
|
|
|
|
|
def _makeResult(self):
|
|
|
|
return OldTextResult()
|
|
|
|
|
|
|
|
runner = Runner()
|
|
|
|
runner.run(unittest.TestSuite())
|
|
|
|
|
|
|
|
def test_startTestRun_stopTestRun_called(self):
|
|
|
|
class LoggingTextResult(LoggingResult):
|
|
|
|
separator2 = ''
|
|
|
|
def printErrors(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class LoggingRunner(unittest.TextTestRunner):
|
|
|
|
def __init__(self, events):
|
|
|
|
super(LoggingRunner, self).__init__(io.StringIO())
|
|
|
|
self._events = events
|
|
|
|
|
|
|
|
def _makeResult(self):
|
|
|
|
return LoggingTextResult(self._events)
|
|
|
|
|
|
|
|
events = []
|
|
|
|
runner = LoggingRunner(events)
|
|
|
|
runner.run(unittest.TestSuite())
|
|
|
|
expected = ['startTestRun', 'stopTestRun']
|
|
|
|
self.assertEqual(events, expected)
|
|
|
|
|
|
|
|
def test_pickle_unpickle(self):
|
|
|
|
# Issue #7197: a TextTestRunner should be (un)pickleable. This is
|
|
|
|
# required by test_multiprocessing under Windows (in verbose mode).
|
|
|
|
stream = io.StringIO("foo")
|
|
|
|
runner = unittest.TextTestRunner(stream)
|
|
|
|
for protocol in range(2, pickle.HIGHEST_PROTOCOL + 1):
|
|
|
|
s = pickle.dumps(runner, protocol)
|
|
|
|
obj = pickle.loads(s)
|
|
|
|
# StringIO objects never compare equal, a cheap test instead.
|
|
|
|
self.assertEqual(obj.stream.getvalue(), stream.getvalue())
|
|
|
|
|
|
|
|
def test_resultclass(self):
|
|
|
|
def MockResultClass(*args):
|
|
|
|
return args
|
|
|
|
STREAM = object()
|
|
|
|
DESCRIPTIONS = object()
|
|
|
|
VERBOSITY = object()
|
|
|
|
runner = unittest.TextTestRunner(STREAM, DESCRIPTIONS, VERBOSITY,
|
|
|
|
resultclass=MockResultClass)
|
|
|
|
self.assertEqual(runner.resultclass, MockResultClass)
|
|
|
|
|
|
|
|
expectedresult = (runner.stream, DESCRIPTIONS, VERBOSITY)
|
|
|
|
self.assertEqual(runner._makeResult(), expectedresult)
|
2010-11-30 20:56:10 -04:00
|
|
|
|
2022-03-22 07:04:36 -03:00
|
|
|
@support.requires_subprocess()
|
2010-11-30 20:56:10 -04:00
|
|
|
def test_warnings(self):
|
|
|
|
"""
|
|
|
|
Check that warnings argument of TextTestRunner correctly affects the
|
|
|
|
behavior of the warnings.
|
|
|
|
"""
|
|
|
|
# see #10535 and the _test_warnings file for more information
|
|
|
|
|
|
|
|
def get_parse_out_err(p):
|
|
|
|
return [b.splitlines() for b in p.communicate()]
|
|
|
|
opts = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
|
|
cwd=os.path.dirname(__file__))
|
|
|
|
|
|
|
|
# no args -> all the warnings are printed, unittest warnings only once
|
2017-12-12 17:59:48 -04:00
|
|
|
p = subprocess.Popen([sys.executable, '-E', '_test_warnings.py'], **opts)
|
2016-03-29 20:15:28 -03:00
|
|
|
with p:
|
|
|
|
out, err = get_parse_out_err(p)
|
2010-11-30 21:45:53 -04:00
|
|
|
self.assertIn(b'OK', err)
|
2010-11-30 20:56:10 -04:00
|
|
|
# check that the total number of warnings in the output is correct
|
2022-06-26 04:18:06 -03:00
|
|
|
self.assertEqual(len(out), 10)
|
2010-11-30 20:56:10 -04:00
|
|
|
# check that the numbers of the different kind of warnings is correct
|
|
|
|
for msg in [b'dw', b'iw', b'uw']:
|
|
|
|
self.assertEqual(out.count(msg), 3)
|
2022-06-26 04:18:06 -03:00
|
|
|
for msg in [b'rw']:
|
2010-11-30 20:56:10 -04:00
|
|
|
self.assertEqual(out.count(msg), 1)
|
|
|
|
|
|
|
|
args_list = (
|
|
|
|
# passing 'ignore' as warnings arg -> no warnings
|
|
|
|
[sys.executable, '_test_warnings.py', 'ignore'],
|
|
|
|
# -W doesn't affect the result if the arg is passed
|
|
|
|
[sys.executable, '-Wa', '_test_warnings.py', 'ignore'],
|
|
|
|
# -W affects the result if the arg is not passed
|
|
|
|
[sys.executable, '-Wi', '_test_warnings.py']
|
|
|
|
)
|
|
|
|
# in all these cases no warnings are printed
|
|
|
|
for args in args_list:
|
|
|
|
p = subprocess.Popen(args, **opts)
|
2016-03-29 20:15:28 -03:00
|
|
|
with p:
|
|
|
|
out, err = get_parse_out_err(p)
|
2010-11-30 21:45:53 -04:00
|
|
|
self.assertIn(b'OK', err)
|
2010-11-30 20:56:10 -04:00
|
|
|
self.assertEqual(len(out), 0)
|
|
|
|
|
|
|
|
|
|
|
|
# passing 'always' as warnings arg -> all the warnings printed,
|
|
|
|
# unittest warnings only once
|
|
|
|
p = subprocess.Popen([sys.executable, '_test_warnings.py', 'always'],
|
|
|
|
**opts)
|
2016-03-29 20:15:28 -03:00
|
|
|
with p:
|
|
|
|
out, err = get_parse_out_err(p)
|
2010-11-30 21:45:53 -04:00
|
|
|
self.assertIn(b'OK', err)
|
2022-06-26 04:18:06 -03:00
|
|
|
self.assertEqual(len(out), 12)
|
2010-11-30 20:56:10 -04:00
|
|
|
for msg in [b'dw', b'iw', b'uw', b'rw']:
|
|
|
|
self.assertEqual(out.count(msg), 3)
|
2010-12-30 15:36:29 -04:00
|
|
|
|
|
|
|
def testStdErrLookedUpAtInstantiationTime(self):
|
|
|
|
# see issue 10786
|
|
|
|
old_stderr = sys.stderr
|
|
|
|
f = io.StringIO()
|
|
|
|
sys.stderr = f
|
|
|
|
try:
|
|
|
|
runner = unittest.TextTestRunner()
|
|
|
|
self.assertTrue(runner.stream.stream is f)
|
|
|
|
finally:
|
|
|
|
sys.stderr = old_stderr
|
|
|
|
|
|
|
|
def testSpecifiedStreamUsed(self):
|
|
|
|
# see issue 10786
|
|
|
|
f = io.StringIO()
|
|
|
|
runner = unittest.TextTestRunner(f)
|
|
|
|
self.assertTrue(runner.stream.stream is f)
|
2013-09-13 18:52:46 -03:00
|
|
|
|
2023-04-02 19:12:51 -03:00
|
|
|
def test_durations(self):
|
2023-04-16 19:19:44 -03:00
|
|
|
def run(test, *, expect_durations=True):
|
2023-04-02 19:12:51 -03:00
|
|
|
stream = BufferedWriter()
|
|
|
|
runner = unittest.TextTestRunner(stream=stream, durations=5, verbosity=2)
|
|
|
|
result = runner.run(test)
|
|
|
|
self.assertEqual(result.durations, 5)
|
|
|
|
stream.flush()
|
|
|
|
text = stream.getvalue()
|
|
|
|
regex = r"\n\d+.\d\d\ds"
|
|
|
|
if expect_durations:
|
|
|
|
self.assertEqual(len(result.collectedDurations), 1)
|
|
|
|
self.assertIn('Slowest test durations', text)
|
|
|
|
self.assertRegex(text, regex)
|
|
|
|
else:
|
|
|
|
self.assertEqual(len(result.collectedDurations), 0)
|
|
|
|
self.assertNotIn('Slowest test durations', text)
|
|
|
|
self.assertNotRegex(text, regex)
|
|
|
|
|
|
|
|
# success
|
|
|
|
class Foo(unittest.TestCase):
|
|
|
|
def test_1(self):
|
|
|
|
pass
|
|
|
|
|
2023-04-16 19:19:44 -03:00
|
|
|
run(Foo('test_1'), expect_durations=True)
|
2023-04-02 19:12:51 -03:00
|
|
|
|
|
|
|
# failure
|
|
|
|
class Foo(unittest.TestCase):
|
|
|
|
def test_1(self):
|
|
|
|
self.assertEqual(0, 1)
|
|
|
|
|
2023-04-16 19:19:44 -03:00
|
|
|
run(Foo('test_1'), expect_durations=True)
|
2023-04-02 19:12:51 -03:00
|
|
|
|
|
|
|
# error
|
|
|
|
class Foo(unittest.TestCase):
|
|
|
|
def test_1(self):
|
|
|
|
1 / 0
|
|
|
|
|
2023-04-16 19:19:44 -03:00
|
|
|
run(Foo('test_1'), expect_durations=True)
|
2023-04-02 19:12:51 -03:00
|
|
|
|
|
|
|
|
|
|
|
# error in setUp and tearDown
|
|
|
|
class Foo(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
1 / 0
|
|
|
|
tearDown = setUp
|
|
|
|
def test_1(self):
|
|
|
|
pass
|
|
|
|
|
2023-04-16 19:19:44 -03:00
|
|
|
run(Foo('test_1'), expect_durations=True)
|
2023-04-02 19:12:51 -03:00
|
|
|
|
|
|
|
# skip (expect no durations)
|
|
|
|
class Foo(unittest.TestCase):
|
|
|
|
@unittest.skip("reason")
|
|
|
|
def test_1(self):
|
|
|
|
pass
|
|
|
|
|
2023-04-16 19:19:44 -03:00
|
|
|
run(Foo('test_1'), expect_durations=False)
|
2023-04-02 19:12:51 -03:00
|
|
|
|
|
|
|
|
2013-09-13 18:52:46 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|