Support for old TestResult object (unittest) with warnings when using unsupported features.
This commit is contained in:
parent
4b81bc7fe6
commit
ae3db0a12b
|
@ -2062,6 +2062,53 @@ class Test_TestResult(TestCase):
|
||||||
'Tests getDescription() for a method with a longer '
|
'Tests getDescription() for a method with a longer '
|
||||||
'docstring.'))
|
'docstring.'))
|
||||||
|
|
||||||
|
classDict = dict(unittest.TestResult.__dict__)
|
||||||
|
for m in 'addSkip', 'addExpectedFailure', 'addUnexpectedSuccess':
|
||||||
|
del classDict[m]
|
||||||
|
OldResult = type('OldResult', (object,), classDict)
|
||||||
|
|
||||||
|
class Test_OldTestResult(unittest.TestCase):
|
||||||
|
|
||||||
|
def assertOldResultWarning(self, test, failures):
|
||||||
|
with warnings.catch_warnings(record=True) as log:
|
||||||
|
result = OldResult()
|
||||||
|
test.run(result)
|
||||||
|
self.assertEqual(len(result.failures), failures)
|
||||||
|
warning, = log
|
||||||
|
self.assertIs(warning.category, RuntimeWarning)
|
||||||
|
|
||||||
|
def testOldTestResult(self):
|
||||||
|
class Test(unittest.TestCase):
|
||||||
|
def testSkip(self):
|
||||||
|
self.skipTest('foobar')
|
||||||
|
@unittest.expectedFailure
|
||||||
|
def testExpectedFail(self):
|
||||||
|
raise TypeError
|
||||||
|
@unittest.expectedFailure
|
||||||
|
def testUnexpectedSuccess(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
for test_name, should_pass in (('testSkip', True),
|
||||||
|
('testExpectedFail', True),
|
||||||
|
('testUnexpectedSuccess', False)):
|
||||||
|
test = Test(test_name)
|
||||||
|
self.assertOldResultWarning(test, int(not should_pass))
|
||||||
|
|
||||||
|
def testOldTestTesultSetup(self):
|
||||||
|
class Test(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.skipTest('no reason')
|
||||||
|
def testFoo(self):
|
||||||
|
pass
|
||||||
|
self.assertOldResultWarning(Test('testFoo'), 0)
|
||||||
|
|
||||||
|
def testOldTestResultClass(self):
|
||||||
|
@unittest.skip('no reason')
|
||||||
|
class Test(unittest.TestCase):
|
||||||
|
def testFoo(self):
|
||||||
|
pass
|
||||||
|
self.assertOldResultWarning(Test('testFoo'), 0)
|
||||||
|
|
||||||
|
|
||||||
### Support code for Test_TestCase
|
### Support code for Test_TestCase
|
||||||
################################################################
|
################################################################
|
||||||
|
@ -3826,7 +3873,8 @@ def test_main():
|
||||||
test_support.run_unittest(Test_TestCase, Test_TestLoader,
|
test_support.run_unittest(Test_TestCase, Test_TestLoader,
|
||||||
Test_TestSuite, Test_TestResult, Test_FunctionTestCase,
|
Test_TestSuite, Test_TestResult, Test_FunctionTestCase,
|
||||||
Test_TestSkipping, Test_Assertions, TestLongMessage,
|
Test_TestSkipping, Test_Assertions, TestLongMessage,
|
||||||
Test_TestProgram, TestCleanUp, TestDiscovery, Test_TextTestRunner)
|
Test_TestProgram, TestCleanUp, TestDiscovery, Test_TextTestRunner,
|
||||||
|
Test_OldTestResult)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test_main()
|
test_main()
|
||||||
|
|
|
@ -249,6 +249,15 @@ class TestCase(object):
|
||||||
return "<%s testMethod=%s>" % \
|
return "<%s testMethod=%s>" % \
|
||||||
(strclass(self.__class__), self._testMethodName)
|
(strclass(self.__class__), self._testMethodName)
|
||||||
|
|
||||||
|
def _addSkip(self, result, reason):
|
||||||
|
addSkip = getattr(result, 'addSkip', None)
|
||||||
|
if addSkip is not None:
|
||||||
|
addSkip(self, reason)
|
||||||
|
else:
|
||||||
|
warnings.warn("TestResult has no addSkip method, skips not reported",
|
||||||
|
RuntimeWarning, 2)
|
||||||
|
result.addSuccess(self)
|
||||||
|
|
||||||
def run(self, result=None):
|
def run(self, result=None):
|
||||||
orig_result = result
|
orig_result = result
|
||||||
if result is None:
|
if result is None:
|
||||||
|
@ -262,7 +271,7 @@ class TestCase(object):
|
||||||
if getattr(self.__class__, "__unittest_skip__", False):
|
if getattr(self.__class__, "__unittest_skip__", False):
|
||||||
# If the whole class was skipped.
|
# If the whole class was skipped.
|
||||||
try:
|
try:
|
||||||
result.addSkip(self, self.__class__.__unittest_skip_why__)
|
self._addSkip(result, self.__class__.__unittest_skip_why__)
|
||||||
finally:
|
finally:
|
||||||
result.stopTest(self)
|
result.stopTest(self)
|
||||||
return
|
return
|
||||||
|
@ -272,7 +281,7 @@ class TestCase(object):
|
||||||
try:
|
try:
|
||||||
self.setUp()
|
self.setUp()
|
||||||
except SkipTest as e:
|
except SkipTest as e:
|
||||||
result.addSkip(self, str(e))
|
self._addSkip(result, str(e))
|
||||||
except Exception:
|
except Exception:
|
||||||
result.addError(self, sys.exc_info())
|
result.addError(self, sys.exc_info())
|
||||||
else:
|
else:
|
||||||
|
@ -281,11 +290,23 @@ class TestCase(object):
|
||||||
except self.failureException:
|
except self.failureException:
|
||||||
result.addFailure(self, sys.exc_info())
|
result.addFailure(self, sys.exc_info())
|
||||||
except _ExpectedFailure as e:
|
except _ExpectedFailure as e:
|
||||||
result.addExpectedFailure(self, e.exc_info)
|
addExpectedFailure = getattr(result, 'addExpectedFailure', None)
|
||||||
|
if addExpectedFailure is not None:
|
||||||
|
addExpectedFailure(self, e.exc_info)
|
||||||
|
else:
|
||||||
|
warnings.warn("TestResult has no addExpectedFailure method, reporting as passes",
|
||||||
|
RuntimeWarning)
|
||||||
|
result.addSuccess(self)
|
||||||
except _UnexpectedSuccess:
|
except _UnexpectedSuccess:
|
||||||
result.addUnexpectedSuccess(self)
|
addUnexpectedSuccess = getattr(result, 'addUnexpectedSuccess', None)
|
||||||
|
if addUnexpectedSuccess is not None:
|
||||||
|
addUnexpectedSuccess(self)
|
||||||
|
else:
|
||||||
|
warnings.warn("TestResult has no addUnexpectedSuccess method, reporting as failures",
|
||||||
|
RuntimeWarning)
|
||||||
|
result.addFailure(self, sys.exc_info())
|
||||||
except SkipTest as e:
|
except SkipTest as e:
|
||||||
result.addSkip(self, str(e))
|
self._addSkip(result, str(e))
|
||||||
except Exception:
|
except Exception:
|
||||||
result.addError(self, sys.exc_info())
|
result.addError(self, sys.exc_info())
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -107,6 +107,6 @@ class TestResult(object):
|
||||||
return length
|
return length
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<%s run=%i errors=%i failures=%i>" % \
|
return ("<%s run=%i errors=%i failures=%i>" %
|
||||||
(util.strclass(self.__class__), self.testsRun, len(self.errors),
|
(util.strclass(self.__class__), self.testsRun, len(self.errors),
|
||||||
len(self.failures))
|
len(self.failures)))
|
||||||
|
|
Loading…
Reference in New Issue