ยปCore Development>Code coverage>Lib/unittest/test/test_skipping.py

Python code coverage for Lib/unittest/test/test_skipping.py

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