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

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

#countcontent
1n/aimport io
2n/aimport os
3n/aimport sys
4n/aimport pickle
5n/aimport subprocess
6n/a
7n/aimport unittest
8n/afrom unittest.case import _Outcome
9n/a
10n/afrom unittest.test.support import (LoggingResult,
11n/a ResultWithNoStartTestRunStopTestRun)
12n/a
13n/a
14n/aclass TestCleanUp(unittest.TestCase):
15n/a
16n/a def testCleanUp(self):
17n/a class TestableTest(unittest.TestCase):
18n/a def testNothing(self):
19n/a pass
20n/a
21n/a test = TestableTest('testNothing')
22n/a self.assertEqual(test._cleanups, [])
23n/a
24n/a cleanups = []
25n/a
26n/a def cleanup1(*args, **kwargs):
27n/a cleanups.append((1, args, kwargs))
28n/a
29n/a def cleanup2(*args, **kwargs):
30n/a cleanups.append((2, args, kwargs))
31n/a
32n/a test.addCleanup(cleanup1, 1, 2, 3, four='hello', five='goodbye')
33n/a test.addCleanup(cleanup2)
34n/a
35n/a self.assertEqual(test._cleanups,
36n/a [(cleanup1, (1, 2, 3), dict(four='hello', five='goodbye')),
37n/a (cleanup2, (), {})])
38n/a
39n/a self.assertTrue(test.doCleanups())
40n/a self.assertEqual(cleanups, [(2, (), {}), (1, (1, 2, 3), dict(four='hello', five='goodbye'))])
41n/a
42n/a def testCleanUpWithErrors(self):
43n/a class TestableTest(unittest.TestCase):
44n/a def testNothing(self):
45n/a pass
46n/a
47n/a test = TestableTest('testNothing')
48n/a outcome = test._outcome = _Outcome()
49n/a
50n/a exc1 = Exception('foo')
51n/a exc2 = Exception('bar')
52n/a def cleanup1():
53n/a raise exc1
54n/a
55n/a def cleanup2():
56n/a raise exc2
57n/a
58n/a test.addCleanup(cleanup1)
59n/a test.addCleanup(cleanup2)
60n/a
61n/a self.assertFalse(test.doCleanups())
62n/a self.assertFalse(outcome.success)
63n/a
64n/a ((_, (Type1, instance1, _)),
65n/a (_, (Type2, instance2, _))) = reversed(outcome.errors)
66n/a self.assertEqual((Type1, instance1), (Exception, exc1))
67n/a self.assertEqual((Type2, instance2), (Exception, exc2))
68n/a
69n/a def testCleanupInRun(self):
70n/a blowUp = False
71n/a ordering = []
72n/a
73n/a class TestableTest(unittest.TestCase):
74n/a def setUp(self):
75n/a ordering.append('setUp')
76n/a if blowUp:
77n/a raise Exception('foo')
78n/a
79n/a def testNothing(self):
80n/a ordering.append('test')
81n/a
82n/a def tearDown(self):
83n/a ordering.append('tearDown')
84n/a
85n/a test = TestableTest('testNothing')
86n/a
87n/a def cleanup1():
88n/a ordering.append('cleanup1')
89n/a def cleanup2():
90n/a ordering.append('cleanup2')
91n/a test.addCleanup(cleanup1)
92n/a test.addCleanup(cleanup2)
93n/a
94n/a def success(some_test):
95n/a self.assertEqual(some_test, test)
96n/a ordering.append('success')
97n/a
98n/a result = unittest.TestResult()
99n/a result.addSuccess = success
100n/a
101n/a test.run(result)
102n/a self.assertEqual(ordering, ['setUp', 'test', 'tearDown',
103n/a 'cleanup2', 'cleanup1', 'success'])
104n/a
105n/a blowUp = True
106n/a ordering = []
107n/a test = TestableTest('testNothing')
108n/a test.addCleanup(cleanup1)
109n/a test.run(result)
110n/a self.assertEqual(ordering, ['setUp', 'cleanup1'])
111n/a
112n/a def testTestCaseDebugExecutesCleanups(self):
113n/a ordering = []
114n/a
115n/a class TestableTest(unittest.TestCase):
116n/a def setUp(self):
117n/a ordering.append('setUp')
118n/a self.addCleanup(cleanup1)
119n/a
120n/a def testNothing(self):
121n/a ordering.append('test')
122n/a
123n/a def tearDown(self):
124n/a ordering.append('tearDown')
125n/a
126n/a test = TestableTest('testNothing')
127n/a
128n/a def cleanup1():
129n/a ordering.append('cleanup1')
130n/a test.addCleanup(cleanup2)
131n/a def cleanup2():
132n/a ordering.append('cleanup2')
133n/a
134n/a test.debug()
135n/a self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup1', 'cleanup2'])
136n/a
137n/a
138n/aclass Test_TextTestRunner(unittest.TestCase):
139n/a """Tests for TextTestRunner."""
140n/a
141n/a def setUp(self):
142n/a # clean the environment from pre-existing PYTHONWARNINGS to make
143n/a # test_warnings results consistent
144n/a self.pythonwarnings = os.environ.get('PYTHONWARNINGS')
145n/a if self.pythonwarnings:
146n/a del os.environ['PYTHONWARNINGS']
147n/a
148n/a def tearDown(self):
149n/a # bring back pre-existing PYTHONWARNINGS if present
150n/a if self.pythonwarnings:
151n/a os.environ['PYTHONWARNINGS'] = self.pythonwarnings
152n/a
153n/a def test_init(self):
154n/a runner = unittest.TextTestRunner()
155n/a self.assertFalse(runner.failfast)
156n/a self.assertFalse(runner.buffer)
157n/a self.assertEqual(runner.verbosity, 1)
158n/a self.assertEqual(runner.warnings, None)
159n/a self.assertTrue(runner.descriptions)
160n/a self.assertEqual(runner.resultclass, unittest.TextTestResult)
161n/a self.assertFalse(runner.tb_locals)
162n/a
163n/a def test_multiple_inheritance(self):
164n/a class AResult(unittest.TestResult):
165n/a def __init__(self, stream, descriptions, verbosity):
166n/a super(AResult, self).__init__(stream, descriptions, verbosity)
167n/a
168n/a class ATextResult(unittest.TextTestResult, AResult):
169n/a pass
170n/a
171n/a # This used to raise an exception due to TextTestResult not passing
172n/a # on arguments in its __init__ super call
173n/a ATextResult(None, None, 1)
174n/a
175n/a def testBufferAndFailfast(self):
176n/a class Test(unittest.TestCase):
177n/a def testFoo(self):
178n/a pass
179n/a result = unittest.TestResult()
180n/a runner = unittest.TextTestRunner(stream=io.StringIO(), failfast=True,
181n/a buffer=True)
182n/a # Use our result object
183n/a runner._makeResult = lambda: result
184n/a runner.run(Test('testFoo'))
185n/a
186n/a self.assertTrue(result.failfast)
187n/a self.assertTrue(result.buffer)
188n/a
189n/a def test_locals(self):
190n/a runner = unittest.TextTestRunner(stream=io.StringIO(), tb_locals=True)
191n/a result = runner.run(unittest.TestSuite())
192n/a self.assertEqual(True, result.tb_locals)
193n/a
194n/a def testRunnerRegistersResult(self):
195n/a class Test(unittest.TestCase):
196n/a def testFoo(self):
197n/a pass
198n/a originalRegisterResult = unittest.runner.registerResult
199n/a def cleanup():
200n/a unittest.runner.registerResult = originalRegisterResult
201n/a self.addCleanup(cleanup)
202n/a
203n/a result = unittest.TestResult()
204n/a runner = unittest.TextTestRunner(stream=io.StringIO())
205n/a # Use our result object
206n/a runner._makeResult = lambda: result
207n/a
208n/a self.wasRegistered = 0
209n/a def fakeRegisterResult(thisResult):
210n/a self.wasRegistered += 1
211n/a self.assertEqual(thisResult, result)
212n/a unittest.runner.registerResult = fakeRegisterResult
213n/a
214n/a runner.run(unittest.TestSuite())
215n/a self.assertEqual(self.wasRegistered, 1)
216n/a
217n/a def test_works_with_result_without_startTestRun_stopTestRun(self):
218n/a class OldTextResult(ResultWithNoStartTestRunStopTestRun):
219n/a separator2 = ''
220n/a def printErrors(self):
221n/a pass
222n/a
223n/a class Runner(unittest.TextTestRunner):
224n/a def __init__(self):
225n/a super(Runner, self).__init__(io.StringIO())
226n/a
227n/a def _makeResult(self):
228n/a return OldTextResult()
229n/a
230n/a runner = Runner()
231n/a runner.run(unittest.TestSuite())
232n/a
233n/a def test_startTestRun_stopTestRun_called(self):
234n/a class LoggingTextResult(LoggingResult):
235n/a separator2 = ''
236n/a def printErrors(self):
237n/a pass
238n/a
239n/a class LoggingRunner(unittest.TextTestRunner):
240n/a def __init__(self, events):
241n/a super(LoggingRunner, self).__init__(io.StringIO())
242n/a self._events = events
243n/a
244n/a def _makeResult(self):
245n/a return LoggingTextResult(self._events)
246n/a
247n/a events = []
248n/a runner = LoggingRunner(events)
249n/a runner.run(unittest.TestSuite())
250n/a expected = ['startTestRun', 'stopTestRun']
251n/a self.assertEqual(events, expected)
252n/a
253n/a def test_pickle_unpickle(self):
254n/a # Issue #7197: a TextTestRunner should be (un)pickleable. This is
255n/a # required by test_multiprocessing under Windows (in verbose mode).
256n/a stream = io.StringIO("foo")
257n/a runner = unittest.TextTestRunner(stream)
258n/a for protocol in range(2, pickle.HIGHEST_PROTOCOL + 1):
259n/a s = pickle.dumps(runner, protocol)
260n/a obj = pickle.loads(s)
261n/a # StringIO objects never compare equal, a cheap test instead.
262n/a self.assertEqual(obj.stream.getvalue(), stream.getvalue())
263n/a
264n/a def test_resultclass(self):
265n/a def MockResultClass(*args):
266n/a return args
267n/a STREAM = object()
268n/a DESCRIPTIONS = object()
269n/a VERBOSITY = object()
270n/a runner = unittest.TextTestRunner(STREAM, DESCRIPTIONS, VERBOSITY,
271n/a resultclass=MockResultClass)
272n/a self.assertEqual(runner.resultclass, MockResultClass)
273n/a
274n/a expectedresult = (runner.stream, DESCRIPTIONS, VERBOSITY)
275n/a self.assertEqual(runner._makeResult(), expectedresult)
276n/a
277n/a def test_warnings(self):
278n/a """
279n/a Check that warnings argument of TextTestRunner correctly affects the
280n/a behavior of the warnings.
281n/a """
282n/a # see #10535 and the _test_warnings file for more information
283n/a
284n/a def get_parse_out_err(p):
285n/a return [b.splitlines() for b in p.communicate()]
286n/a opts = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE,
287n/a cwd=os.path.dirname(__file__))
288n/a ae_msg = b'Please use assertEqual instead.'
289n/a at_msg = b'Please use assertTrue instead.'
290n/a
291n/a # no args -> all the warnings are printed, unittest warnings only once
292n/a p = subprocess.Popen([sys.executable, '_test_warnings.py'], **opts)
293n/a with p:
294n/a out, err = get_parse_out_err(p)
295n/a self.assertIn(b'OK', err)
296n/a # check that the total number of warnings in the output is correct
297n/a self.assertEqual(len(out), 12)
298n/a # check that the numbers of the different kind of warnings is correct
299n/a for msg in [b'dw', b'iw', b'uw']:
300n/a self.assertEqual(out.count(msg), 3)
301n/a for msg in [ae_msg, at_msg, b'rw']:
302n/a self.assertEqual(out.count(msg), 1)
303n/a
304n/a args_list = (
305n/a # passing 'ignore' as warnings arg -> no warnings
306n/a [sys.executable, '_test_warnings.py', 'ignore'],
307n/a # -W doesn't affect the result if the arg is passed
308n/a [sys.executable, '-Wa', '_test_warnings.py', 'ignore'],
309n/a # -W affects the result if the arg is not passed
310n/a [sys.executable, '-Wi', '_test_warnings.py']
311n/a )
312n/a # in all these cases no warnings are printed
313n/a for args in args_list:
314n/a p = subprocess.Popen(args, **opts)
315n/a with p:
316n/a out, err = get_parse_out_err(p)
317n/a self.assertIn(b'OK', err)
318n/a self.assertEqual(len(out), 0)
319n/a
320n/a
321n/a # passing 'always' as warnings arg -> all the warnings printed,
322n/a # unittest warnings only once
323n/a p = subprocess.Popen([sys.executable, '_test_warnings.py', 'always'],
324n/a **opts)
325n/a with p:
326n/a out, err = get_parse_out_err(p)
327n/a self.assertIn(b'OK', err)
328n/a self.assertEqual(len(out), 14)
329n/a for msg in [b'dw', b'iw', b'uw', b'rw']:
330n/a self.assertEqual(out.count(msg), 3)
331n/a for msg in [ae_msg, at_msg]:
332n/a self.assertEqual(out.count(msg), 1)
333n/a
334n/a def testStdErrLookedUpAtInstantiationTime(self):
335n/a # see issue 10786
336n/a old_stderr = sys.stderr
337n/a f = io.StringIO()
338n/a sys.stderr = f
339n/a try:
340n/a runner = unittest.TextTestRunner()
341n/a self.assertTrue(runner.stream.stream is f)
342n/a finally:
343n/a sys.stderr = old_stderr
344n/a
345n/a def testSpecifiedStreamUsed(self):
346n/a # see issue 10786
347n/a f = io.StringIO()
348n/a runner = unittest.TextTestRunner(f)
349n/a self.assertTrue(runner.stream.stream is f)
350n/a
351n/a
352n/aif __name__ == "__main__":
353n/a unittest.main()