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