175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport sys 275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport warnings 375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport unittest2 575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chendef resultFactory(*_): 875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return unittest2.TestResult() 975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 1075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass OldTestResult(object): 1175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen """An object honouring TestResult before startTestRun/stopTestRun.""" 1275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 1375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def __init__(self, *_): 1475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.failures = [] 1575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.errors = [] 1675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.testsRun = 0 1775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.shouldStop = False 1875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 1975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def startTest(self, test): 2075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 2175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 2275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def stopTest(self, test): 2375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 2475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 2575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def addError(self, test, err): 2675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.errors.append((test, err)) 2775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 2875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def addFailure(self, test, err): 2975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.failures.append((test, err)) 3075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 3175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def addSuccess(self, test): 3275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 3375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 3475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def wasSuccessful(self): 3575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return True 3675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 3775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def printErrors(self): 3875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 3975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 4075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass LoggingResult(unittest2.TestResult): 4175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def __init__(self, log): 4275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._events = log 4375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(LoggingResult, self).__init__() 4475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 4575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def startTest(self, test): 4675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._events.append('startTest') 4775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(LoggingResult, self).startTest(test) 4875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 4975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def startTestRun(self): 5075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._events.append('startTestRun') 5175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(LoggingResult, self).startTestRun() 5275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 5375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def stopTest(self, test): 5475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._events.append('stopTest') 5575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(LoggingResult, self).stopTest(test) 5675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 5775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def stopTestRun(self): 5875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._events.append('stopTestRun') 5975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(LoggingResult, self).stopTestRun() 6075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 6175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def addFailure(self, *args): 6275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._events.append('addFailure') 6375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(LoggingResult, self).addFailure(*args) 6475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 6575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def addSuccess(self, *args): 6675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._events.append('addSuccess') 6775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(LoggingResult, self).addSuccess(*args) 6875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 6975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def addError(self, *args): 7075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._events.append('addError') 7175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(LoggingResult, self).addError(*args) 7275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 7375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def addSkip(self, *args): 7475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._events.append('addSkip') 7575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(LoggingResult, self).addSkip(*args) 7675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 7775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def addExpectedFailure(self, *args): 7875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._events.append('addExpectedFailure') 7975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(LoggingResult, self).addExpectedFailure(*args) 8075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 8175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def addUnexpectedSuccess(self, *args): 8275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._events.append('addUnexpectedSuccess') 8375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(LoggingResult, self).addUnexpectedSuccess(*args) 8475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 8575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 8675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass EqualityMixin(object): 8775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen """Used as a mixin for TestCase""" 8875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 8975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Check for a valid __eq__ implementation 9075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_eq(self): 9175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for obj_1, obj_2 in self.eq_pairs: 9275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(obj_1, obj_2) 9375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(obj_2, obj_1) 9475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 9575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Check for a valid __ne__ implementation 9675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_ne(self): 9775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for obj_1, obj_2 in self.ne_pairs: 9875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertNotEqual(obj_1, obj_2) 9975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertNotEqual(obj_2, obj_1) 10075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 10175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass HashingMixin(object): 10275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen """Used as a mixin for TestCase""" 10375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 10475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Check for a valid __hash__ implementation 10575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_hash(self): 10675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for obj_1, obj_2 in self.eq_pairs: 10775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen try: 10875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if not hash(obj_1) == hash(obj_2): 10975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail("%r and %r do not hash equal" % (obj_1, obj_2)) 11075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except KeyboardInterrupt: 11175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise 11275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except Exception, e: 11375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail("Problem hashing %r and %r: %s" % (obj_1, obj_2, e)) 11475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 11575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for obj_1, obj_2 in self.ne_pairs: 11675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen try: 11775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if hash(obj_1) == hash(obj_2): 11875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail("%s and %s hash equal, but shouldn't" % 11975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen (obj_1, obj_2)) 12075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except KeyboardInterrupt: 12175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise 12275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except Exception, e: 12375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e)) 12475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 12575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 12675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 12775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen# copied from Python 2.6 12875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chentry: 12975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen from warnings import catch_warnings 13075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenexcept ImportError: 13175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class catch_warnings(object): 13275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def __init__(self, record=False, module=None): 13375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._record = record 13475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._module = sys.modules['warnings'] 13575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._entered = False 13675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 13775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def __repr__(self): 13875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen args = [] 13975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if self._record: 14075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen args.append("record=True") 14175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen name = type(self).__name__ 14275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return "%s(%s)" % (name, ", ".join(args)) 14375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 14475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def __enter__(self): 14575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if self._entered: 14675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise RuntimeError("Cannot enter %r twice" % self) 14775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._entered = True 14875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._filters = self._module.filters 14975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._module.filters = self._filters[:] 15075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._showwarning = self._module.showwarning 15175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if self._record: 15275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen log = [] 15375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def showwarning(*args, **kwargs): 15475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen log.append(WarningMessage(*args, **kwargs)) 15575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._module.showwarning = showwarning 15675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return log 15775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen else: 15875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return None 15975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 16075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def __exit__(self, *exc_info): 16175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if not self._entered: 16275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise RuntimeError("Cannot exit %r without entering first" % self) 16375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._module.filters = self._filters 16475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._module.showwarning = self._showwarning 16575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 16675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class WarningMessage(object): 16775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen _WARNING_DETAILS = ("message", "category", "filename", "lineno", "file", 16875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen "line") 16975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def __init__(self, message, category, filename, lineno, file=None, 17075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen line=None): 17175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen local_values = locals() 17275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for attr in self._WARNING_DETAILS: 17375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen setattr(self, attr, local_values[attr]) 17475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._category_name = None 17575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if category.__name__: 17675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self._category_name = category.__name__ 17775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 178