1 | n/a | import unittest |
---|
2 | n/a | |
---|
3 | n/a | from unittest.test.support import LoggingResult |
---|
4 | n/a | |
---|
5 | n/a | |
---|
6 | n/a | class Test_TestSkipping(unittest.TestCase): |
---|
7 | n/a | |
---|
8 | n/a | def test_skipping(self): |
---|
9 | n/a | class Foo(unittest.TestCase): |
---|
10 | n/a | def test_skip_me(self): |
---|
11 | n/a | self.skipTest("skip") |
---|
12 | n/a | events = [] |
---|
13 | n/a | result = LoggingResult(events) |
---|
14 | n/a | test = Foo("test_skip_me") |
---|
15 | n/a | test.run(result) |
---|
16 | n/a | self.assertEqual(events, ['startTest', 'addSkip', 'stopTest']) |
---|
17 | n/a | self.assertEqual(result.skipped, [(test, "skip")]) |
---|
18 | n/a | |
---|
19 | n/a | # Try letting setUp skip the test now. |
---|
20 | n/a | class Foo(unittest.TestCase): |
---|
21 | n/a | def setUp(self): |
---|
22 | n/a | self.skipTest("testing") |
---|
23 | n/a | def test_nothing(self): pass |
---|
24 | n/a | events = [] |
---|
25 | n/a | result = LoggingResult(events) |
---|
26 | n/a | test = Foo("test_nothing") |
---|
27 | n/a | test.run(result) |
---|
28 | n/a | self.assertEqual(events, ['startTest', 'addSkip', 'stopTest']) |
---|
29 | n/a | self.assertEqual(result.skipped, [(test, "testing")]) |
---|
30 | n/a | self.assertEqual(result.testsRun, 1) |
---|
31 | n/a | |
---|
32 | n/a | def test_skipping_subtests(self): |
---|
33 | n/a | class Foo(unittest.TestCase): |
---|
34 | n/a | def test_skip_me(self): |
---|
35 | n/a | with self.subTest(a=1): |
---|
36 | n/a | with self.subTest(b=2): |
---|
37 | n/a | self.skipTest("skip 1") |
---|
38 | n/a | self.skipTest("skip 2") |
---|
39 | n/a | self.skipTest("skip 3") |
---|
40 | n/a | events = [] |
---|
41 | n/a | result = LoggingResult(events) |
---|
42 | n/a | test = Foo("test_skip_me") |
---|
43 | n/a | test.run(result) |
---|
44 | n/a | self.assertEqual(events, ['startTest', 'addSkip', 'addSkip', |
---|
45 | n/a | 'addSkip', 'stopTest']) |
---|
46 | n/a | self.assertEqual(len(result.skipped), 3) |
---|
47 | n/a | subtest, msg = result.skipped[0] |
---|
48 | n/a | self.assertEqual(msg, "skip 1") |
---|
49 | n/a | self.assertIsInstance(subtest, unittest.TestCase) |
---|
50 | n/a | self.assertIsNot(subtest, test) |
---|
51 | n/a | subtest, msg = result.skipped[1] |
---|
52 | n/a | self.assertEqual(msg, "skip 2") |
---|
53 | n/a | self.assertIsInstance(subtest, unittest.TestCase) |
---|
54 | n/a | self.assertIsNot(subtest, test) |
---|
55 | n/a | self.assertEqual(result.skipped[2], (test, "skip 3")) |
---|
56 | n/a | |
---|
57 | n/a | def test_skipping_decorators(self): |
---|
58 | n/a | op_table = ((unittest.skipUnless, False, True), |
---|
59 | n/a | (unittest.skipIf, True, False)) |
---|
60 | n/a | for deco, do_skip, dont_skip in op_table: |
---|
61 | n/a | class Foo(unittest.TestCase): |
---|
62 | n/a | @deco(do_skip, "testing") |
---|
63 | n/a | def test_skip(self): pass |
---|
64 | n/a | |
---|
65 | n/a | @deco(dont_skip, "testing") |
---|
66 | n/a | def test_dont_skip(self): pass |
---|
67 | n/a | test_do_skip = Foo("test_skip") |
---|
68 | n/a | test_dont_skip = Foo("test_dont_skip") |
---|
69 | n/a | suite = unittest.TestSuite([test_do_skip, test_dont_skip]) |
---|
70 | n/a | events = [] |
---|
71 | n/a | result = LoggingResult(events) |
---|
72 | n/a | suite.run(result) |
---|
73 | n/a | self.assertEqual(len(result.skipped), 1) |
---|
74 | n/a | expected = ['startTest', 'addSkip', 'stopTest', |
---|
75 | n/a | 'startTest', 'addSuccess', 'stopTest'] |
---|
76 | n/a | self.assertEqual(events, expected) |
---|
77 | n/a | self.assertEqual(result.testsRun, 2) |
---|
78 | n/a | self.assertEqual(result.skipped, [(test_do_skip, "testing")]) |
---|
79 | n/a | self.assertTrue(result.wasSuccessful()) |
---|
80 | n/a | |
---|
81 | n/a | def test_skip_class(self): |
---|
82 | n/a | @unittest.skip("testing") |
---|
83 | n/a | class Foo(unittest.TestCase): |
---|
84 | n/a | def test_1(self): |
---|
85 | n/a | record.append(1) |
---|
86 | n/a | record = [] |
---|
87 | n/a | result = unittest.TestResult() |
---|
88 | n/a | test = Foo("test_1") |
---|
89 | n/a | suite = unittest.TestSuite([test]) |
---|
90 | n/a | suite.run(result) |
---|
91 | n/a | self.assertEqual(result.skipped, [(test, "testing")]) |
---|
92 | n/a | self.assertEqual(record, []) |
---|
93 | n/a | |
---|
94 | n/a | def test_skip_non_unittest_class(self): |
---|
95 | n/a | @unittest.skip("testing") |
---|
96 | n/a | class Mixin: |
---|
97 | n/a | def test_1(self): |
---|
98 | n/a | record.append(1) |
---|
99 | n/a | class Foo(Mixin, unittest.TestCase): |
---|
100 | n/a | pass |
---|
101 | n/a | record = [] |
---|
102 | n/a | result = unittest.TestResult() |
---|
103 | n/a | test = Foo("test_1") |
---|
104 | n/a | suite = unittest.TestSuite([test]) |
---|
105 | n/a | suite.run(result) |
---|
106 | n/a | self.assertEqual(result.skipped, [(test, "testing")]) |
---|
107 | n/a | self.assertEqual(record, []) |
---|
108 | n/a | |
---|
109 | n/a | def test_expected_failure(self): |
---|
110 | n/a | class Foo(unittest.TestCase): |
---|
111 | n/a | @unittest.expectedFailure |
---|
112 | n/a | def test_die(self): |
---|
113 | n/a | self.fail("help me!") |
---|
114 | n/a | events = [] |
---|
115 | n/a | result = LoggingResult(events) |
---|
116 | n/a | test = Foo("test_die") |
---|
117 | n/a | test.run(result) |
---|
118 | n/a | self.assertEqual(events, |
---|
119 | n/a | ['startTest', 'addExpectedFailure', 'stopTest']) |
---|
120 | n/a | self.assertEqual(result.expectedFailures[0][0], test) |
---|
121 | n/a | self.assertTrue(result.wasSuccessful()) |
---|
122 | n/a | |
---|
123 | n/a | def test_expected_failure_with_wrapped_class(self): |
---|
124 | n/a | @unittest.expectedFailure |
---|
125 | n/a | class Foo(unittest.TestCase): |
---|
126 | n/a | def test_1(self): |
---|
127 | n/a | self.assertTrue(False) |
---|
128 | n/a | |
---|
129 | n/a | events = [] |
---|
130 | n/a | result = LoggingResult(events) |
---|
131 | n/a | test = Foo("test_1") |
---|
132 | n/a | test.run(result) |
---|
133 | n/a | self.assertEqual(events, |
---|
134 | n/a | ['startTest', 'addExpectedFailure', 'stopTest']) |
---|
135 | n/a | self.assertEqual(result.expectedFailures[0][0], test) |
---|
136 | n/a | self.assertTrue(result.wasSuccessful()) |
---|
137 | n/a | |
---|
138 | n/a | def test_expected_failure_with_wrapped_subclass(self): |
---|
139 | n/a | class Foo(unittest.TestCase): |
---|
140 | n/a | def test_1(self): |
---|
141 | n/a | self.assertTrue(False) |
---|
142 | n/a | |
---|
143 | n/a | @unittest.expectedFailure |
---|
144 | n/a | class Bar(Foo): |
---|
145 | n/a | pass |
---|
146 | n/a | |
---|
147 | n/a | events = [] |
---|
148 | n/a | result = LoggingResult(events) |
---|
149 | n/a | test = Bar("test_1") |
---|
150 | n/a | test.run(result) |
---|
151 | n/a | self.assertEqual(events, |
---|
152 | n/a | ['startTest', 'addExpectedFailure', 'stopTest']) |
---|
153 | n/a | self.assertEqual(result.expectedFailures[0][0], test) |
---|
154 | n/a | self.assertTrue(result.wasSuccessful()) |
---|
155 | n/a | |
---|
156 | n/a | def test_expected_failure_subtests(self): |
---|
157 | n/a | # A failure in any subtest counts as the expected failure of the |
---|
158 | n/a | # whole test. |
---|
159 | n/a | class Foo(unittest.TestCase): |
---|
160 | n/a | @unittest.expectedFailure |
---|
161 | n/a | def test_die(self): |
---|
162 | n/a | with self.subTest(): |
---|
163 | n/a | # This one succeeds |
---|
164 | n/a | pass |
---|
165 | n/a | with self.subTest(): |
---|
166 | n/a | self.fail("help me!") |
---|
167 | n/a | with self.subTest(): |
---|
168 | n/a | # This one doesn't get executed |
---|
169 | n/a | self.fail("shouldn't come here") |
---|
170 | n/a | events = [] |
---|
171 | n/a | result = LoggingResult(events) |
---|
172 | n/a | test = Foo("test_die") |
---|
173 | n/a | test.run(result) |
---|
174 | n/a | self.assertEqual(events, |
---|
175 | n/a | ['startTest', 'addSubTestSuccess', |
---|
176 | n/a | 'addExpectedFailure', 'stopTest']) |
---|
177 | n/a | self.assertEqual(len(result.expectedFailures), 1) |
---|
178 | n/a | self.assertIs(result.expectedFailures[0][0], test) |
---|
179 | n/a | self.assertTrue(result.wasSuccessful()) |
---|
180 | n/a | |
---|
181 | n/a | def test_unexpected_success(self): |
---|
182 | n/a | class Foo(unittest.TestCase): |
---|
183 | n/a | @unittest.expectedFailure |
---|
184 | n/a | def test_die(self): |
---|
185 | n/a | pass |
---|
186 | n/a | events = [] |
---|
187 | n/a | result = LoggingResult(events) |
---|
188 | n/a | test = Foo("test_die") |
---|
189 | n/a | test.run(result) |
---|
190 | n/a | self.assertEqual(events, |
---|
191 | n/a | ['startTest', 'addUnexpectedSuccess', 'stopTest']) |
---|
192 | n/a | self.assertFalse(result.failures) |
---|
193 | n/a | self.assertEqual(result.unexpectedSuccesses, [test]) |
---|
194 | n/a | self.assertFalse(result.wasSuccessful()) |
---|
195 | n/a | |
---|
196 | n/a | def test_unexpected_success_subtests(self): |
---|
197 | n/a | # Success in all subtests counts as the unexpected success of |
---|
198 | n/a | # the whole test. |
---|
199 | n/a | class Foo(unittest.TestCase): |
---|
200 | n/a | @unittest.expectedFailure |
---|
201 | n/a | def test_die(self): |
---|
202 | n/a | with self.subTest(): |
---|
203 | n/a | # This one succeeds |
---|
204 | n/a | pass |
---|
205 | n/a | with self.subTest(): |
---|
206 | n/a | # So does this one |
---|
207 | n/a | pass |
---|
208 | n/a | events = [] |
---|
209 | n/a | result = LoggingResult(events) |
---|
210 | n/a | test = Foo("test_die") |
---|
211 | n/a | test.run(result) |
---|
212 | n/a | self.assertEqual(events, |
---|
213 | n/a | ['startTest', |
---|
214 | n/a | 'addSubTestSuccess', 'addSubTestSuccess', |
---|
215 | n/a | 'addUnexpectedSuccess', 'stopTest']) |
---|
216 | n/a | self.assertFalse(result.failures) |
---|
217 | n/a | self.assertEqual(result.unexpectedSuccesses, [test]) |
---|
218 | n/a | self.assertFalse(result.wasSuccessful()) |
---|
219 | n/a | |
---|
220 | n/a | def test_skip_doesnt_run_setup(self): |
---|
221 | n/a | class Foo(unittest.TestCase): |
---|
222 | n/a | wasSetUp = False |
---|
223 | n/a | wasTornDown = False |
---|
224 | n/a | def setUp(self): |
---|
225 | n/a | Foo.wasSetUp = True |
---|
226 | n/a | def tornDown(self): |
---|
227 | n/a | Foo.wasTornDown = True |
---|
228 | n/a | @unittest.skip('testing') |
---|
229 | n/a | def test_1(self): |
---|
230 | n/a | pass |
---|
231 | n/a | |
---|
232 | n/a | result = unittest.TestResult() |
---|
233 | n/a | test = Foo("test_1") |
---|
234 | n/a | suite = unittest.TestSuite([test]) |
---|
235 | n/a | suite.run(result) |
---|
236 | n/a | self.assertEqual(result.skipped, [(test, "testing")]) |
---|
237 | n/a | self.assertFalse(Foo.wasSetUp) |
---|
238 | n/a | self.assertFalse(Foo.wasTornDown) |
---|
239 | n/a | |
---|
240 | n/a | def test_decorated_skip(self): |
---|
241 | n/a | def decorator(func): |
---|
242 | n/a | def inner(*a): |
---|
243 | n/a | return func(*a) |
---|
244 | n/a | return inner |
---|
245 | n/a | |
---|
246 | n/a | class Foo(unittest.TestCase): |
---|
247 | n/a | @decorator |
---|
248 | n/a | @unittest.skip('testing') |
---|
249 | n/a | def test_1(self): |
---|
250 | n/a | pass |
---|
251 | n/a | |
---|
252 | n/a | result = unittest.TestResult() |
---|
253 | n/a | test = Foo("test_1") |
---|
254 | n/a | suite = unittest.TestSuite([test]) |
---|
255 | n/a | suite.run(result) |
---|
256 | n/a | self.assertEqual(result.skipped, [(test, "testing")]) |
---|
257 | n/a | |
---|
258 | n/a | |
---|
259 | n/a | if __name__ == "__main__": |
---|
260 | n/a | unittest.main() |
---|