• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2
3from unittest.test.support import LoggingResult
4
5
6class Test_TestSkipping(unittest.TestCase):
7
8    def test_skipping(self):
9        class Foo(unittest.TestCase):
10            def defaultTestResult(self):
11                return LoggingResult(events)
12            def test_skip_me(self):
13                self.skipTest("skip")
14        events = []
15        result = LoggingResult(events)
16        test = Foo("test_skip_me")
17        self.assertIs(test.run(result), result)
18        self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
19        self.assertEqual(result.skipped, [(test, "skip")])
20
21        events = []
22        result = test.run()
23        self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip',
24                                  'stopTest', 'stopTestRun'])
25        self.assertEqual(result.skipped, [(test, "skip")])
26        self.assertEqual(result.testsRun, 1)
27
28        # Try letting setUp skip the test now.
29        class Foo(unittest.TestCase):
30            def defaultTestResult(self):
31                return LoggingResult(events)
32            def setUp(self):
33                self.skipTest("testing")
34            def test_nothing(self): pass
35        events = []
36        result = LoggingResult(events)
37        test = Foo("test_nothing")
38        self.assertIs(test.run(result), result)
39        self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
40        self.assertEqual(result.skipped, [(test, "testing")])
41        self.assertEqual(result.testsRun, 1)
42
43        events = []
44        result = test.run()
45        self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip',
46                                  'stopTest', 'stopTestRun'])
47        self.assertEqual(result.skipped, [(test, "testing")])
48        self.assertEqual(result.testsRun, 1)
49
50    def test_skipping_subtests(self):
51        class Foo(unittest.TestCase):
52            def defaultTestResult(self):
53                return LoggingResult(events)
54            def test_skip_me(self):
55                with self.subTest(a=1):
56                    with self.subTest(b=2):
57                        self.skipTest("skip 1")
58                    self.skipTest("skip 2")
59                self.skipTest("skip 3")
60        events = []
61        result = LoggingResult(events)
62        test = Foo("test_skip_me")
63        self.assertIs(test.run(result), result)
64        self.assertEqual(events, ['startTest', 'addSkip', 'addSkip',
65                                  'addSkip', 'stopTest'])
66        self.assertEqual(len(result.skipped), 3)
67        subtest, msg = result.skipped[0]
68        self.assertEqual(msg, "skip 1")
69        self.assertIsInstance(subtest, unittest.TestCase)
70        self.assertIsNot(subtest, test)
71        subtest, msg = result.skipped[1]
72        self.assertEqual(msg, "skip 2")
73        self.assertIsInstance(subtest, unittest.TestCase)
74        self.assertIsNot(subtest, test)
75        self.assertEqual(result.skipped[2], (test, "skip 3"))
76
77        events = []
78        result = test.run()
79        self.assertEqual(events,
80                         ['startTestRun', 'startTest', 'addSkip', 'addSkip',
81                          'addSkip', 'stopTest', 'stopTestRun'])
82        self.assertEqual([msg for subtest, msg in result.skipped],
83                         ['skip 1', 'skip 2', 'skip 3'])
84
85    def test_skipping_decorators(self):
86        op_table = ((unittest.skipUnless, False, True),
87                    (unittest.skipIf, True, False))
88        for deco, do_skip, dont_skip in op_table:
89            class Foo(unittest.TestCase):
90                def defaultTestResult(self):
91                    return LoggingResult(events)
92
93                @deco(do_skip, "testing")
94                def test_skip(self): pass
95
96                @deco(dont_skip, "testing")
97                def test_dont_skip(self): pass
98            test_do_skip = Foo("test_skip")
99            test_dont_skip = Foo("test_dont_skip")
100
101            suite = unittest.TestSuite([test_do_skip, test_dont_skip])
102            events = []
103            result = LoggingResult(events)
104            self.assertIs(suite.run(result), result)
105            self.assertEqual(len(result.skipped), 1)
106            expected = ['startTest', 'addSkip', 'stopTest',
107                        'startTest', 'addSuccess', 'stopTest']
108            self.assertEqual(events, expected)
109            self.assertEqual(result.testsRun, 2)
110            self.assertEqual(result.skipped, [(test_do_skip, "testing")])
111            self.assertTrue(result.wasSuccessful())
112
113            events = []
114            result = test_do_skip.run()
115            self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip',
116                                      'stopTest', 'stopTestRun'])
117            self.assertEqual(result.skipped, [(test_do_skip, "testing")])
118
119            events = []
120            result = test_dont_skip.run()
121            self.assertEqual(events, ['startTestRun', 'startTest', 'addSuccess',
122                                      'stopTest', 'stopTestRun'])
123            self.assertEqual(result.skipped, [])
124
125    def test_skip_class(self):
126        @unittest.skip("testing")
127        class Foo(unittest.TestCase):
128            def defaultTestResult(self):
129                return LoggingResult(events)
130            def test_1(self):
131                record.append(1)
132        events = []
133        record = []
134        result = LoggingResult(events)
135        test = Foo("test_1")
136        suite = unittest.TestSuite([test])
137        self.assertIs(suite.run(result), result)
138        self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
139        self.assertEqual(result.skipped, [(test, "testing")])
140        self.assertEqual(record, [])
141
142        events = []
143        result = test.run()
144        self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip',
145                                  'stopTest', 'stopTestRun'])
146        self.assertEqual(result.skipped, [(test, "testing")])
147        self.assertEqual(record, [])
148
149    def test_skip_non_unittest_class(self):
150        @unittest.skip("testing")
151        class Mixin:
152            def test_1(self):
153                record.append(1)
154        class Foo(Mixin, unittest.TestCase):
155            pass
156        record = []
157        result = unittest.TestResult()
158        test = Foo("test_1")
159        suite = unittest.TestSuite([test])
160        self.assertIs(suite.run(result), result)
161        self.assertEqual(result.skipped, [(test, "testing")])
162        self.assertEqual(record, [])
163
164    def test_skip_in_setup(self):
165        class Foo(unittest.TestCase):
166            def setUp(self):
167                self.skipTest("skip")
168            def test_skip_me(self):
169                self.fail("shouldn't come here")
170        events = []
171        result = LoggingResult(events)
172        test = Foo("test_skip_me")
173        self.assertIs(test.run(result), result)
174        self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
175        self.assertEqual(result.skipped, [(test, "skip")])
176
177    def test_skip_in_cleanup(self):
178        class Foo(unittest.TestCase):
179            def test_skip_me(self):
180                pass
181            def tearDown(self):
182                self.skipTest("skip")
183        events = []
184        result = LoggingResult(events)
185        test = Foo("test_skip_me")
186        self.assertIs(test.run(result), result)
187        self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
188        self.assertEqual(result.skipped, [(test, "skip")])
189
190    def test_failure_and_skip_in_cleanup(self):
191        class Foo(unittest.TestCase):
192            def test_skip_me(self):
193                self.fail("fail")
194            def tearDown(self):
195                self.skipTest("skip")
196        events = []
197        result = LoggingResult(events)
198        test = Foo("test_skip_me")
199        self.assertIs(test.run(result), result)
200        self.assertEqual(events, ['startTest', 'addSkip', 'addFailure', 'stopTest'])
201        self.assertEqual(result.skipped, [(test, "skip")])
202
203    def test_skipping_and_fail_in_cleanup(self):
204        class Foo(unittest.TestCase):
205            def test_skip_me(self):
206                self.skipTest("skip")
207            def tearDown(self):
208                self.fail("fail")
209        events = []
210        result = LoggingResult(events)
211        test = Foo("test_skip_me")
212        self.assertIs(test.run(result), result)
213        self.assertEqual(events, ['startTest', 'addSkip', 'addFailure', 'stopTest'])
214        self.assertEqual(result.skipped, [(test, "skip")])
215
216    def test_expected_failure(self):
217        class Foo(unittest.TestCase):
218            @unittest.expectedFailure
219            def test_die(self):
220                self.fail("help me!")
221        events = []
222        result = LoggingResult(events)
223        test = Foo("test_die")
224        self.assertIs(test.run(result), result)
225        self.assertEqual(events,
226                         ['startTest', 'addExpectedFailure', 'stopTest'])
227        self.assertFalse(result.failures)
228        self.assertEqual(result.expectedFailures[0][0], test)
229        self.assertFalse(result.unexpectedSuccesses)
230        self.assertTrue(result.wasSuccessful())
231
232    def test_expected_failure_with_wrapped_class(self):
233        @unittest.expectedFailure
234        class Foo(unittest.TestCase):
235            def test_1(self):
236                self.assertTrue(False)
237
238        events = []
239        result = LoggingResult(events)
240        test = Foo("test_1")
241        self.assertIs(test.run(result), result)
242        self.assertEqual(events,
243                         ['startTest', 'addExpectedFailure', 'stopTest'])
244        self.assertFalse(result.failures)
245        self.assertEqual(result.expectedFailures[0][0], test)
246        self.assertFalse(result.unexpectedSuccesses)
247        self.assertTrue(result.wasSuccessful())
248
249    def test_expected_failure_with_wrapped_subclass(self):
250        class Foo(unittest.TestCase):
251            def test_1(self):
252                self.assertTrue(False)
253
254        @unittest.expectedFailure
255        class Bar(Foo):
256            pass
257
258        events = []
259        result = LoggingResult(events)
260        test = Bar("test_1")
261        self.assertIs(test.run(result), result)
262        self.assertEqual(events,
263                         ['startTest', 'addExpectedFailure', 'stopTest'])
264        self.assertFalse(result.failures)
265        self.assertEqual(result.expectedFailures[0][0], test)
266        self.assertFalse(result.unexpectedSuccesses)
267        self.assertTrue(result.wasSuccessful())
268
269    def test_expected_failure_subtests(self):
270        # A failure in any subtest counts as the expected failure of the
271        # whole test.
272        class Foo(unittest.TestCase):
273            @unittest.expectedFailure
274            def test_die(self):
275                with self.subTest():
276                    # This one succeeds
277                    pass
278                with self.subTest():
279                    self.fail("help me!")
280                with self.subTest():
281                    # This one doesn't get executed
282                    self.fail("shouldn't come here")
283        events = []
284        result = LoggingResult(events)
285        test = Foo("test_die")
286        self.assertIs(test.run(result), result)
287        self.assertEqual(events,
288                         ['startTest', 'addSubTestSuccess',
289                          'addExpectedFailure', 'stopTest'])
290        self.assertFalse(result.failures)
291        self.assertEqual(len(result.expectedFailures), 1)
292        self.assertIs(result.expectedFailures[0][0], test)
293        self.assertFalse(result.unexpectedSuccesses)
294        self.assertTrue(result.wasSuccessful())
295
296    def test_expected_failure_and_fail_in_cleanup(self):
297        class Foo(unittest.TestCase):
298            @unittest.expectedFailure
299            def test_die(self):
300                self.fail("help me!")
301            def tearDown(self):
302                self.fail("bad tearDown")
303        events = []
304        result = LoggingResult(events)
305        test = Foo("test_die")
306        self.assertIs(test.run(result), result)
307        self.assertEqual(events,
308                         ['startTest', 'addFailure', 'stopTest'])
309        self.assertEqual(len(result.failures), 1)
310        self.assertIn('AssertionError: bad tearDown', result.failures[0][1])
311        self.assertFalse(result.expectedFailures)
312        self.assertFalse(result.unexpectedSuccesses)
313        self.assertFalse(result.wasSuccessful())
314
315    def test_expected_failure_and_skip_in_cleanup(self):
316        class Foo(unittest.TestCase):
317            @unittest.expectedFailure
318            def test_die(self):
319                self.fail("help me!")
320            def tearDown(self):
321                self.skipTest("skip")
322        events = []
323        result = LoggingResult(events)
324        test = Foo("test_die")
325        self.assertIs(test.run(result), result)
326        self.assertEqual(events,
327                         ['startTest', 'addSkip', 'stopTest'])
328        self.assertFalse(result.failures)
329        self.assertFalse(result.expectedFailures)
330        self.assertFalse(result.unexpectedSuccesses)
331        self.assertEqual(result.skipped, [(test, "skip")])
332        self.assertTrue(result.wasSuccessful())
333
334    def test_unexpected_success(self):
335        class Foo(unittest.TestCase):
336            @unittest.expectedFailure
337            def test_die(self):
338                pass
339        events = []
340        result = LoggingResult(events)
341        test = Foo("test_die")
342        self.assertIs(test.run(result), result)
343        self.assertEqual(events,
344                         ['startTest', 'addUnexpectedSuccess', 'stopTest'])
345        self.assertFalse(result.failures)
346        self.assertFalse(result.expectedFailures)
347        self.assertEqual(result.unexpectedSuccesses, [test])
348        self.assertFalse(result.wasSuccessful())
349
350    def test_unexpected_success_subtests(self):
351        # Success in all subtests counts as the unexpected success of
352        # the whole test.
353        class Foo(unittest.TestCase):
354            @unittest.expectedFailure
355            def test_die(self):
356                with self.subTest():
357                    # This one succeeds
358                    pass
359                with self.subTest():
360                    # So does this one
361                    pass
362        events = []
363        result = LoggingResult(events)
364        test = Foo("test_die")
365        self.assertIs(test.run(result), result)
366        self.assertEqual(events,
367                         ['startTest',
368                          'addSubTestSuccess', 'addSubTestSuccess',
369                          'addUnexpectedSuccess', 'stopTest'])
370        self.assertFalse(result.failures)
371        self.assertFalse(result.expectedFailures)
372        self.assertEqual(result.unexpectedSuccesses, [test])
373        self.assertFalse(result.wasSuccessful())
374
375    def test_unexpected_success_and_fail_in_cleanup(self):
376        class Foo(unittest.TestCase):
377            @unittest.expectedFailure
378            def test_die(self):
379                pass
380            def tearDown(self):
381                self.fail("bad tearDown")
382        events = []
383        result = LoggingResult(events)
384        test = Foo("test_die")
385        self.assertIs(test.run(result), result)
386        self.assertEqual(events,
387                         ['startTest', 'addFailure', 'stopTest'])
388        self.assertEqual(len(result.failures), 1)
389        self.assertIn('AssertionError: bad tearDown', result.failures[0][1])
390        self.assertFalse(result.expectedFailures)
391        self.assertFalse(result.unexpectedSuccesses)
392        self.assertFalse(result.wasSuccessful())
393
394    def test_unexpected_success_and_skip_in_cleanup(self):
395        class Foo(unittest.TestCase):
396            @unittest.expectedFailure
397            def test_die(self):
398                pass
399            def tearDown(self):
400                self.skipTest("skip")
401        events = []
402        result = LoggingResult(events)
403        test = Foo("test_die")
404        self.assertIs(test.run(result), result)
405        self.assertEqual(events,
406                         ['startTest', 'addSkip', 'stopTest'])
407        self.assertFalse(result.failures)
408        self.assertFalse(result.expectedFailures)
409        self.assertFalse(result.unexpectedSuccesses)
410        self.assertEqual(result.skipped, [(test, "skip")])
411        self.assertTrue(result.wasSuccessful())
412
413    def test_skip_doesnt_run_setup(self):
414        class Foo(unittest.TestCase):
415            wasSetUp = False
416            wasTornDown = False
417            def setUp(self):
418                Foo.wasSetUp = True
419            def tornDown(self):
420                Foo.wasTornDown = True
421            @unittest.skip('testing')
422            def test_1(self):
423                pass
424
425        result = unittest.TestResult()
426        test = Foo("test_1")
427        suite = unittest.TestSuite([test])
428        self.assertIs(suite.run(result), result)
429        self.assertEqual(result.skipped, [(test, "testing")])
430        self.assertFalse(Foo.wasSetUp)
431        self.assertFalse(Foo.wasTornDown)
432
433    def test_decorated_skip(self):
434        def decorator(func):
435            def inner(*a):
436                return func(*a)
437            return inner
438
439        class Foo(unittest.TestCase):
440            @decorator
441            @unittest.skip('testing')
442            def test_1(self):
443                pass
444
445        result = unittest.TestResult()
446        test = Foo("test_1")
447        suite = unittest.TestSuite([test])
448        self.assertIs(suite.run(result), result)
449        self.assertEqual(result.skipped, [(test, "testing")])
450
451    def test_skip_without_reason(self):
452        class Foo(unittest.TestCase):
453            @unittest.skip
454            def test_1(self):
455                pass
456
457        result = unittest.TestResult()
458        test = Foo("test_1")
459        suite = unittest.TestSuite([test])
460        self.assertIs(suite.run(result), result)
461        self.assertEqual(result.skipped, [(test, "")])
462
463    def test_debug_skipping(self):
464        class Foo(unittest.TestCase):
465            def setUp(self):
466                events.append("setUp")
467            def tearDown(self):
468                events.append("tearDown")
469            def test1(self):
470                self.skipTest('skipping exception')
471                events.append("test1")
472            @unittest.skip("skipping decorator")
473            def test2(self):
474                events.append("test2")
475
476        events = []
477        test = Foo("test1")
478        with self.assertRaises(unittest.SkipTest) as cm:
479            test.debug()
480        self.assertIn("skipping exception", str(cm.exception))
481        self.assertEqual(events, ["setUp"])
482
483        events = []
484        test = Foo("test2")
485        with self.assertRaises(unittest.SkipTest) as cm:
486            test.debug()
487        self.assertIn("skipping decorator", str(cm.exception))
488        self.assertEqual(events, [])
489
490    def test_debug_skipping_class(self):
491        @unittest.skip("testing")
492        class Foo(unittest.TestCase):
493            def setUp(self):
494                events.append("setUp")
495            def tearDown(self):
496                events.append("tearDown")
497            def test(self):
498                events.append("test")
499
500        events = []
501        test = Foo("test")
502        with self.assertRaises(unittest.SkipTest) as cm:
503            test.debug()
504        self.assertIn("testing", str(cm.exception))
505        self.assertEqual(events, [])
506
507    def test_debug_skipping_subtests(self):
508        class Foo(unittest.TestCase):
509            def setUp(self):
510                events.append("setUp")
511            def tearDown(self):
512                events.append("tearDown")
513            def test(self):
514                with self.subTest(a=1):
515                    events.append('subtest')
516                    self.skipTest("skip subtest")
517                    events.append('end subtest')
518                events.append('end test')
519
520        events = []
521        result = LoggingResult(events)
522        test = Foo("test")
523        with self.assertRaises(unittest.SkipTest) as cm:
524            test.debug()
525        self.assertIn("skip subtest", str(cm.exception))
526        self.assertEqual(events, ['setUp', 'subtest'])
527
528
529if __name__ == "__main__":
530    unittest.main()
531