OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2007 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 from twisted.python.compat import set | |
5 from twisted.trial import unittest, reporter, runner | |
6 | |
7 _setUpClassRuns = 0 | |
8 _tearDownClassRuns = 0 | |
9 | |
10 class NumberOfRuns(unittest.TestCase): | |
11 def setUpClass(self): | |
12 global _setUpClassRuns | |
13 _setUpClassRuns += 1 | |
14 | |
15 def test_1(self): | |
16 global _setUpClassRuns | |
17 self.failUnlessEqual(_setUpClassRuns, 1) | |
18 | |
19 def test_2(self): | |
20 global _setUpClassRuns | |
21 self.failUnlessEqual(_setUpClassRuns, 1) | |
22 | |
23 def test_3(self): | |
24 global _setUpClassRuns | |
25 self.failUnlessEqual(_setUpClassRuns, 1) | |
26 | |
27 def tearDownClass(self): | |
28 global _tearDownClassRuns | |
29 self.failUnlessEqual(_tearDownClassRuns, 0) | |
30 _tearDownClassRuns += 1 | |
31 | |
32 | |
33 class AttributeSetUp(unittest.TestCase): | |
34 def setUpClass(self): | |
35 self.x = 42 | |
36 | |
37 def setUp(self): | |
38 self.failUnless(hasattr(self, 'x'), "Attribute 'x' not set") | |
39 self.failUnlessEqual(self.x, 42) | |
40 | |
41 def test_1(self): | |
42 self.failUnlessEqual(self.x, 42) # still the same | |
43 | |
44 def test_2(self): | |
45 self.failUnlessEqual(self.x, 42) # still the same | |
46 | |
47 def tearDown(self): | |
48 self.failUnlessEqual(self.x, 42) # still the same | |
49 | |
50 def tearDownClass(self): | |
51 self.x = None | |
52 | |
53 | |
54 class AttributeManipulation(unittest.TestCase): | |
55 def setUpClass(self): | |
56 self.testsRun = 0 | |
57 | |
58 def test_1(self): | |
59 self.testsRun += 1 | |
60 | |
61 def test_2(self): | |
62 self.testsRun += 1 | |
63 | |
64 def test_3(self): | |
65 self.testsRun += 1 | |
66 | |
67 def tearDown(self): | |
68 self.failUnless(self.testsRun > 0) | |
69 | |
70 def tearDownClass(self): | |
71 self.failUnlessEqual(self.testsRun, 3) | |
72 | |
73 | |
74 class AttributeSharing(unittest.TestCase): | |
75 class AttributeSharer(unittest.TestCase): | |
76 def test_1(self): | |
77 self.first = 'test1Run' | |
78 | |
79 def test_2(self): | |
80 self.failIf(hasattr(self, 'first')) | |
81 | |
82 class ClassAttributeSharer(AttributeSharer): | |
83 def setUpClass(self): | |
84 pass | |
85 | |
86 def test_3(self): | |
87 self.failUnlessEqual('test1Run', self.first) | |
88 | |
89 def setUp(self): | |
90 self.loader = runner.TestLoader() | |
91 | |
92 def test_normal(self): | |
93 result = reporter.TestResult() | |
94 suite = self.loader.loadClass(AttributeSharing.AttributeSharer) | |
95 suite.run(result) | |
96 self.failUnlessEqual(result.errors, []) | |
97 self.failUnlessEqual(result.failures, []) | |
98 | |
99 def test_shared(self): | |
100 result = reporter.TestResult() | |
101 suite = self.loader.loadClass(AttributeSharing.ClassAttributeSharer) | |
102 suite.run(result) | |
103 self.failUnlessEqual(result.errors, []) | |
104 self.failUnlessEqual(len(result.failures), 1) # from test_2 | |
105 self.failUnlessEqual(result.failures[0][0].shortDescription(), | |
106 'test_2') | |
107 | |
108 | |
109 class FactoryCounting(unittest.TestCase): | |
110 class MyTestCase(unittest.TestCase): | |
111 _setUpClassRun = 0 | |
112 _tearDownClassRun = 0 | |
113 def setUpClass(self): | |
114 self.__class__._setUpClassRun += 1 | |
115 | |
116 def test_1(self): | |
117 pass | |
118 | |
119 def test_2(self): | |
120 pass | |
121 | |
122 def tearDownClass(self): | |
123 self.__class__._tearDownClassRun += 1 | |
124 | |
125 class AnotherTestCase(MyTestCase): | |
126 _setUpClassRun = 0 | |
127 _tearDownClassRun = 0 | |
128 def setUpClass(self): | |
129 self.__class__._setUpClassRun += 1 | |
130 raise unittest.SkipTest('reason') | |
131 | |
132 def test_1(self): | |
133 pass | |
134 | |
135 def test_2(self): | |
136 pass | |
137 | |
138 def tearDownClass(self): | |
139 self.__class__._tearDownClassRun += 1 | |
140 | |
141 | |
142 def setUp(self): | |
143 self.factory = FactoryCounting.MyTestCase | |
144 self.subFactory = FactoryCounting.AnotherTestCase | |
145 self._reset() | |
146 | |
147 def _reset(self): | |
148 self.factory._setUpClassRun = self.factory._tearDownClassRun = 0 | |
149 self.subFactory._setUpClassRun = self.subFactory._tearDownClassRun = 0 | |
150 self.factory._instances = set() | |
151 self.factory._instancesRun = set() | |
152 | |
153 def test_createAndRun(self): | |
154 test = self.factory('test_1') | |
155 self.failUnlessEqual(test._isFirst(), True) | |
156 result = reporter.TestResult() | |
157 test(result) | |
158 self.failUnlessEqual(self.factory._setUpClassRun, 1) | |
159 self.failUnlessEqual(self.factory._tearDownClassRun, 1) | |
160 | |
161 def test_createTwoAndRun(self): | |
162 tests = map(self.factory, ['test_1', 'test_2']) | |
163 self.failUnlessEqual(tests[0]._isFirst(), True) | |
164 self.failUnlessEqual(tests[1]._isFirst(), True) | |
165 result = reporter.TestResult() | |
166 tests[0](result) | |
167 self.failUnlessEqual(self.factory._setUpClassRun, 1) | |
168 self.failUnlessEqual(self.factory._tearDownClassRun, 0) | |
169 tests[1](result) | |
170 self.failUnlessEqual(self.factory._setUpClassRun, 1) | |
171 self.failUnlessEqual(self.factory._tearDownClassRun, 1) | |
172 | |
173 def test_runTwice(self): | |
174 test = self.factory('test_1') | |
175 result = reporter.TestResult() | |
176 test(result) | |
177 self.failUnlessEqual(self.factory._setUpClassRun, 1) | |
178 self.failUnlessEqual(self.factory._tearDownClassRun, 1) | |
179 test(result) | |
180 self.failUnlessEqual(self.factory._setUpClassRun, 2) | |
181 self.failUnlessEqual(self.factory._tearDownClassRun, 2) | |
182 | |
183 def test_runMultipleCopies(self): | |
184 tests = map(self.factory, ['test_1', 'test_1']) | |
185 result = reporter.TestResult() | |
186 tests[0](result) | |
187 self.failUnlessEqual(self.factory._setUpClassRun, 1) | |
188 self.failUnlessEqual(self.factory._tearDownClassRun, 0) | |
189 tests[1](result) | |
190 self.failUnlessEqual(self.factory._setUpClassRun, 1) | |
191 self.failUnlessEqual(self.factory._tearDownClassRun, 1) | |
192 | |
193 def test_skippingSetUpClass(self): | |
194 tests = map(self.subFactory, ['test_1', 'test_2']) | |
195 result = reporter.TestResult() | |
196 tests[0](result) | |
197 self.failUnlessEqual(self.subFactory._setUpClassRun, 1) | |
198 self.failUnlessEqual(self.subFactory._tearDownClassRun, 0) | |
199 tests[1](result) | |
200 self.failUnlessEqual(self.subFactory._setUpClassRun, 2) | |
201 self.failUnlessEqual(self.subFactory._tearDownClassRun, 0) | |
202 | |
OLD | NEW |