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