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

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

#countcontent
1n/aimport unittest
2n/a
3n/afrom unittest.test.support import LoggingResult
4n/a
5n/a
6n/aclass Test_FunctionTestCase(unittest.TestCase):
7n/a
8n/a # "Return the number of tests represented by the this test object. For
9n/a # TestCase instances, this will always be 1"
10n/a def test_countTestCases(self):
11n/a test = unittest.FunctionTestCase(lambda: None)
12n/a
13n/a self.assertEqual(test.countTestCases(), 1)
14n/a
15n/a # "When a setUp() method is defined, the test runner will run that method
16n/a # prior to each test. Likewise, if a tearDown() method is defined, the
17n/a # test runner will invoke that method after each test. In the example,
18n/a # setUp() was used to create a fresh sequence for each test."
19n/a #
20n/a # Make sure the proper call order is maintained, even if setUp() raises
21n/a # an exception.
22n/a def test_run_call_order__error_in_setUp(self):
23n/a events = []
24n/a result = LoggingResult(events)
25n/a
26n/a def setUp():
27n/a events.append('setUp')
28n/a raise RuntimeError('raised by setUp')
29n/a
30n/a def test():
31n/a events.append('test')
32n/a
33n/a def tearDown():
34n/a events.append('tearDown')
35n/a
36n/a expected = ['startTest', 'setUp', 'addError', 'stopTest']
37n/a unittest.FunctionTestCase(test, setUp, tearDown).run(result)
38n/a self.assertEqual(events, expected)
39n/a
40n/a # "When a setUp() method is defined, the test runner will run that method
41n/a # prior to each test. Likewise, if a tearDown() method is defined, the
42n/a # test runner will invoke that method after each test. In the example,
43n/a # setUp() was used to create a fresh sequence for each test."
44n/a #
45n/a # Make sure the proper call order is maintained, even if the test raises
46n/a # an error (as opposed to a failure).
47n/a def test_run_call_order__error_in_test(self):
48n/a events = []
49n/a result = LoggingResult(events)
50n/a
51n/a def setUp():
52n/a events.append('setUp')
53n/a
54n/a def test():
55n/a events.append('test')
56n/a raise RuntimeError('raised by test')
57n/a
58n/a def tearDown():
59n/a events.append('tearDown')
60n/a
61n/a expected = ['startTest', 'setUp', 'test', 'tearDown',
62n/a 'addError', 'stopTest']
63n/a unittest.FunctionTestCase(test, setUp, tearDown).run(result)
64n/a self.assertEqual(events, expected)
65n/a
66n/a # "When a setUp() method is defined, the test runner will run that method
67n/a # prior to each test. Likewise, if a tearDown() method is defined, the
68n/a # test runner will invoke that method after each test. In the example,
69n/a # setUp() was used to create a fresh sequence for each test."
70n/a #
71n/a # Make sure the proper call order is maintained, even if the test signals
72n/a # a failure (as opposed to an error).
73n/a def test_run_call_order__failure_in_test(self):
74n/a events = []
75n/a result = LoggingResult(events)
76n/a
77n/a def setUp():
78n/a events.append('setUp')
79n/a
80n/a def test():
81n/a events.append('test')
82n/a self.fail('raised by test')
83n/a
84n/a def tearDown():
85n/a events.append('tearDown')
86n/a
87n/a expected = ['startTest', 'setUp', 'test', 'tearDown',
88n/a 'addFailure', 'stopTest']
89n/a unittest.FunctionTestCase(test, setUp, tearDown).run(result)
90n/a self.assertEqual(events, expected)
91n/a
92n/a # "When a setUp() method is defined, the test runner will run that method
93n/a # prior to each test. Likewise, if a tearDown() method is defined, the
94n/a # test runner will invoke that method after each test. In the example,
95n/a # setUp() was used to create a fresh sequence for each test."
96n/a #
97n/a # Make sure the proper call order is maintained, even if tearDown() raises
98n/a # an exception.
99n/a def test_run_call_order__error_in_tearDown(self):
100n/a events = []
101n/a result = LoggingResult(events)
102n/a
103n/a def setUp():
104n/a events.append('setUp')
105n/a
106n/a def test():
107n/a events.append('test')
108n/a
109n/a def tearDown():
110n/a events.append('tearDown')
111n/a raise RuntimeError('raised by tearDown')
112n/a
113n/a expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
114n/a 'stopTest']
115n/a unittest.FunctionTestCase(test, setUp, tearDown).run(result)
116n/a self.assertEqual(events, expected)
117n/a
118n/a # "Return a string identifying the specific test case."
119n/a #
120n/a # Because of the vague nature of the docs, I'm not going to lock this
121n/a # test down too much. Really all that can be asserted is that the id()
122n/a # will be a string (either 8-byte or unicode -- again, because the docs
123n/a # just say "string")
124n/a def test_id(self):
125n/a test = unittest.FunctionTestCase(lambda: None)
126n/a
127n/a self.assertIsInstance(test.id(), str)
128n/a
129n/a # "Returns a one-line description of the test, or None if no description
130n/a # has been provided. The default implementation of this method returns
131n/a # the first line of the test method's docstring, if available, or None."
132n/a def test_shortDescription__no_docstring(self):
133n/a test = unittest.FunctionTestCase(lambda: None)
134n/a
135n/a self.assertEqual(test.shortDescription(), None)
136n/a
137n/a # "Returns a one-line description of the test, or None if no description
138n/a # has been provided. The default implementation of this method returns
139n/a # the first line of the test method's docstring, if available, or None."
140n/a def test_shortDescription__singleline_docstring(self):
141n/a desc = "this tests foo"
142n/a test = unittest.FunctionTestCase(lambda: None, description=desc)
143n/a
144n/a self.assertEqual(test.shortDescription(), "this tests foo")
145n/a
146n/a
147n/aif __name__ == "__main__":
148n/a unittest.main()