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

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

#countcontent
1n/aimport unittest
2n/a
3n/aimport gc
4n/aimport sys
5n/aimport weakref
6n/afrom unittest.test.support import LoggingResult, TestEquality
7n/a
8n/a
9n/a### Support code for Test_TestSuite
10n/a################################################################
11n/a
12n/aclass Test(object):
13n/a class Foo(unittest.TestCase):
14n/a def test_1(self): pass
15n/a def test_2(self): pass
16n/a def test_3(self): pass
17n/a def runTest(self): pass
18n/a
19n/adef _mk_TestSuite(*names):
20n/a return unittest.TestSuite(Test.Foo(n) for n in names)
21n/a
22n/a################################################################
23n/a
24n/a
25n/aclass Test_TestSuite(unittest.TestCase, TestEquality):
26n/a
27n/a ### Set up attributes needed by inherited tests
28n/a ################################################################
29n/a
30n/a # Used by TestEquality.test_eq
31n/a eq_pairs = [(unittest.TestSuite(), unittest.TestSuite())
32n/a ,(unittest.TestSuite(), unittest.TestSuite([]))
33n/a ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))]
34n/a
35n/a # Used by TestEquality.test_ne
36n/a ne_pairs = [(unittest.TestSuite(), _mk_TestSuite('test_1'))
37n/a ,(unittest.TestSuite([]), _mk_TestSuite('test_1'))
38n/a ,(_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3'))
39n/a ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))]
40n/a
41n/a ################################################################
42n/a ### /Set up attributes needed by inherited tests
43n/a
44n/a ### Tests for TestSuite.__init__
45n/a ################################################################
46n/a
47n/a # "class TestSuite([tests])"
48n/a #
49n/a # The tests iterable should be optional
50n/a def test_init__tests_optional(self):
51n/a suite = unittest.TestSuite()
52n/a
53n/a self.assertEqual(suite.countTestCases(), 0)
54n/a # countTestCases() still works after tests are run
55n/a suite.run(unittest.TestResult())
56n/a self.assertEqual(suite.countTestCases(), 0)
57n/a
58n/a # "class TestSuite([tests])"
59n/a # ...
60n/a # "If tests is given, it must be an iterable of individual test cases
61n/a # or other test suites that will be used to build the suite initially"
62n/a #
63n/a # TestSuite should deal with empty tests iterables by allowing the
64n/a # creation of an empty suite
65n/a def test_init__empty_tests(self):
66n/a suite = unittest.TestSuite([])
67n/a
68n/a self.assertEqual(suite.countTestCases(), 0)
69n/a # countTestCases() still works after tests are run
70n/a suite.run(unittest.TestResult())
71n/a self.assertEqual(suite.countTestCases(), 0)
72n/a
73n/a # "class TestSuite([tests])"
74n/a # ...
75n/a # "If tests is given, it must be an iterable of individual test cases
76n/a # or other test suites that will be used to build the suite initially"
77n/a #
78n/a # TestSuite should allow any iterable to provide tests
79n/a def test_init__tests_from_any_iterable(self):
80n/a def tests():
81n/a yield unittest.FunctionTestCase(lambda: None)
82n/a yield unittest.FunctionTestCase(lambda: None)
83n/a
84n/a suite_1 = unittest.TestSuite(tests())
85n/a self.assertEqual(suite_1.countTestCases(), 2)
86n/a
87n/a suite_2 = unittest.TestSuite(suite_1)
88n/a self.assertEqual(suite_2.countTestCases(), 2)
89n/a
90n/a suite_3 = unittest.TestSuite(set(suite_1))
91n/a self.assertEqual(suite_3.countTestCases(), 2)
92n/a
93n/a # countTestCases() still works after tests are run
94n/a suite_1.run(unittest.TestResult())
95n/a self.assertEqual(suite_1.countTestCases(), 2)
96n/a suite_2.run(unittest.TestResult())
97n/a self.assertEqual(suite_2.countTestCases(), 2)
98n/a suite_3.run(unittest.TestResult())
99n/a self.assertEqual(suite_3.countTestCases(), 2)
100n/a
101n/a # "class TestSuite([tests])"
102n/a # ...
103n/a # "If tests is given, it must be an iterable of individual test cases
104n/a # or other test suites that will be used to build the suite initially"
105n/a #
106n/a # Does TestSuite() also allow other TestSuite() instances to be present
107n/a # in the tests iterable?
108n/a def test_init__TestSuite_instances_in_tests(self):
109n/a def tests():
110n/a ftc = unittest.FunctionTestCase(lambda: None)
111n/a yield unittest.TestSuite([ftc])
112n/a yield unittest.FunctionTestCase(lambda: None)
113n/a
114n/a suite = unittest.TestSuite(tests())
115n/a self.assertEqual(suite.countTestCases(), 2)
116n/a # countTestCases() still works after tests are run
117n/a suite.run(unittest.TestResult())
118n/a self.assertEqual(suite.countTestCases(), 2)
119n/a
120n/a ################################################################
121n/a ### /Tests for TestSuite.__init__
122n/a
123n/a # Container types should support the iter protocol
124n/a def test_iter(self):
125n/a test1 = unittest.FunctionTestCase(lambda: None)
126n/a test2 = unittest.FunctionTestCase(lambda: None)
127n/a suite = unittest.TestSuite((test1, test2))
128n/a
129n/a self.assertEqual(list(suite), [test1, test2])
130n/a
131n/a # "Return the number of tests represented by the this test object.
132n/a # ...this method is also implemented by the TestSuite class, which can
133n/a # return larger [greater than 1] values"
134n/a #
135n/a # Presumably an empty TestSuite returns 0?
136n/a def test_countTestCases_zero_simple(self):
137n/a suite = unittest.TestSuite()
138n/a
139n/a self.assertEqual(suite.countTestCases(), 0)
140n/a
141n/a # "Return the number of tests represented by the this test object.
142n/a # ...this method is also implemented by the TestSuite class, which can
143n/a # return larger [greater than 1] values"
144n/a #
145n/a # Presumably an empty TestSuite (even if it contains other empty
146n/a # TestSuite instances) returns 0?
147n/a def test_countTestCases_zero_nested(self):
148n/a class Test1(unittest.TestCase):
149n/a def test(self):
150n/a pass
151n/a
152n/a suite = unittest.TestSuite([unittest.TestSuite()])
153n/a
154n/a self.assertEqual(suite.countTestCases(), 0)
155n/a
156n/a # "Return the number of tests represented by the this test object.
157n/a # ...this method is also implemented by the TestSuite class, which can
158n/a # return larger [greater than 1] values"
159n/a def test_countTestCases_simple(self):
160n/a test1 = unittest.FunctionTestCase(lambda: None)
161n/a test2 = unittest.FunctionTestCase(lambda: None)
162n/a suite = unittest.TestSuite((test1, test2))
163n/a
164n/a self.assertEqual(suite.countTestCases(), 2)
165n/a # countTestCases() still works after tests are run
166n/a suite.run(unittest.TestResult())
167n/a self.assertEqual(suite.countTestCases(), 2)
168n/a
169n/a # "Return the number of tests represented by the this test object.
170n/a # ...this method is also implemented by the TestSuite class, which can
171n/a # return larger [greater than 1] values"
172n/a #
173n/a # Make sure this holds for nested TestSuite instances, too
174n/a def test_countTestCases_nested(self):
175n/a class Test1(unittest.TestCase):
176n/a def test1(self): pass
177n/a def test2(self): pass
178n/a
179n/a test2 = unittest.FunctionTestCase(lambda: None)
180n/a test3 = unittest.FunctionTestCase(lambda: None)
181n/a child = unittest.TestSuite((Test1('test2'), test2))
182n/a parent = unittest.TestSuite((test3, child, Test1('test1')))
183n/a
184n/a self.assertEqual(parent.countTestCases(), 4)
185n/a # countTestCases() still works after tests are run
186n/a parent.run(unittest.TestResult())
187n/a self.assertEqual(parent.countTestCases(), 4)
188n/a self.assertEqual(child.countTestCases(), 2)
189n/a
190n/a # "Run the tests associated with this suite, collecting the result into
191n/a # the test result object passed as result."
192n/a #
193n/a # And if there are no tests? What then?
194n/a def test_run__empty_suite(self):
195n/a events = []
196n/a result = LoggingResult(events)
197n/a
198n/a suite = unittest.TestSuite()
199n/a
200n/a suite.run(result)
201n/a
202n/a self.assertEqual(events, [])
203n/a
204n/a # "Note that unlike TestCase.run(), TestSuite.run() requires the
205n/a # "result object to be passed in."
206n/a def test_run__requires_result(self):
207n/a suite = unittest.TestSuite()
208n/a
209n/a try:
210n/a suite.run()
211n/a except TypeError:
212n/a pass
213n/a else:
214n/a self.fail("Failed to raise TypeError")
215n/a
216n/a # "Run the tests associated with this suite, collecting the result into
217n/a # the test result object passed as result."
218n/a def test_run(self):
219n/a events = []
220n/a result = LoggingResult(events)
221n/a
222n/a class LoggingCase(unittest.TestCase):
223n/a def run(self, result):
224n/a events.append('run %s' % self._testMethodName)
225n/a
226n/a def test1(self): pass
227n/a def test2(self): pass
228n/a
229n/a tests = [LoggingCase('test1'), LoggingCase('test2')]
230n/a
231n/a unittest.TestSuite(tests).run(result)
232n/a
233n/a self.assertEqual(events, ['run test1', 'run test2'])
234n/a
235n/a # "Add a TestCase ... to the suite"
236n/a def test_addTest__TestCase(self):
237n/a class Foo(unittest.TestCase):
238n/a def test(self): pass
239n/a
240n/a test = Foo('test')
241n/a suite = unittest.TestSuite()
242n/a
243n/a suite.addTest(test)
244n/a
245n/a self.assertEqual(suite.countTestCases(), 1)
246n/a self.assertEqual(list(suite), [test])
247n/a # countTestCases() still works after tests are run
248n/a suite.run(unittest.TestResult())
249n/a self.assertEqual(suite.countTestCases(), 1)
250n/a
251n/a # "Add a ... TestSuite to the suite"
252n/a def test_addTest__TestSuite(self):
253n/a class Foo(unittest.TestCase):
254n/a def test(self): pass
255n/a
256n/a suite_2 = unittest.TestSuite([Foo('test')])
257n/a
258n/a suite = unittest.TestSuite()
259n/a suite.addTest(suite_2)
260n/a
261n/a self.assertEqual(suite.countTestCases(), 1)
262n/a self.assertEqual(list(suite), [suite_2])
263n/a # countTestCases() still works after tests are run
264n/a suite.run(unittest.TestResult())
265n/a self.assertEqual(suite.countTestCases(), 1)
266n/a
267n/a # "Add all the tests from an iterable of TestCase and TestSuite
268n/a # instances to this test suite."
269n/a #
270n/a # "This is equivalent to iterating over tests, calling addTest() for
271n/a # each element"
272n/a def test_addTests(self):
273n/a class Foo(unittest.TestCase):
274n/a def test_1(self): pass
275n/a def test_2(self): pass
276n/a
277n/a test_1 = Foo('test_1')
278n/a test_2 = Foo('test_2')
279n/a inner_suite = unittest.TestSuite([test_2])
280n/a
281n/a def gen():
282n/a yield test_1
283n/a yield test_2
284n/a yield inner_suite
285n/a
286n/a suite_1 = unittest.TestSuite()
287n/a suite_1.addTests(gen())
288n/a
289n/a self.assertEqual(list(suite_1), list(gen()))
290n/a
291n/a # "This is equivalent to iterating over tests, calling addTest() for
292n/a # each element"
293n/a suite_2 = unittest.TestSuite()
294n/a for t in gen():
295n/a suite_2.addTest(t)
296n/a
297n/a self.assertEqual(suite_1, suite_2)
298n/a
299n/a # "Add all the tests from an iterable of TestCase and TestSuite
300n/a # instances to this test suite."
301n/a #
302n/a # What happens if it doesn't get an iterable?
303n/a def test_addTest__noniterable(self):
304n/a suite = unittest.TestSuite()
305n/a
306n/a try:
307n/a suite.addTests(5)
308n/a except TypeError:
309n/a pass
310n/a else:
311n/a self.fail("Failed to raise TypeError")
312n/a
313n/a def test_addTest__noncallable(self):
314n/a suite = unittest.TestSuite()
315n/a self.assertRaises(TypeError, suite.addTest, 5)
316n/a
317n/a def test_addTest__casesuiteclass(self):
318n/a suite = unittest.TestSuite()
319n/a self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
320n/a self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)
321n/a
322n/a def test_addTests__string(self):
323n/a suite = unittest.TestSuite()
324n/a self.assertRaises(TypeError, suite.addTests, "foo")
325n/a
326n/a def test_function_in_suite(self):
327n/a def f(_):
328n/a pass
329n/a suite = unittest.TestSuite()
330n/a suite.addTest(f)
331n/a
332n/a # when the bug is fixed this line will not crash
333n/a suite.run(unittest.TestResult())
334n/a
335n/a def test_remove_test_at_index(self):
336n/a if not unittest.BaseTestSuite._cleanup:
337n/a raise unittest.SkipTest("Suite cleanup is disabled")
338n/a
339n/a suite = unittest.TestSuite()
340n/a
341n/a suite._tests = [1, 2, 3]
342n/a suite._removeTestAtIndex(1)
343n/a
344n/a self.assertEqual([1, None, 3], suite._tests)
345n/a
346n/a def test_remove_test_at_index_not_indexable(self):
347n/a if not unittest.BaseTestSuite._cleanup:
348n/a raise unittest.SkipTest("Suite cleanup is disabled")
349n/a
350n/a suite = unittest.TestSuite()
351n/a suite._tests = None
352n/a
353n/a # if _removeAtIndex raises for noniterables this next line will break
354n/a suite._removeTestAtIndex(2)
355n/a
356n/a def assert_garbage_collect_test_after_run(self, TestSuiteClass):
357n/a if not unittest.BaseTestSuite._cleanup:
358n/a raise unittest.SkipTest("Suite cleanup is disabled")
359n/a
360n/a class Foo(unittest.TestCase):
361n/a def test_nothing(self):
362n/a pass
363n/a
364n/a test = Foo('test_nothing')
365n/a wref = weakref.ref(test)
366n/a
367n/a suite = TestSuiteClass([wref()])
368n/a suite.run(unittest.TestResult())
369n/a
370n/a del test
371n/a
372n/a # for the benefit of non-reference counting implementations
373n/a gc.collect()
374n/a
375n/a self.assertEqual(suite._tests, [None])
376n/a self.assertIsNone(wref())
377n/a
378n/a def test_garbage_collect_test_after_run_BaseTestSuite(self):
379n/a self.assert_garbage_collect_test_after_run(unittest.BaseTestSuite)
380n/a
381n/a def test_garbage_collect_test_after_run_TestSuite(self):
382n/a self.assert_garbage_collect_test_after_run(unittest.TestSuite)
383n/a
384n/a def test_basetestsuite(self):
385n/a class Test(unittest.TestCase):
386n/a wasSetUp = False
387n/a wasTornDown = False
388n/a @classmethod
389n/a def setUpClass(cls):
390n/a cls.wasSetUp = True
391n/a @classmethod
392n/a def tearDownClass(cls):
393n/a cls.wasTornDown = True
394n/a def testPass(self):
395n/a pass
396n/a def testFail(self):
397n/a fail
398n/a class Module(object):
399n/a wasSetUp = False
400n/a wasTornDown = False
401n/a @staticmethod
402n/a def setUpModule():
403n/a Module.wasSetUp = True
404n/a @staticmethod
405n/a def tearDownModule():
406n/a Module.wasTornDown = True
407n/a
408n/a Test.__module__ = 'Module'
409n/a sys.modules['Module'] = Module
410n/a self.addCleanup(sys.modules.pop, 'Module')
411n/a
412n/a suite = unittest.BaseTestSuite()
413n/a suite.addTests([Test('testPass'), Test('testFail')])
414n/a self.assertEqual(suite.countTestCases(), 2)
415n/a
416n/a result = unittest.TestResult()
417n/a suite.run(result)
418n/a self.assertFalse(Module.wasSetUp)
419n/a self.assertFalse(Module.wasTornDown)
420n/a self.assertFalse(Test.wasSetUp)
421n/a self.assertFalse(Test.wasTornDown)
422n/a self.assertEqual(len(result.errors), 1)
423n/a self.assertEqual(len(result.failures), 0)
424n/a self.assertEqual(result.testsRun, 2)
425n/a self.assertEqual(suite.countTestCases(), 2)
426n/a
427n/a
428n/a def test_overriding_call(self):
429n/a class MySuite(unittest.TestSuite):
430n/a called = False
431n/a def __call__(self, *args, **kw):
432n/a self.called = True
433n/a unittest.TestSuite.__call__(self, *args, **kw)
434n/a
435n/a suite = MySuite()
436n/a result = unittest.TestResult()
437n/a wrapper = unittest.TestSuite()
438n/a wrapper.addTest(suite)
439n/a wrapper(result)
440n/a self.assertTrue(suite.called)
441n/a
442n/a # reusing results should be permitted even if abominable
443n/a self.assertFalse(result._testRunEntered)
444n/a
445n/a
446n/aif __name__ == '__main__':
447n/a unittest.main()