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

Python code coverage for Lib/unittest/result.py

#countcontent
1n/a"""Test result object"""
2n/a
3n/aimport io
4n/aimport sys
5n/aimport traceback
6n/a
7n/afrom . import util
8n/afrom functools import wraps
9n/a
10n/a__unittest = True
11n/a
12n/adef failfast(method):
13n/a @wraps(method)
14n/a def inner(self, *args, **kw):
15n/a if getattr(self, 'failfast', False):
16n/a self.stop()
17n/a return method(self, *args, **kw)
18n/a return inner
19n/a
20n/aSTDOUT_LINE = '\nStdout:\n%s'
21n/aSTDERR_LINE = '\nStderr:\n%s'
22n/a
23n/a
24n/aclass TestResult(object):
25n/a """Holder for test result information.
26n/a
27n/a Test results are automatically managed by the TestCase and TestSuite
28n/a classes, and do not need to be explicitly manipulated by writers of tests.
29n/a
30n/a Each instance holds the total number of tests run, and collections of
31n/a failures and errors that occurred among those test runs. The collections
32n/a contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
33n/a formatted traceback of the error that occurred.
34n/a """
35n/a _previousTestClass = None
36n/a _testRunEntered = False
37n/a _moduleSetUpFailed = False
38n/a def __init__(self, stream=None, descriptions=None, verbosity=None):
39n/a self.failfast = False
40n/a self.failures = []
41n/a self.errors = []
42n/a self.testsRun = 0
43n/a self.skipped = []
44n/a self.expectedFailures = []
45n/a self.unexpectedSuccesses = []
46n/a self.shouldStop = False
47n/a self.buffer = False
48n/a self.tb_locals = False
49n/a self._stdout_buffer = None
50n/a self._stderr_buffer = None
51n/a self._original_stdout = sys.stdout
52n/a self._original_stderr = sys.stderr
53n/a self._mirrorOutput = False
54n/a
55n/a def printErrors(self):
56n/a "Called by TestRunner after test run"
57n/a
58n/a def startTest(self, test):
59n/a "Called when the given test is about to be run"
60n/a self.testsRun += 1
61n/a self._mirrorOutput = False
62n/a self._setupStdout()
63n/a
64n/a def _setupStdout(self):
65n/a if self.buffer:
66n/a if self._stderr_buffer is None:
67n/a self._stderr_buffer = io.StringIO()
68n/a self._stdout_buffer = io.StringIO()
69n/a sys.stdout = self._stdout_buffer
70n/a sys.stderr = self._stderr_buffer
71n/a
72n/a def startTestRun(self):
73n/a """Called once before any tests are executed.
74n/a
75n/a See startTest for a method called before each test.
76n/a """
77n/a
78n/a def stopTest(self, test):
79n/a """Called when the given test has been run"""
80n/a self._restoreStdout()
81n/a self._mirrorOutput = False
82n/a
83n/a def _restoreStdout(self):
84n/a if self.buffer:
85n/a if self._mirrorOutput:
86n/a output = sys.stdout.getvalue()
87n/a error = sys.stderr.getvalue()
88n/a if output:
89n/a if not output.endswith('\n'):
90n/a output += '\n'
91n/a self._original_stdout.write(STDOUT_LINE % output)
92n/a if error:
93n/a if not error.endswith('\n'):
94n/a error += '\n'
95n/a self._original_stderr.write(STDERR_LINE % error)
96n/a
97n/a sys.stdout = self._original_stdout
98n/a sys.stderr = self._original_stderr
99n/a self._stdout_buffer.seek(0)
100n/a self._stdout_buffer.truncate()
101n/a self._stderr_buffer.seek(0)
102n/a self._stderr_buffer.truncate()
103n/a
104n/a def stopTestRun(self):
105n/a """Called once after all tests are executed.
106n/a
107n/a See stopTest for a method called after each test.
108n/a """
109n/a
110n/a @failfast
111n/a def addError(self, test, err):
112n/a """Called when an error has occurred. 'err' is a tuple of values as
113n/a returned by sys.exc_info().
114n/a """
115n/a self.errors.append((test, self._exc_info_to_string(err, test)))
116n/a self._mirrorOutput = True
117n/a
118n/a @failfast
119n/a def addFailure(self, test, err):
120n/a """Called when an error has occurred. 'err' is a tuple of values as
121n/a returned by sys.exc_info()."""
122n/a self.failures.append((test, self._exc_info_to_string(err, test)))
123n/a self._mirrorOutput = True
124n/a
125n/a def addSubTest(self, test, subtest, err):
126n/a """Called at the end of a subtest.
127n/a 'err' is None if the subtest ended successfully, otherwise it's a
128n/a tuple of values as returned by sys.exc_info().
129n/a """
130n/a # By default, we don't do anything with successful subtests, but
131n/a # more sophisticated test results might want to record them.
132n/a if err is not None:
133n/a if getattr(self, 'failfast', False):
134n/a self.stop()
135n/a if issubclass(err[0], test.failureException):
136n/a errors = self.failures
137n/a else:
138n/a errors = self.errors
139n/a errors.append((subtest, self._exc_info_to_string(err, test)))
140n/a self._mirrorOutput = True
141n/a
142n/a def addSuccess(self, test):
143n/a "Called when a test has completed successfully"
144n/a pass
145n/a
146n/a def addSkip(self, test, reason):
147n/a """Called when a test is skipped."""
148n/a self.skipped.append((test, reason))
149n/a
150n/a def addExpectedFailure(self, test, err):
151n/a """Called when an expected failure/error occurred."""
152n/a self.expectedFailures.append(
153n/a (test, self._exc_info_to_string(err, test)))
154n/a
155n/a @failfast
156n/a def addUnexpectedSuccess(self, test):
157n/a """Called when a test was expected to fail, but succeed."""
158n/a self.unexpectedSuccesses.append(test)
159n/a
160n/a def wasSuccessful(self):
161n/a """Tells whether or not this result was a success."""
162n/a # The hasattr check is for test_result's OldResult test. That
163n/a # way this method works on objects that lack the attribute.
164n/a # (where would such result intances come from? old stored pickles?)
165n/a return ((len(self.failures) == len(self.errors) == 0) and
166n/a (not hasattr(self, 'unexpectedSuccesses') or
167n/a len(self.unexpectedSuccesses) == 0))
168n/a
169n/a def stop(self):
170n/a """Indicates that the tests should be aborted."""
171n/a self.shouldStop = True
172n/a
173n/a def _exc_info_to_string(self, err, test):
174n/a """Converts a sys.exc_info()-style tuple of values into a string."""
175n/a exctype, value, tb = err
176n/a # Skip test runner traceback levels
177n/a while tb and self._is_relevant_tb_level(tb):
178n/a tb = tb.tb_next
179n/a
180n/a if exctype is test.failureException:
181n/a # Skip assert*() traceback levels
182n/a length = self._count_relevant_tb_levels(tb)
183n/a else:
184n/a length = None
185n/a tb_e = traceback.TracebackException(
186n/a exctype, value, tb, limit=length, capture_locals=self.tb_locals)
187n/a msgLines = list(tb_e.format())
188n/a
189n/a if self.buffer:
190n/a output = sys.stdout.getvalue()
191n/a error = sys.stderr.getvalue()
192n/a if output:
193n/a if not output.endswith('\n'):
194n/a output += '\n'
195n/a msgLines.append(STDOUT_LINE % output)
196n/a if error:
197n/a if not error.endswith('\n'):
198n/a error += '\n'
199n/a msgLines.append(STDERR_LINE % error)
200n/a return ''.join(msgLines)
201n/a
202n/a
203n/a def _is_relevant_tb_level(self, tb):
204n/a return '__unittest' in tb.tb_frame.f_globals
205n/a
206n/a def _count_relevant_tb_levels(self, tb):
207n/a length = 0
208n/a while tb and not self._is_relevant_tb_level(tb):
209n/a length += 1
210n/a tb = tb.tb_next
211n/a return length
212n/a
213n/a def __repr__(self):
214n/a return ("<%s run=%i errors=%i failures=%i>" %
215n/a (util.strclass(self.__class__), self.testsRun, len(self.errors),
216n/a len(self.failures)))