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.

detests.py 5.5KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for Deferred handling by L{twisted.trial.unittest.TestCase}.
  5. """
  6. from twisted.internet import defer, reactor, threads
  7. from twisted.python.util import runWithWarningsSuppressed
  8. from twisted.trial import unittest
  9. from twisted.trial.util import suppress as SUPPRESS
  10. class DeferredSetUpOK(unittest.TestCase):
  11. def setUp(self):
  12. d = defer.succeed("value")
  13. d.addCallback(self._cb_setUpCalled)
  14. return d
  15. def _cb_setUpCalled(self, ignored):
  16. self._setUpCalled = True
  17. def test_ok(self):
  18. self.assertTrue(self._setUpCalled)
  19. class DeferredSetUpFail(unittest.TestCase):
  20. testCalled = False
  21. def setUp(self):
  22. return defer.fail(unittest.FailTest("i fail"))
  23. def test_ok(self):
  24. DeferredSetUpFail.testCalled = True
  25. self.fail("I should not get called")
  26. class DeferredSetUpCallbackFail(unittest.TestCase):
  27. testCalled = False
  28. def setUp(self):
  29. d = defer.succeed("value")
  30. d.addCallback(self._cb_setUpCalled)
  31. return d
  32. def _cb_setUpCalled(self, ignored):
  33. self.fail("deliberate failure")
  34. def test_ok(self):
  35. DeferredSetUpCallbackFail.testCalled = True
  36. class DeferredSetUpError(unittest.TestCase):
  37. testCalled = False
  38. def setUp(self):
  39. return defer.fail(RuntimeError("deliberate error"))
  40. def test_ok(self):
  41. DeferredSetUpError.testCalled = True
  42. class DeferredSetUpNeverFire(unittest.TestCase):
  43. testCalled = False
  44. def setUp(self):
  45. return defer.Deferred()
  46. def test_ok(self):
  47. DeferredSetUpNeverFire.testCalled = True
  48. class DeferredSetUpSkip(unittest.TestCase):
  49. testCalled = False
  50. def setUp(self):
  51. d = defer.succeed("value")
  52. d.addCallback(self._cb1)
  53. return d
  54. def _cb1(self, ignored):
  55. raise unittest.SkipTest("skip me")
  56. def test_ok(self):
  57. DeferredSetUpSkip.testCalled = True
  58. class DeferredTests(unittest.TestCase):
  59. touched = False
  60. def _cb_fail(self, reason):
  61. self.fail(reason)
  62. def _cb_error(self, reason):
  63. raise RuntimeError(reason)
  64. def _cb_skip(self, reason):
  65. raise unittest.SkipTest(reason)
  66. def _touchClass(self, ignored):
  67. self.__class__.touched = True
  68. def setUp(self):
  69. self.__class__.touched = False
  70. def test_pass(self):
  71. return defer.succeed("success")
  72. def test_passGenerated(self):
  73. self._touchClass(None)
  74. yield None
  75. test_passGenerated = runWithWarningsSuppressed(
  76. [
  77. SUPPRESS(
  78. message="twisted.internet.defer.deferredGenerator was " "deprecated"
  79. )
  80. ],
  81. defer.deferredGenerator,
  82. test_passGenerated,
  83. )
  84. @defer.inlineCallbacks
  85. def test_passInlineCallbacks(self):
  86. """
  87. Test case that is decorated with L{defer.inlineCallbacks}.
  88. """
  89. self._touchClass(None)
  90. yield None
  91. def test_fail(self):
  92. return defer.fail(self.failureException("I fail"))
  93. def test_failureInCallback(self):
  94. d = defer.succeed("fail")
  95. d.addCallback(self._cb_fail)
  96. return d
  97. def test_errorInCallback(self):
  98. d = defer.succeed("error")
  99. d.addCallback(self._cb_error)
  100. return d
  101. def test_skip(self):
  102. d = defer.succeed("skip")
  103. d.addCallback(self._cb_skip)
  104. d.addCallback(self._touchClass)
  105. return d
  106. def test_thread(self):
  107. return threads.deferToThread(lambda: None)
  108. def test_expectedFailure(self):
  109. d = defer.succeed("todo")
  110. d.addCallback(self._cb_error)
  111. return d
  112. test_expectedFailure.todo = "Expected failure" # type: ignore[attr-defined]
  113. class TimeoutTests(unittest.TestCase):
  114. timedOut = None
  115. def test_pass(self):
  116. d = defer.Deferred()
  117. reactor.callLater(0, d.callback, "hoorj!")
  118. return d
  119. test_pass.timeout = 2 # type: ignore[attr-defined]
  120. def test_passDefault(self):
  121. # test default timeout
  122. d = defer.Deferred()
  123. reactor.callLater(0, d.callback, "hoorj!")
  124. return d
  125. def test_timeout(self):
  126. return defer.Deferred()
  127. test_timeout.timeout = 0.1 # type: ignore[attr-defined]
  128. def test_timeoutZero(self):
  129. return defer.Deferred()
  130. test_timeoutZero.timeout = 0 # type: ignore[attr-defined]
  131. def test_expectedFailure(self):
  132. return defer.Deferred()
  133. test_expectedFailure.timeout = 0.1 # type: ignore[attr-defined]
  134. test_expectedFailure.todo = "i will get it right, eventually" # type: ignore[attr-defined]
  135. def test_skip(self):
  136. return defer.Deferred()
  137. test_skip.timeout = 0.1 # type: ignore[attr-defined]
  138. test_skip.skip = "i will get it right, eventually" # type: ignore[attr-defined]
  139. def test_errorPropagation(self):
  140. def timedOut(err):
  141. self.__class__.timedOut = err
  142. return err
  143. d = defer.Deferred()
  144. d.addErrback(timedOut)
  145. return d
  146. test_errorPropagation.timeout = 0.1 # type: ignore[attr-defined]
  147. def test_calledButNeverCallback(self):
  148. d = defer.Deferred()
  149. def neverFire(r):
  150. return defer.Deferred()
  151. d.addCallback(neverFire)
  152. d.callback(1)
  153. return d
  154. test_calledButNeverCallback.timeout = 0.1 # type: ignore[attr-defined]
  155. class TestClassTimeoutAttribute(unittest.TestCase):
  156. timeout = 0.2
  157. def setUp(self):
  158. self.d = defer.Deferred()
  159. def testMethod(self):
  160. self.methodCalled = True
  161. return self.d