diff options
author | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-02-22 23:28:32 (GMT) |
---|---|---|
committer | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-02-22 23:28:32 (GMT) |
commit | ae3db0a12b0c5ca42ea9fe54161d56c57269ea92 (patch) | |
tree | 5a636558ffc55d6f41efdece75cb2163056b4ce1 /Lib | |
parent | 4b81bc7fe6aad900cf2904c8a22a2c70a8b42659 (diff) | |
download | cpython-ae3db0a12b0c5ca42ea9fe54161d56c57269ea92.zip cpython-ae3db0a12b0c5ca42ea9fe54161d56c57269ea92.tar.gz cpython-ae3db0a12b0c5ca42ea9fe54161d56c57269ea92.tar.bz2 |
Support for old TestResult object (unittest) with warnings when using unsupported features.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_unittest.py | 50 | ||||
-rw-r--r-- | Lib/unittest/case.py | 31 | ||||
-rw-r--r-- | Lib/unittest/result.py | 4 |
3 files changed, 77 insertions, 8 deletions
diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py index df760a7..d5f9a5c 100644 --- a/Lib/test/test_unittest.py +++ b/Lib/test/test_unittest.py @@ -2062,6 +2062,53 @@ class Test_TestResult(TestCase): 'Tests getDescription() for a method with a longer ' '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 ################################################################ @@ -3826,7 +3873,8 @@ def test_main(): test_support.run_unittest(Test_TestCase, Test_TestLoader, Test_TestSuite, Test_TestResult, Test_FunctionTestCase, Test_TestSkipping, Test_Assertions, TestLongMessage, - Test_TestProgram, TestCleanUp, TestDiscovery, Test_TextTestRunner) + Test_TestProgram, TestCleanUp, TestDiscovery, Test_TextTestRunner, + Test_OldTestResult) if __name__ == "__main__": test_main() diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py index 8de36d7..88d1bec 100644 --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -249,6 +249,15 @@ class TestCase(object): return "<%s testMethod=%s>" % \ (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): orig_result = result if result is None: @@ -262,7 +271,7 @@ class TestCase(object): if getattr(self.__class__, "__unittest_skip__", False): # If the whole class was skipped. try: - result.addSkip(self, self.__class__.__unittest_skip_why__) + self._addSkip(result, self.__class__.__unittest_skip_why__) finally: result.stopTest(self) return @@ -272,7 +281,7 @@ class TestCase(object): try: self.setUp() except SkipTest as e: - result.addSkip(self, str(e)) + self._addSkip(result, str(e)) except Exception: result.addError(self, sys.exc_info()) else: @@ -281,11 +290,23 @@ class TestCase(object): except self.failureException: result.addFailure(self, sys.exc_info()) 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: - 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: - result.addSkip(self, str(e)) + self._addSkip(result, str(e)) except Exception: result.addError(self, sys.exc_info()) else: diff --git a/Lib/unittest/result.py b/Lib/unittest/result.py index 4538304..bb2a47c 100644 --- a/Lib/unittest/result.py +++ b/Lib/unittest/result.py @@ -107,6 +107,6 @@ class TestResult(object): return length 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), - len(self.failures)) + len(self.failures))) |