Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

erroneous.py 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # -*- test-case-name: twisted.trial.test.test_tests -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Definitions of test cases with various interesting error-related behaviors, to
  6. be used by test modules to exercise different features of trial's test runner.
  7. See the L{twisted.trial.test.test_tests} module docstring for details about how
  8. this code is arranged.
  9. Some of these tests are also used by L{twisted.trial._dist.test}.
  10. """
  11. from unittest import skipIf
  12. from twisted.internet import defer, protocol, reactor
  13. from twisted.internet.task import deferLater
  14. from twisted.trial import unittest, util
  15. class FoolishError(Exception):
  16. pass
  17. class LargeError(Exception):
  18. """
  19. An exception which has a string representation of at least a specified
  20. number of characters.
  21. """
  22. def __init__(self, minSize: int) -> None:
  23. Exception.__init__(self)
  24. self.minSize = minSize
  25. def __str__(self):
  26. large = "x" * self.minSize
  27. return f"LargeError<I fail: {large}>"
  28. class FailureInSetUpMixin:
  29. def setUp(self):
  30. raise FoolishError("I am a broken setUp method")
  31. def test_noop(self):
  32. pass
  33. class SynchronousTestFailureInSetUp(FailureInSetUpMixin, unittest.SynchronousTestCase):
  34. pass
  35. class AsynchronousTestFailureInSetUp(FailureInSetUpMixin, unittest.TestCase):
  36. pass
  37. class FailureInTearDownMixin:
  38. def tearDown(self):
  39. raise FoolishError("I am a broken tearDown method")
  40. def test_noop(self):
  41. pass
  42. class SynchronousTestFailureInTearDown(
  43. FailureInTearDownMixin, unittest.SynchronousTestCase
  44. ):
  45. pass
  46. class AsynchronousTestFailureInTearDown(FailureInTearDownMixin, unittest.TestCase):
  47. pass
  48. class FailureButTearDownRunsMixin:
  49. """
  50. A test fails, but its L{tearDown} still runs.
  51. """
  52. tornDown = False
  53. def tearDown(self):
  54. self.tornDown = True
  55. def test_fails(self):
  56. """
  57. A test that fails.
  58. """
  59. raise FoolishError("I am a broken test")
  60. class SynchronousTestFailureButTearDownRuns(
  61. FailureButTearDownRunsMixin, unittest.SynchronousTestCase
  62. ):
  63. pass
  64. class AsynchronousTestFailureButTearDownRuns(
  65. FailureButTearDownRunsMixin, unittest.TestCase
  66. ):
  67. pass
  68. class TestRegularFail(unittest.SynchronousTestCase):
  69. def test_fail(self):
  70. self.fail("I fail")
  71. def test_subfail(self):
  72. self.subroutine()
  73. def subroutine(self):
  74. self.fail("I fail inside")
  75. class TestAsynchronousFail(unittest.TestCase):
  76. """
  77. Test failures for L{unittest.TestCase} based classes.
  78. """
  79. text = "I fail"
  80. def test_fail(self) -> defer.Deferred[None]:
  81. """
  82. A test which fails in the callback of the returned L{defer.Deferred}.
  83. """
  84. return deferLater(reactor, 0, self.fail, "I fail later") # type: ignore[arg-type]
  85. def test_failGreaterThan64k(self) -> defer.Deferred[None]:
  86. """
  87. A test which fails in the callback of the returned L{defer.Deferred}
  88. with a very long string.
  89. """
  90. return deferLater(reactor, 0, self.fail, "I fail later: " + "x" * 2 ** 16) # type: ignore[arg-type]
  91. def test_exception(self) -> None:
  92. """
  93. A test which raises an exception synchronously.
  94. """
  95. raise Exception(self.text)
  96. def test_exceptionGreaterThan64k(self) -> None:
  97. """
  98. A test which raises an exception with a long string representation
  99. synchronously.
  100. """
  101. raise LargeError(2 ** 16)
  102. def test_exceptionGreaterThan64kEncoded(self) -> None:
  103. """
  104. A test which synchronously raises an exception with a long string
  105. representation including non-ascii content.
  106. """
  107. # The exception text itself is not greater than 64k but SNOWMAN
  108. # encodes to 3 bytes with UTF-8 so the length of the UTF-8 encoding of
  109. # the string representation of this exception will be greater than 2
  110. # ** 16.
  111. raise Exception("\N{SNOWMAN}" * 2 ** 15)
  112. class ErrorTest(unittest.SynchronousTestCase):
  113. """
  114. A test case which has a L{test_foo} which will raise an error.
  115. @ivar ran: boolean indicating whether L{test_foo} has been run.
  116. """
  117. ran = False
  118. def test_foo(self):
  119. """
  120. Set C{self.ran} to True and raise a C{ZeroDivisionError}
  121. """
  122. self.ran = True
  123. 1 / 0
  124. @skipIf(True, "skipping this test")
  125. class TestSkipTestCase(unittest.SynchronousTestCase):
  126. pass
  127. class DelayedCall(unittest.TestCase):
  128. hiddenExceptionMsg = "something blew up"
  129. def go(self):
  130. raise RuntimeError(self.hiddenExceptionMsg)
  131. def testHiddenException(self):
  132. """
  133. What happens if an error is raised in a DelayedCall and an error is
  134. also raised in the test?
  135. L{test_reporter.ErrorReportingTests.testHiddenException} checks that
  136. both errors get reported.
  137. Note that this behaviour is deprecated. A B{real} test would return a
  138. Deferred that got triggered by the callLater. This would guarantee the
  139. delayed call error gets reported.
  140. """
  141. reactor.callLater(0, self.go)
  142. reactor.iterate(0.01)
  143. self.fail("Deliberate failure to mask the hidden exception")
  144. testHiddenException.suppress = [ # type: ignore[attr-defined]
  145. util.suppress(
  146. message=r"reactor\.iterate cannot be used.*", category=DeprecationWarning
  147. )
  148. ]
  149. class ReactorCleanupTests(unittest.TestCase):
  150. def test_leftoverPendingCalls(self):
  151. def _():
  152. print("foo!")
  153. reactor.callLater(10000.0, _)
  154. class SocketOpenTest(unittest.TestCase):
  155. def test_socketsLeftOpen(self):
  156. f = protocol.Factory()
  157. f.protocol = protocol.Protocol
  158. reactor.listenTCP(0, f)
  159. class TimingOutDeferred(unittest.TestCase):
  160. def test_alpha(self):
  161. pass
  162. def test_deferredThatNeverFires(self):
  163. self.methodCalled = True
  164. d = defer.Deferred()
  165. return d
  166. def test_omega(self):
  167. pass
  168. def unexpectedException(self):
  169. """i will raise an unexpected exception...
  170. ... *CAUSE THAT'S THE KINDA GUY I AM*
  171. >>> 1/0
  172. """
  173. class EventuallyFailingTestCase(unittest.SynchronousTestCase):
  174. """
  175. A test suite that fails after it is run a few times.
  176. """
  177. n: int = 0
  178. def test_it(self):
  179. """
  180. Run successfully a few times and then fail forever after.
  181. """
  182. self.n += 1
  183. if self.n >= 5:
  184. self.fail("eventually failing")