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