support.py 3.7 KB

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