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

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

#countcontent
1n/aimport io
2n/aimport sys
3n/a
4n/aimport unittest
5n/a
6n/a
7n/adef resultFactory(*_):
8n/a return unittest.TestResult()
9n/a
10n/a
11n/aclass TestSetups(unittest.TestCase):
12n/a
13n/a def getRunner(self):
14n/a return unittest.TextTestRunner(resultclass=resultFactory,
15n/a stream=io.StringIO())
16n/a def runTests(self, *cases):
17n/a suite = unittest.TestSuite()
18n/a for case in cases:
19n/a tests = unittest.defaultTestLoader.loadTestsFromTestCase(case)
20n/a suite.addTests(tests)
21n/a
22n/a runner = self.getRunner()
23n/a
24n/a # creating a nested suite exposes some potential bugs
25n/a realSuite = unittest.TestSuite()
26n/a realSuite.addTest(suite)
27n/a # adding empty suites to the end exposes potential bugs
28n/a suite.addTest(unittest.TestSuite())
29n/a realSuite.addTest(unittest.TestSuite())
30n/a return runner.run(realSuite)
31n/a
32n/a def test_setup_class(self):
33n/a class Test(unittest.TestCase):
34n/a setUpCalled = 0
35n/a @classmethod
36n/a def setUpClass(cls):
37n/a Test.setUpCalled += 1
38n/a unittest.TestCase.setUpClass()
39n/a def test_one(self):
40n/a pass
41n/a def test_two(self):
42n/a pass
43n/a
44n/a result = self.runTests(Test)
45n/a
46n/a self.assertEqual(Test.setUpCalled, 1)
47n/a self.assertEqual(result.testsRun, 2)
48n/a self.assertEqual(len(result.errors), 0)
49n/a
50n/a def test_teardown_class(self):
51n/a class Test(unittest.TestCase):
52n/a tearDownCalled = 0
53n/a @classmethod
54n/a def tearDownClass(cls):
55n/a Test.tearDownCalled += 1
56n/a unittest.TestCase.tearDownClass()
57n/a def test_one(self):
58n/a pass
59n/a def test_two(self):
60n/a pass
61n/a
62n/a result = self.runTests(Test)
63n/a
64n/a self.assertEqual(Test.tearDownCalled, 1)
65n/a self.assertEqual(result.testsRun, 2)
66n/a self.assertEqual(len(result.errors), 0)
67n/a
68n/a def test_teardown_class_two_classes(self):
69n/a class Test(unittest.TestCase):
70n/a tearDownCalled = 0
71n/a @classmethod
72n/a def tearDownClass(cls):
73n/a Test.tearDownCalled += 1
74n/a unittest.TestCase.tearDownClass()
75n/a def test_one(self):
76n/a pass
77n/a def test_two(self):
78n/a pass
79n/a
80n/a class Test2(unittest.TestCase):
81n/a tearDownCalled = 0
82n/a @classmethod
83n/a def tearDownClass(cls):
84n/a Test2.tearDownCalled += 1
85n/a unittest.TestCase.tearDownClass()
86n/a def test_one(self):
87n/a pass
88n/a def test_two(self):
89n/a pass
90n/a
91n/a result = self.runTests(Test, Test2)
92n/a
93n/a self.assertEqual(Test.tearDownCalled, 1)
94n/a self.assertEqual(Test2.tearDownCalled, 1)
95n/a self.assertEqual(result.testsRun, 4)
96n/a self.assertEqual(len(result.errors), 0)
97n/a
98n/a def test_error_in_setupclass(self):
99n/a class BrokenTest(unittest.TestCase):
100n/a @classmethod
101n/a def setUpClass(cls):
102n/a raise TypeError('foo')
103n/a def test_one(self):
104n/a pass
105n/a def test_two(self):
106n/a pass
107n/a
108n/a result = self.runTests(BrokenTest)
109n/a
110n/a self.assertEqual(result.testsRun, 0)
111n/a self.assertEqual(len(result.errors), 1)
112n/a error, _ = result.errors[0]
113n/a self.assertEqual(str(error),
114n/a 'setUpClass (%s.%s)' % (__name__, BrokenTest.__qualname__))
115n/a
116n/a def test_error_in_teardown_class(self):
117n/a class Test(unittest.TestCase):
118n/a tornDown = 0
119n/a @classmethod
120n/a def tearDownClass(cls):
121n/a Test.tornDown += 1
122n/a raise TypeError('foo')
123n/a def test_one(self):
124n/a pass
125n/a def test_two(self):
126n/a pass
127n/a
128n/a class Test2(unittest.TestCase):
129n/a tornDown = 0
130n/a @classmethod
131n/a def tearDownClass(cls):
132n/a Test2.tornDown += 1
133n/a raise TypeError('foo')
134n/a def test_one(self):
135n/a pass
136n/a def test_two(self):
137n/a pass
138n/a
139n/a result = self.runTests(Test, Test2)
140n/a self.assertEqual(result.testsRun, 4)
141n/a self.assertEqual(len(result.errors), 2)
142n/a self.assertEqual(Test.tornDown, 1)
143n/a self.assertEqual(Test2.tornDown, 1)
144n/a
145n/a error, _ = result.errors[0]
146n/a self.assertEqual(str(error),
147n/a 'tearDownClass (%s.%s)' % (__name__, Test.__qualname__))
148n/a
149n/a def test_class_not_torndown_when_setup_fails(self):
150n/a class Test(unittest.TestCase):
151n/a tornDown = False
152n/a @classmethod
153n/a def setUpClass(cls):
154n/a raise TypeError
155n/a @classmethod
156n/a def tearDownClass(cls):
157n/a Test.tornDown = True
158n/a raise TypeError('foo')
159n/a def test_one(self):
160n/a pass
161n/a
162n/a self.runTests(Test)
163n/a self.assertFalse(Test.tornDown)
164n/a
165n/a def test_class_not_setup_or_torndown_when_skipped(self):
166n/a class Test(unittest.TestCase):
167n/a classSetUp = False
168n/a tornDown = False
169n/a @classmethod
170n/a def setUpClass(cls):
171n/a Test.classSetUp = True
172n/a @classmethod
173n/a def tearDownClass(cls):
174n/a Test.tornDown = True
175n/a def test_one(self):
176n/a pass
177n/a
178n/a Test = unittest.skip("hop")(Test)
179n/a self.runTests(Test)
180n/a self.assertFalse(Test.classSetUp)
181n/a self.assertFalse(Test.tornDown)
182n/a
183n/a def test_setup_teardown_order_with_pathological_suite(self):
184n/a results = []
185n/a
186n/a class Module1(object):
187n/a @staticmethod
188n/a def setUpModule():
189n/a results.append('Module1.setUpModule')
190n/a @staticmethod
191n/a def tearDownModule():
192n/a results.append('Module1.tearDownModule')
193n/a
194n/a class Module2(object):
195n/a @staticmethod
196n/a def setUpModule():
197n/a results.append('Module2.setUpModule')
198n/a @staticmethod
199n/a def tearDownModule():
200n/a results.append('Module2.tearDownModule')
201n/a
202n/a class Test1(unittest.TestCase):
203n/a @classmethod
204n/a def setUpClass(cls):
205n/a results.append('setup 1')
206n/a @classmethod
207n/a def tearDownClass(cls):
208n/a results.append('teardown 1')
209n/a def testOne(self):
210n/a results.append('Test1.testOne')
211n/a def testTwo(self):
212n/a results.append('Test1.testTwo')
213n/a
214n/a class Test2(unittest.TestCase):
215n/a @classmethod
216n/a def setUpClass(cls):
217n/a results.append('setup 2')
218n/a @classmethod
219n/a def tearDownClass(cls):
220n/a results.append('teardown 2')
221n/a def testOne(self):
222n/a results.append('Test2.testOne')
223n/a def testTwo(self):
224n/a results.append('Test2.testTwo')
225n/a
226n/a class Test3(unittest.TestCase):
227n/a @classmethod
228n/a def setUpClass(cls):
229n/a results.append('setup 3')
230n/a @classmethod
231n/a def tearDownClass(cls):
232n/a results.append('teardown 3')
233n/a def testOne(self):
234n/a results.append('Test3.testOne')
235n/a def testTwo(self):
236n/a results.append('Test3.testTwo')
237n/a
238n/a Test1.__module__ = Test2.__module__ = 'Module'
239n/a Test3.__module__ = 'Module2'
240n/a sys.modules['Module'] = Module1
241n/a sys.modules['Module2'] = Module2
242n/a
243n/a first = unittest.TestSuite((Test1('testOne'),))
244n/a second = unittest.TestSuite((Test1('testTwo'),))
245n/a third = unittest.TestSuite((Test2('testOne'),))
246n/a fourth = unittest.TestSuite((Test2('testTwo'),))
247n/a fifth = unittest.TestSuite((Test3('testOne'),))
248n/a sixth = unittest.TestSuite((Test3('testTwo'),))
249n/a suite = unittest.TestSuite((first, second, third, fourth, fifth, sixth))
250n/a
251n/a runner = self.getRunner()
252n/a result = runner.run(suite)
253n/a self.assertEqual(result.testsRun, 6)
254n/a self.assertEqual(len(result.errors), 0)
255n/a
256n/a self.assertEqual(results,
257n/a ['Module1.setUpModule', 'setup 1',
258n/a 'Test1.testOne', 'Test1.testTwo', 'teardown 1',
259n/a 'setup 2', 'Test2.testOne', 'Test2.testTwo',
260n/a 'teardown 2', 'Module1.tearDownModule',
261n/a 'Module2.setUpModule', 'setup 3',
262n/a 'Test3.testOne', 'Test3.testTwo',
263n/a 'teardown 3', 'Module2.tearDownModule'])
264n/a
265n/a def test_setup_module(self):
266n/a class Module(object):
267n/a moduleSetup = 0
268n/a @staticmethod
269n/a def setUpModule():
270n/a Module.moduleSetup += 1
271n/a
272n/a class Test(unittest.TestCase):
273n/a def test_one(self):
274n/a pass
275n/a def test_two(self):
276n/a pass
277n/a Test.__module__ = 'Module'
278n/a sys.modules['Module'] = Module
279n/a
280n/a result = self.runTests(Test)
281n/a self.assertEqual(Module.moduleSetup, 1)
282n/a self.assertEqual(result.testsRun, 2)
283n/a self.assertEqual(len(result.errors), 0)
284n/a
285n/a def test_error_in_setup_module(self):
286n/a class Module(object):
287n/a moduleSetup = 0
288n/a moduleTornDown = 0
289n/a @staticmethod
290n/a def setUpModule():
291n/a Module.moduleSetup += 1
292n/a raise TypeError('foo')
293n/a @staticmethod
294n/a def tearDownModule():
295n/a Module.moduleTornDown += 1
296n/a
297n/a class Test(unittest.TestCase):
298n/a classSetUp = False
299n/a classTornDown = False
300n/a @classmethod
301n/a def setUpClass(cls):
302n/a Test.classSetUp = True
303n/a @classmethod
304n/a def tearDownClass(cls):
305n/a Test.classTornDown = True
306n/a def test_one(self):
307n/a pass
308n/a def test_two(self):
309n/a pass
310n/a
311n/a class Test2(unittest.TestCase):
312n/a def test_one(self):
313n/a pass
314n/a def test_two(self):
315n/a pass
316n/a Test.__module__ = 'Module'
317n/a Test2.__module__ = 'Module'
318n/a sys.modules['Module'] = Module
319n/a
320n/a result = self.runTests(Test, Test2)
321n/a self.assertEqual(Module.moduleSetup, 1)
322n/a self.assertEqual(Module.moduleTornDown, 0)
323n/a self.assertEqual(result.testsRun, 0)
324n/a self.assertFalse(Test.classSetUp)
325n/a self.assertFalse(Test.classTornDown)
326n/a self.assertEqual(len(result.errors), 1)
327n/a error, _ = result.errors[0]
328n/a self.assertEqual(str(error), 'setUpModule (Module)')
329n/a
330n/a def test_testcase_with_missing_module(self):
331n/a class Test(unittest.TestCase):
332n/a def test_one(self):
333n/a pass
334n/a def test_two(self):
335n/a pass
336n/a Test.__module__ = 'Module'
337n/a sys.modules.pop('Module', None)
338n/a
339n/a result = self.runTests(Test)
340n/a self.assertEqual(result.testsRun, 2)
341n/a
342n/a def test_teardown_module(self):
343n/a class Module(object):
344n/a moduleTornDown = 0
345n/a @staticmethod
346n/a def tearDownModule():
347n/a Module.moduleTornDown += 1
348n/a
349n/a class Test(unittest.TestCase):
350n/a def test_one(self):
351n/a pass
352n/a def test_two(self):
353n/a pass
354n/a Test.__module__ = 'Module'
355n/a sys.modules['Module'] = Module
356n/a
357n/a result = self.runTests(Test)
358n/a self.assertEqual(Module.moduleTornDown, 1)
359n/a self.assertEqual(result.testsRun, 2)
360n/a self.assertEqual(len(result.errors), 0)
361n/a
362n/a def test_error_in_teardown_module(self):
363n/a class Module(object):
364n/a moduleTornDown = 0
365n/a @staticmethod
366n/a def tearDownModule():
367n/a Module.moduleTornDown += 1
368n/a raise TypeError('foo')
369n/a
370n/a class Test(unittest.TestCase):
371n/a classSetUp = False
372n/a classTornDown = False
373n/a @classmethod
374n/a def setUpClass(cls):
375n/a Test.classSetUp = True
376n/a @classmethod
377n/a def tearDownClass(cls):
378n/a Test.classTornDown = True
379n/a def test_one(self):
380n/a pass
381n/a def test_two(self):
382n/a pass
383n/a
384n/a class Test2(unittest.TestCase):
385n/a def test_one(self):
386n/a pass
387n/a def test_two(self):
388n/a pass
389n/a Test.__module__ = 'Module'
390n/a Test2.__module__ = 'Module'
391n/a sys.modules['Module'] = Module
392n/a
393n/a result = self.runTests(Test, Test2)
394n/a self.assertEqual(Module.moduleTornDown, 1)
395n/a self.assertEqual(result.testsRun, 4)
396n/a self.assertTrue(Test.classSetUp)
397n/a self.assertTrue(Test.classTornDown)
398n/a self.assertEqual(len(result.errors), 1)
399n/a error, _ = result.errors[0]
400n/a self.assertEqual(str(error), 'tearDownModule (Module)')
401n/a
402n/a def test_skiptest_in_setupclass(self):
403n/a class Test(unittest.TestCase):
404n/a @classmethod
405n/a def setUpClass(cls):
406n/a raise unittest.SkipTest('foo')
407n/a def test_one(self):
408n/a pass
409n/a def test_two(self):
410n/a pass
411n/a
412n/a result = self.runTests(Test)
413n/a self.assertEqual(result.testsRun, 0)
414n/a self.assertEqual(len(result.errors), 0)
415n/a self.assertEqual(len(result.skipped), 1)
416n/a skipped = result.skipped[0][0]
417n/a self.assertEqual(str(skipped),
418n/a 'setUpClass (%s.%s)' % (__name__, Test.__qualname__))
419n/a
420n/a def test_skiptest_in_setupmodule(self):
421n/a class Test(unittest.TestCase):
422n/a def test_one(self):
423n/a pass
424n/a def test_two(self):
425n/a pass
426n/a
427n/a class Module(object):
428n/a @staticmethod
429n/a def setUpModule():
430n/a raise unittest.SkipTest('foo')
431n/a
432n/a Test.__module__ = 'Module'
433n/a sys.modules['Module'] = Module
434n/a
435n/a result = self.runTests(Test)
436n/a self.assertEqual(result.testsRun, 0)
437n/a self.assertEqual(len(result.errors), 0)
438n/a self.assertEqual(len(result.skipped), 1)
439n/a skipped = result.skipped[0][0]
440n/a self.assertEqual(str(skipped), 'setUpModule (Module)')
441n/a
442n/a def test_suite_debug_executes_setups_and_teardowns(self):
443n/a ordering = []
444n/a
445n/a class Module(object):
446n/a @staticmethod
447n/a def setUpModule():
448n/a ordering.append('setUpModule')
449n/a @staticmethod
450n/a def tearDownModule():
451n/a ordering.append('tearDownModule')
452n/a
453n/a class Test(unittest.TestCase):
454n/a @classmethod
455n/a def setUpClass(cls):
456n/a ordering.append('setUpClass')
457n/a @classmethod
458n/a def tearDownClass(cls):
459n/a ordering.append('tearDownClass')
460n/a def test_something(self):
461n/a ordering.append('test_something')
462n/a
463n/a Test.__module__ = 'Module'
464n/a sys.modules['Module'] = Module
465n/a
466n/a suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
467n/a suite.debug()
468n/a expectedOrder = ['setUpModule', 'setUpClass', 'test_something', 'tearDownClass', 'tearDownModule']
469n/a self.assertEqual(ordering, expectedOrder)
470n/a
471n/a def test_suite_debug_propagates_exceptions(self):
472n/a class Module(object):
473n/a @staticmethod
474n/a def setUpModule():
475n/a if phase == 0:
476n/a raise Exception('setUpModule')
477n/a @staticmethod
478n/a def tearDownModule():
479n/a if phase == 1:
480n/a raise Exception('tearDownModule')
481n/a
482n/a class Test(unittest.TestCase):
483n/a @classmethod
484n/a def setUpClass(cls):
485n/a if phase == 2:
486n/a raise Exception('setUpClass')
487n/a @classmethod
488n/a def tearDownClass(cls):
489n/a if phase == 3:
490n/a raise Exception('tearDownClass')
491n/a def test_something(self):
492n/a if phase == 4:
493n/a raise Exception('test_something')
494n/a
495n/a Test.__module__ = 'Module'
496n/a sys.modules['Module'] = Module
497n/a
498n/a messages = ('setUpModule', 'tearDownModule', 'setUpClass', 'tearDownClass', 'test_something')
499n/a for phase, msg in enumerate(messages):
500n/a _suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
501n/a suite = unittest.TestSuite([_suite])
502n/a with self.assertRaisesRegex(Exception, msg):
503n/a suite.debug()
504n/a
505n/a
506n/aif __name__ == '__main__':
507n/a unittest.main()