Index: Lib/unittest.py =================================================================== --- Lib/unittest.py (revision 60916) +++ Lib/unittest.py (working copy) @@ -63,36 +63,16 @@ # Expose obsolete functions for backwards compatibility __all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases']) - ############################################################################## -# Backward compatibility -############################################################################## -if sys.version_info[:2] < (2, 2): - False, True = 0, 1 - def isinstance(obj, clsinfo): - import __builtin__ - if type(clsinfo) in (tuple, list): - for cls in clsinfo: - if cls is type: cls = types.ClassType - if __builtin__.isinstance(obj, cls): - return 1 - return 0 - else: return __builtin__.isinstance(obj, clsinfo) - - -############################################################################## # Test framework core ############################################################################## -# All classes defined herein are 'new-style' classes, allowing use of 'super()' -__metaclass__ = type - def _strclass(cls): return "%s.%s" % (cls.__module__, cls.__name__) __unittest = 1 -class TestResult: +class TestResult(object): """Holder for test result information. Test results are automatically managed by the TestCase and TestSuite @@ -153,7 +133,7 @@ return ''.join(traceback.format_exception(exctype, value, tb)) def _is_relevant_tb_level(self, tb): - return tb.tb_frame.f_globals.has_key('__unittest') + return '__unittest' in tb.tb_frame.f_globals def _count_relevant_tb_levels(self, tb): length = 0 @@ -167,7 +147,7 @@ (_strclass(self.__class__), self.testsRun, len(self.errors), len(self.failures)) -class TestCase: +class TestCase(object): """A class whose instances are single test cases. By default, the test code itself should be placed in a method named @@ -205,8 +185,8 @@ testMethod = getattr(self, methodName) self._testMethodDoc = testMethod.__doc__ except AttributeError: - raise ValueError, "no such test method in %s: %s" % \ - (self.__class__, methodName) + raise ValueError("no such test method in %s: %s" % \ + (self.__class__, methodName)) def setUp(self): "Hook method for setting up the test fixture before exercising it." @@ -261,9 +241,7 @@ try: try: self.setUp() - except KeyboardInterrupt: - raise - except: + except Exception: result.addError(self, self._exc_info()) return @@ -273,16 +251,12 @@ ok = True except self.failureException: result.addFailure(self, self._exc_info()) - except KeyboardInterrupt: - raise - except: + except Exception: result.addError(self, self._exc_info()) try: self.tearDown() - except KeyboardInterrupt: - raise - except: + except Exception: result.addError(self, self._exc_info()) ok = False if ok: result.addSuccess(self) @@ -307,15 +281,15 @@ def fail(self, msg=None): """Fail immediately, with the given message.""" - raise self.failureException, msg + raise self.failureException(msg) def failIf(self, expr, msg=None): "Fail the test if the expression is true." - if expr: raise self.failureException, msg + if expr: raise self.failureException(msg) def failUnless(self, expr, msg=None): """Fail the test unless the expression is true.""" - if not expr: raise self.failureException, msg + if not expr: raise self.failureException(msg) def failUnlessRaises(self, excClass, callableObj, *args, **kwargs): """Fail unless an exception of class excClass is thrown @@ -332,23 +306,23 @@ else: if hasattr(excClass,'__name__'): excName = excClass.__name__ else: excName = str(excClass) - raise self.failureException, "%s not raised" % excName + raise self.failureException("%s not raised" % excName) def failUnlessEqual(self, first, second, msg=None): """Fail if the two objects are unequal as determined by the '==' operator. """ if not first == second: - raise self.failureException, \ - (msg or '%r != %r' % (first, second)) + raise self.failureException( + (msg or '%r != %r' % (first, second))) def failIfEqual(self, first, second, msg=None): """Fail if the two objects are equal as determined by the '==' operator. """ if first == second: - raise self.failureException, \ - (msg or '%r == %r' % (first, second)) + raise self.failureException( + (msg or '%r == %r' % (first, second))) def failUnlessAlmostEqual(self, first, second, places=7, msg=None): """Fail if the two objects are unequal as determined by their @@ -359,8 +333,8 @@ as significant digits (measured from the most signficant digit). """ if round(abs(second-first), places) != 0: - raise self.failureException, \ - (msg or '%r != %r within %r places' % (first, second, places)) + raise self.failureException( + (msg or '%r != %r within %r places' % (first, second, places))) def failIfAlmostEqual(self, first, second, places=7, msg=None): """Fail if the two objects are equal as determined by their @@ -371,8 +345,8 @@ as significant digits (measured from the most signficant digit). """ if round(abs(second-first), places) == 0: - raise self.failureException, \ - (msg or '%r == %r within %r places' % (first, second, places)) + raise self.failureException( + (msg or '%r == %r within %r places' % (first, second, places))) # Synonyms for assertion methods @@ -392,7 +366,7 @@ -class TestSuite: +class TestSuite(object): """A test suite is a composite test consisting of a number of TestCases. For use, create an instance of TestSuite, then add test case instances. @@ -522,7 +496,7 @@ # Locating and loading tests ############################################################################## -class TestLoader: +class TestLoader(object): """This class is responsible for loading tests according to various criteria and returning them wrapped in a TestSuite """ @@ -573,12 +547,12 @@ for part in parts: parent, obj = obj, getattr(obj, part) - if type(obj) == types.ModuleType: + if isinstance(obj, types.ModuleType): return self.loadTestsFromModule(obj) elif (isinstance(obj, (type, types.ClassType)) and issubclass(obj, TestCase)): return self.loadTestsFromTestCase(obj) - elif (type(obj) == types.UnboundMethodType and + elif (isinstance(obj, types.UnboundMethodType) and isinstance(parent, (type, types.ClassType)) and issubclass(parent, TestCase)): return TestSuite([parent(obj.__name__)]) @@ -643,7 +617,7 @@ # Text UI ############################################################################## -class _WritelnDecorator: +class _WritelnDecorator(object): """Used to decorate file-like objects with a handy 'writeln' method""" def __init__(self,stream): self.stream = stream @@ -786,7 +760,7 @@ def __init__(self, module='__main__', defaultTest=None, argv=None, testRunner=TextTestRunner, testLoader=defaultTestLoader): - if type(module) == type(''): + if isinstance(module, basestring): self.module = __import__(module) for part in module.split('.')[1:]: self.module = getattr(self.module, part)