erroneous.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. """
  10. from __future__ import division, absolute_import
  11. from twisted.trial import unittest, util
  12. from twisted.internet import reactor, protocol, defer
  13. class FoolishError(Exception):
  14. pass
  15. class FailureInSetUpMixin(object):
  16. def setUp(self):
  17. raise FoolishError("I am a broken setUp method")
  18. def test_noop(self):
  19. pass
  20. class SynchronousTestFailureInSetUp(
  21. FailureInSetUpMixin, unittest.SynchronousTestCase):
  22. pass
  23. class AsynchronousTestFailureInSetUp(
  24. FailureInSetUpMixin, unittest.TestCase):
  25. pass
  26. class FailureInTearDownMixin(object):
  27. def tearDown(self):
  28. raise FoolishError("I am a broken tearDown method")
  29. def test_noop(self):
  30. pass
  31. class SynchronousTestFailureInTearDown(
  32. FailureInTearDownMixin, unittest.SynchronousTestCase):
  33. pass
  34. class AsynchronousTestFailureInTearDown(
  35. FailureInTearDownMixin, unittest.TestCase):
  36. pass
  37. class TestRegularFail(unittest.SynchronousTestCase):
  38. def test_fail(self):
  39. self.fail("I fail")
  40. def test_subfail(self):
  41. self.subroutine()
  42. def subroutine(self):
  43. self.fail("I fail inside")
  44. class TestAsynchronousFail(unittest.TestCase):
  45. """
  46. Test failures for L{unittest.TestCase} based classes.
  47. """
  48. def test_fail(self):
  49. """
  50. A test which fails in the callback of the returned L{defer.Deferred}.
  51. """
  52. d = defer.Deferred()
  53. d.addCallback(self._later)
  54. reactor.callLater(0, d.callback, None)
  55. return d
  56. def _later(self, res):
  57. self.fail("I fail later")
  58. def test_exception(self):
  59. """
  60. A test which raises an exception synchronously.
  61. """
  62. raise Exception("I fail")
  63. class ErrorTest(unittest.SynchronousTestCase):
  64. """
  65. A test case which has a L{test_foo} which will raise an error.
  66. @ivar ran: boolean indicating whether L{test_foo} has been run.
  67. """
  68. ran = False
  69. def test_foo(self):
  70. """
  71. Set C{self.ran} to True and raise a C{ZeroDivisionError}
  72. """
  73. self.ran = True
  74. 1/0
  75. class TestSkipTestCase(unittest.SynchronousTestCase):
  76. pass
  77. TestSkipTestCase.skip = "skipping this test"
  78. class DelayedCall(unittest.TestCase):
  79. hiddenExceptionMsg = "something blew up"
  80. def go(self):
  81. raise RuntimeError(self.hiddenExceptionMsg)
  82. def testHiddenException(self):
  83. """
  84. What happens if an error is raised in a DelayedCall and an error is
  85. also raised in the test?
  86. L{test_reporter.ErrorReportingTests.testHiddenException} checks that
  87. both errors get reported.
  88. Note that this behaviour is deprecated. A B{real} test would return a
  89. Deferred that got triggered by the callLater. This would guarantee the
  90. delayed call error gets reported.
  91. """
  92. reactor.callLater(0, self.go)
  93. reactor.iterate(0.01)
  94. self.fail("Deliberate failure to mask the hidden exception")
  95. testHiddenException.suppress = [util.suppress(
  96. message=r'reactor\.iterate cannot be used.*',
  97. category=DeprecationWarning)]
  98. class ReactorCleanupTests(unittest.TestCase):
  99. def test_leftoverPendingCalls(self):
  100. def _():
  101. print('foo!')
  102. reactor.callLater(10000.0, _)
  103. class SocketOpenTest(unittest.TestCase):
  104. def test_socketsLeftOpen(self):
  105. f = protocol.Factory()
  106. f.protocol = protocol.Protocol
  107. reactor.listenTCP(0, f)
  108. class TimingOutDeferred(unittest.TestCase):
  109. def test_alpha(self):
  110. pass
  111. def test_deferredThatNeverFires(self):
  112. self.methodCalled = True
  113. d = defer.Deferred()
  114. return d
  115. def test_omega(self):
  116. pass
  117. def unexpectedException(self):
  118. """i will raise an unexpected exception...
  119. ... *CAUSE THAT'S THE KINDA GUY I AM*
  120. >>> 1/0
  121. """