support.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import unittest
  2. class TestHashing(object):
  3. """Used as a mixin for TestCase"""
  4. # Check for a valid __hash__ implementation
  5. def test_hash(self):
  6. for obj_1, obj_2 in self.eq_pairs:
  7. try:
  8. if not hash(obj_1) == hash(obj_2):
  9. self.fail("%r and %r do not hash equal" % (obj_1, obj_2))
  10. except KeyboardInterrupt:
  11. raise
  12. except Exception, e:
  13. self.fail("Problem hashing %r and %r: %s" % (obj_1, obj_2, e))
  14. for obj_1, obj_2 in self.ne_pairs:
  15. try:
  16. if hash(obj_1) == hash(obj_2):
  17. self.fail("%s and %s hash equal, but shouldn't" %
  18. (obj_1, obj_2))
  19. except KeyboardInterrupt:
  20. raise
  21. except Exception, e:
  22. self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))
  23. class TestEquality(object):
  24. """Used as a mixin for TestCase"""
  25. # Check for a valid __eq__ implementation
  26. def test_eq(self):
  27. for obj_1, obj_2 in self.eq_pairs:
  28. self.assertEqual(obj_1, obj_2)
  29. self.assertEqual(obj_2, obj_1)
  30. # Check for a valid __ne__ implementation
  31. def test_ne(self):
  32. for obj_1, obj_2 in self.ne_pairs:
  33. self.assertNotEqual(obj_1, obj_2)
  34. self.assertNotEqual(obj_2, obj_1)
  35. class LoggingResult(unittest.TestResult):
  36. def __init__(self, log):
  37. self._events = log
  38. super(LoggingResult, self).__init__()
  39. def startTest(self, test):
  40. self._events.append('startTest')
  41. super(LoggingResult, self).startTest(test)
  42. def startTestRun(self):
  43. self._events.append('startTestRun')
  44. super(LoggingResult, self).startTestRun()
  45. def stopTest(self, test):
  46. self._events.append('stopTest')
  47. super(LoggingResult, self).stopTest(test)
  48. def stopTestRun(self):
  49. self._events.append('stopTestRun')
  50. super(LoggingResult, self).stopTestRun()
  51. def addFailure(self, *args):
  52. self._events.append('addFailure')
  53. super(LoggingResult, self).addFailure(*args)
  54. def addSuccess(self, *args):
  55. self._events.append('addSuccess')
  56. super(LoggingResult, self).addSuccess(*args)
  57. def addError(self, *args):
  58. self._events.append('addError')
  59. super(LoggingResult, self).addError(*args)
  60. def addSkip(self, *args):
  61. self._events.append('addSkip')
  62. super(LoggingResult, self).addSkip(*args)
  63. def addExpectedFailure(self, *args):
  64. self._events.append('addExpectedFailure')
  65. super(LoggingResult, self).addExpectedFailure(*args)
  66. def addUnexpectedSuccess(self, *args):
  67. self._events.append('addUnexpectedSuccess')
  68. super(LoggingResult, self).addUnexpectedSuccess(*args)
  69. class ResultWithNoStartTestRunStopTestRun(object):
  70. """An object honouring TestResult before startTestRun/stopTestRun."""
  71. def __init__(self):
  72. self.failures = []
  73. self.errors = []
  74. self.testsRun = 0
  75. self.skipped = []
  76. self.expectedFailures = []
  77. self.unexpectedSuccesses = []
  78. self.shouldStop = False
  79. def startTest(self, test):
  80. pass
  81. def stopTest(self, test):
  82. pass
  83. def addError(self, test):
  84. pass
  85. def addFailure(self, test):
  86. pass
  87. def addSuccess(self, test):
  88. pass
  89. def wasSuccessful(self):
  90. return True