• 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_decorators(self):
33        op_table = ((unittest.skipUnless, False, True),
34                    (unittest.skipIf, True, False))
35        for deco, do_skip, dont_skip in op_table:
36            class Foo(unittest.TestCase):
37                @deco(do_skip, "testing")
38                def test_skip(self): pass
39
40                @deco(dont_skip, "testing")
41                def test_dont_skip(self): pass
42            test_do_skip = Foo("test_skip")
43            test_dont_skip = Foo("test_dont_skip")
44            suite = unittest.TestSuite([test_do_skip, test_dont_skip])
45            events = []
46            result = LoggingResult(events)
47            suite.run(result)
48            self.assertEqual(len(result.skipped), 1)
49            expected = ['startTest', 'addSkip', 'stopTest',
50                        'startTest', 'addSuccess', 'stopTest']
51            self.assertEqual(events, expected)
52            self.assertEqual(result.testsRun, 2)
53            self.assertEqual(result.skipped, [(test_do_skip, "testing")])
54            self.assertTrue(result.wasSuccessful())
55
56    def test_skip_class(self):
57        @unittest.skip("testing")
58        class Foo(unittest.TestCase):
59            def test_1(self):
60                record.append(1)
61        record = []
62        result = unittest.TestResult()
63        test = Foo("test_1")
64        suite = unittest.TestSuite([test])
65        suite.run(result)
66        self.assertEqual(result.skipped, [(test, "testing")])
67        self.assertEqual(record, [])
68
69    def test_skip_non_unittest_class_old_style(self):
70        @unittest.skip("testing")
71        class Mixin:
72            def test_1(self):
73                record.append(1)
74        class Foo(Mixin, unittest.TestCase):
75            pass
76        record = []
77        result = unittest.TestResult()
78        test = Foo("test_1")
79        suite = unittest.TestSuite([test])
80        suite.run(result)
81        self.assertEqual(result.skipped, [(test, "testing")])
82        self.assertEqual(record, [])
83
84    def test_skip_non_unittest_class_new_style(self):
85        @unittest.skip("testing")
86        class Mixin(object):
87            def test_1(self):
88                record.append(1)
89        class Foo(Mixin, unittest.TestCase):
90            pass
91        record = []
92        result = unittest.TestResult()
93        test = Foo("test_1")
94        suite = unittest.TestSuite([test])
95        suite.run(result)
96        self.assertEqual(result.skipped, [(test, "testing")])
97        self.assertEqual(record, [])
98
99    def test_expected_failure(self):
100        class Foo(unittest.TestCase):
101            @unittest.expectedFailure
102            def test_die(self):
103                self.fail("help me!")
104        events = []
105        result = LoggingResult(events)
106        test = Foo("test_die")
107        test.run(result)
108        self.assertEqual(events,
109                         ['startTest', 'addExpectedFailure', 'stopTest'])
110        self.assertEqual(result.expectedFailures[0][0], test)
111        self.assertTrue(result.wasSuccessful())
112
113    def test_unexpected_success(self):
114        class Foo(unittest.TestCase):
115            @unittest.expectedFailure
116            def test_die(self):
117                pass
118        events = []
119        result = LoggingResult(events)
120        test = Foo("test_die")
121        test.run(result)
122        self.assertEqual(events,
123                         ['startTest', 'addUnexpectedSuccess', 'stopTest'])
124        self.assertFalse(result.failures)
125        self.assertEqual(result.unexpectedSuccesses, [test])
126        self.assertTrue(result.wasSuccessful())
127
128    def test_skip_doesnt_run_setup(self):
129        class Foo(unittest.TestCase):
130            wasSetUp = False
131            wasTornDown = False
132            def setUp(self):
133                Foo.wasSetUp = True
134            def tornDown(self):
135                Foo.wasTornDown = True
136            @unittest.skip('testing')
137            def test_1(self):
138                pass
139
140        result = unittest.TestResult()
141        test = Foo("test_1")
142        suite = unittest.TestSuite([test])
143        suite.run(result)
144        self.assertEqual(result.skipped, [(test, "testing")])
145        self.assertFalse(Foo.wasSetUp)
146        self.assertFalse(Foo.wasTornDown)
147
148    def test_decorated_skip(self):
149        def decorator(func):
150            def inner(*a):
151                return func(*a)
152            return inner
153
154        class Foo(unittest.TestCase):
155            @decorator
156            @unittest.skip('testing')
157            def test_1(self):
158                pass
159
160        result = unittest.TestResult()
161        test = Foo("test_1")
162        suite = unittest.TestSuite([test])
163        suite.run(result)
164        self.assertEqual(result.skipped, [(test, "testing")])
165
166
167if __name__ == '__main__':
168    unittest.main()
169