test_pyunitcompat.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. from __future__ import division, absolute_import
  4. import sys
  5. import traceback
  6. from zope.interface import implementer
  7. from twisted.python.failure import Failure
  8. from twisted.trial.unittest import SynchronousTestCase, PyUnitResultAdapter
  9. from twisted.trial.itrial import IReporter, ITestCase
  10. import unittest as pyunit
  11. class PyUnitTestTests(SynchronousTestCase):
  12. class PyUnitTest(pyunit.TestCase):
  13. def test_pass(self):
  14. pass
  15. def setUp(self):
  16. self.original = self.PyUnitTest('test_pass')
  17. self.test = ITestCase(self.original)
  18. def test_callable(self):
  19. """
  20. Tests must be callable in order to be used with Python's unittest.py.
  21. """
  22. self.assertTrue(callable(self.test),
  23. "%r is not callable." % (self.test,))
  24. class PyUnitResultTests(SynchronousTestCase):
  25. """
  26. Tests to show that PyUnitResultAdapter wraps TestResult objects from the
  27. standard library 'unittest' module in such a way as to make them usable and
  28. useful from Trial.
  29. """
  30. # Once erroneous is ported to Python 3 this can be replaced with
  31. # erroneous.ErrorTest:
  32. class ErrorTest(SynchronousTestCase):
  33. """
  34. A test case which has a L{test_foo} which will raise an error.
  35. @ivar ran: boolean indicating whether L{test_foo} has been run.
  36. """
  37. ran = False
  38. def test_foo(self):
  39. """
  40. Set C{self.ran} to True and raise a C{ZeroDivisionError}
  41. """
  42. self.ran = True
  43. 1/0
  44. def test_dontUseAdapterWhenReporterProvidesIReporter(self):
  45. """
  46. The L{PyUnitResultAdapter} is only used when the result passed to
  47. C{run} does *not* provide L{IReporter}.
  48. """
  49. @implementer(IReporter)
  50. class StubReporter(object):
  51. """
  52. A reporter which records data about calls made to it.
  53. @ivar errors: Errors passed to L{addError}.
  54. @ivar failures: Failures passed to L{addFailure}.
  55. """
  56. def __init__(self):
  57. self.errors = []
  58. self.failures = []
  59. def startTest(self, test):
  60. """
  61. Do nothing.
  62. """
  63. def stopTest(self, test):
  64. """
  65. Do nothing.
  66. """
  67. def addError(self, test, error):
  68. """
  69. Record the error.
  70. """
  71. self.errors.append(error)
  72. test = self.ErrorTest("test_foo")
  73. result = StubReporter()
  74. test.run(result)
  75. self.assertIsInstance(result.errors[0], Failure)
  76. def test_success(self):
  77. class SuccessTest(SynchronousTestCase):
  78. ran = False
  79. def test_foo(s):
  80. s.ran = True
  81. test = SuccessTest('test_foo')
  82. result = pyunit.TestResult()
  83. test.run(result)
  84. self.assertTrue(test.ran)
  85. self.assertEqual(1, result.testsRun)
  86. self.assertTrue(result.wasSuccessful())
  87. def test_failure(self):
  88. class FailureTest(SynchronousTestCase):
  89. ran = False
  90. def test_foo(s):
  91. s.ran = True
  92. s.fail('boom!')
  93. test = FailureTest('test_foo')
  94. result = pyunit.TestResult()
  95. test.run(result)
  96. self.assertTrue(test.ran)
  97. self.assertEqual(1, result.testsRun)
  98. self.assertEqual(1, len(result.failures))
  99. self.assertFalse(result.wasSuccessful())
  100. def test_error(self):
  101. test = self.ErrorTest('test_foo')
  102. result = pyunit.TestResult()
  103. test.run(result)
  104. self.assertTrue(test.ran)
  105. self.assertEqual(1, result.testsRun)
  106. self.assertEqual(1, len(result.errors))
  107. self.assertFalse(result.wasSuccessful())
  108. def test_setUpError(self):
  109. class ErrorTest(SynchronousTestCase):
  110. ran = False
  111. def setUp(self):
  112. 1/0
  113. def test_foo(s):
  114. s.ran = True
  115. test = ErrorTest('test_foo')
  116. result = pyunit.TestResult()
  117. test.run(result)
  118. self.assertFalse(test.ran)
  119. self.assertEqual(1, result.testsRun)
  120. self.assertEqual(1, len(result.errors))
  121. self.assertFalse(result.wasSuccessful())
  122. def test_tracebackFromFailure(self):
  123. """
  124. Errors added through the L{PyUnitResultAdapter} have the same traceback
  125. information as if there were no adapter at all.
  126. """
  127. try:
  128. 1/0
  129. except ZeroDivisionError:
  130. exc_info = sys.exc_info()
  131. f = Failure()
  132. pyresult = pyunit.TestResult()
  133. result = PyUnitResultAdapter(pyresult)
  134. result.addError(self, f)
  135. self.assertEqual(pyresult.errors[0][1],
  136. ''.join(traceback.format_exception(*exc_info)))
  137. def test_traceback(self):
  138. """
  139. As test_tracebackFromFailure, but covering more code.
  140. """
  141. class ErrorTest(SynchronousTestCase):
  142. exc_info = None
  143. def test_foo(self):
  144. try:
  145. 1/0
  146. except ZeroDivisionError:
  147. self.exc_info = sys.exc_info()
  148. raise
  149. test = ErrorTest('test_foo')
  150. result = pyunit.TestResult()
  151. test.run(result)
  152. # We can't test that the tracebacks are equal, because Trial's
  153. # machinery inserts a few extra frames on the top and we don't really
  154. # want to trim them off without an extremely good reason.
  155. #
  156. # So, we just test that the result's stack ends with the
  157. # exception's stack.
  158. expected_stack = ''.join(traceback.format_tb(test.exc_info[2]))
  159. observed_stack = '\n'.join(result.errors[0][1].splitlines()[:-1])
  160. self.assertEqual(expected_stack.strip(),
  161. observed_stack[-len(expected_stack):].strip())
  162. def test_tracebackFromCleanFailure(self):
  163. """
  164. Errors added through the L{PyUnitResultAdapter} have the same
  165. traceback information as if there were no adapter at all, even
  166. if the Failure that held the information has been cleaned.
  167. """
  168. try:
  169. 1/0
  170. except ZeroDivisionError:
  171. exc_info = sys.exc_info()
  172. f = Failure()
  173. f.cleanFailure()
  174. pyresult = pyunit.TestResult()
  175. result = PyUnitResultAdapter(pyresult)
  176. result.addError(self, f)
  177. self.assertEqual(pyresult.errors[0][1],
  178. ''.join(traceback.format_exception(*exc_info)))
  179. def test_trialSkip(self):
  180. """
  181. Skips using trial's skipping functionality are reported as skips in
  182. the L{pyunit.TestResult}.
  183. """
  184. class SkipTest(SynchronousTestCase):
  185. def test_skip(self):
  186. 1/0
  187. test_skip.skip = "Let's skip!"
  188. test = SkipTest('test_skip')
  189. result = pyunit.TestResult()
  190. test.run(result)
  191. self.assertEqual(result.skipped, [(test, "Let's skip!")])
  192. def test_pyunitSkip(self):
  193. """
  194. Skips using pyunit's skipping functionality are reported as skips in
  195. the L{pyunit.TestResult}.
  196. """
  197. class SkipTest(SynchronousTestCase):
  198. @pyunit.skip("skippy")
  199. def test_skip(self):
  200. 1/0
  201. test = SkipTest('test_skip')
  202. result = pyunit.TestResult()
  203. test.run(result)
  204. self.assertEqual(result.skipped, [(test, "skippy")])