1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# -*- test-case-name: twisted.trial.test.test_tests -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Definitions of test cases with various interesting error-related behaviors, to
be used by test modules to exercise different features of trial's test runner.
See the L{twisted.trial.test.test_tests} module docstring for details about how
this code is arranged.
"""
from __future__ import division
from twisted.trial import unittest, util
from twisted.internet import reactor, protocol, defer
class FoolishError(Exception):
pass
class FailureInSetUpMixin(object):
def setUp(self):
raise FoolishError, "I am a broken setUp method"
def test_noop(self):
pass
class SynchronousTestFailureInSetUp(
FailureInSetUpMixin, unittest.SynchronousTestCase):
pass
class AsynchronousTestFailureInSetUp(
FailureInSetUpMixin, unittest.TestCase):
pass
class FailureInTearDownMixin(object):
def tearDown(self):
raise FoolishError, "I am a broken tearDown method"
def test_noop(self):
pass
class SynchronousTestFailureInTearDown(
FailureInTearDownMixin, unittest.SynchronousTestCase):
pass
class AsynchronousTestFailureInTearDown(
FailureInTearDownMixin, unittest.TestCase):
pass
class TestRegularFail(unittest.SynchronousTestCase):
def test_fail(self):
self.fail("I fail")
def test_subfail(self):
self.subroutine()
def subroutine(self):
self.fail("I fail inside")
class TestFailureInDeferredChain(unittest.TestCase):
def test_fail(self):
d = defer.Deferred()
d.addCallback(self._later)
reactor.callLater(0, d.callback, None)
return d
def _later(self, res):
self.fail("I fail later")
class ErrorTest(unittest.SynchronousTestCase):
"""
A test case which has a L{test_foo} which will raise an error.
@ivar ran: boolean indicating whether L{test_foo} has been run.
"""
ran = False
def test_foo(self):
"""
Set C{self.ran} to True and raise a C{ZeroDivisionError}
"""
self.ran = True
1/0
class TestSkipTestCase(unittest.SynchronousTestCase):
pass
TestSkipTestCase.skip = "skipping this test"
class DelayedCall(unittest.TestCase):
hiddenExceptionMsg = "something blew up"
def go(self):
raise RuntimeError(self.hiddenExceptionMsg)
def testHiddenException(self):
"""
What happens if an error is raised in a DelayedCall and an error is
also raised in the test?
L{test_reporter.TestErrorReporting.testHiddenException} checks that
both errors get reported.
Note that this behaviour is deprecated. A B{real} test would return a
Deferred that got triggered by the callLater. This would guarantee the
delayed call error gets reported.
"""
reactor.callLater(0, self.go)
reactor.iterate(0.01)
self.fail("Deliberate failure to mask the hidden exception")
testHiddenException.suppress = [util.suppress(
message=r'reactor\.iterate cannot be used.*',
category=DeprecationWarning)]
class ReactorCleanupTests(unittest.TestCase):
def test_leftoverPendingCalls(self):
def _():
print 'foo!'
reactor.callLater(10000.0, _)
class SocketOpenTest(unittest.TestCase):
def test_socketsLeftOpen(self):
f = protocol.Factory()
f.protocol = protocol.Protocol
reactor.listenTCP(0, f)
class TimingOutDeferred(unittest.TestCase):
def test_alpha(self):
pass
def test_deferredThatNeverFires(self):
self.methodCalled = True
d = defer.Deferred()
return d
def test_omega(self):
pass
def unexpectedException(self):
"""i will raise an unexpected exception...
... *CAUSE THAT'S THE KINDA GUY I AM*
>>> 1/0
"""
|