本文整理汇总了Python中six.moves.StringIO.truncate方法的典型用法代码示例。如果您正苦于以下问题:Python StringIO.truncate方法的具体用法?Python StringIO.truncate怎么用?Python StringIO.truncate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.StringIO
的用法示例。
在下文中一共展示了StringIO.truncate方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _XMLTestResult
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import truncate [as 别名]
#.........这里部分代码省略.........
# Handle partial and partialmethod objects.
test_method = getattr(test_method, 'func', test_method)
_, self.lineno = inspect.getsourcelines(test_method)
except (AttributeError, TypeError):
# issue #188, #189, some frameworks can make test method opaque.
pass
if self.showAll:
self.stream.write(' ' + self.getDescription(test))
self.stream.write(" ... ")
self.stream.flush()
def _setupStdout(self):
"""
Capture stdout / stderr by replacing sys.stdout / sys.stderr
"""
super(_XMLTestResult, self)._setupStdout()
self.__stdout_saved = sys.stdout
sys.stdout = _DuplicateWriter(sys.stdout, self._stdout_capture)
self.__stderr_saved = sys.stderr
sys.stderr = _DuplicateWriter(sys.stderr, self._stderr_capture)
def _restoreStdout(self):
"""
Stop capturing stdout / stderr and recover sys.stdout / sys.stderr
"""
if self.__stdout_saved:
sys.stdout = self.__stdout_saved
self.__stdout_saved = None
if self.__stderr_saved:
sys.stderr = self.__stderr_saved
self.__stderr_saved = None
self._stdout_capture.seek(0)
self._stdout_capture.truncate()
self._stderr_capture.seek(0)
self._stderr_capture.truncate()
super(_XMLTestResult, self)._restoreStdout()
def _save_output_data(self):
self._stdout_data = self._stdout_capture.getvalue()
self._stderr_data = self._stderr_capture.getvalue()
def stopTest(self, test):
"""
Called after execute each test method.
"""
self._save_output_data()
# self._stdout_data = sys.stdout.getvalue()
# self._stderr_data = sys.stderr.getvalue()
_TextTestResult.stopTest(self, test)
self.stop_time = time()
if self.callback and callable(self.callback):
self.callback()
self.callback = None
def addSuccess(self, test):
"""
Called when a test executes successfully.
"""
self._save_output_data()
self._prepare_callback(
self.infoclass(self, test), self.successes, 'ok', '.'
)
示例2: ResultCollector
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import truncate [as 别名]
class ResultCollector(object):
"""Collecter for test results. This handles creating
:class:`~.TestResult` instances and handing them off the registered
result output handlers.
"""
# Temporary compatibility with unittest's runner
separator2 = separator2
def __init__(self, buffer=False, failfast=False):
self.buffer = buffer
self.failfast = failfast
self._result_handlers = []
self._sorted_handlers = None
self.testsRun = 0
self.expectedFailures = []
self.unexpectedSuccesses = []
self.skipped = []
self.failures = []
self.errors = []
self.shouldStop = False
self._successful = True
self._mirror_output = False
self._stderr_buffer = None
self._stdout_buffer = None
self._original_stderr = sys.stderr
self._original_stdout = sys.stdout
self._test_timing = {}
@property
def _handlers(self):
if self._sorted_handlers is None:
from .plugins.result_handler import sort_result_handlers
self._sorted_handlers = sort_result_handlers(self._result_handlers)
return self._sorted_handlers
@staticmethod
def _testcase_to_key(test):
return (type(test), test._testMethodName)
def _setup_stdout(self):
"""Hook stdout and stderr if buffering is enabled.
"""
if self.buffer:
if self._stderr_buffer is None:
self._stderr_buffer = StringIO()
self._stdout_buffer = StringIO()
sys.stdout = self._stdout_buffer
sys.stderr = self._stderr_buffer
def _restore_stdout(self):
"""Unhook stdout and stderr if buffering is enabled.
"""
if self.buffer:
if self._mirror_output:
output = sys.stdout.getvalue()
error = sys.stderr.getvalue()
if output:
if not output.endswith('\n'):
output += '\n'
self._original_stdout.write(STDOUT_LINE % output)
if error:
if not error.endswith('\n'):
error += '\n'
self._original_stderr.write(STDERR_LINE % error)
sys.stdout = self._original_stdout
sys.stderr = self._original_stderr
self._stdout_buffer.seek(0)
self._stdout_buffer.truncate()
self._stderr_buffer.seek(0)
self._stderr_buffer.truncate()
def printErrors(self): # pragma: no cover
# FIXME: Remove
pass
def add_result_handler(self, handler):
"""Register a new result handler.
"""
self._result_handlers.append(handler)
# Reset sorted handlers
if self._sorted_handlers:
self._sorted_handlers = None
def startTest(self, test, start_time=None):
"""Indicate that an individual test is starting.
Parameters
----------
test : unittest.TestCase
The test that is starting.
start_time : datetime
An internal parameter to allow the parallel test runner to
set the actual start time of a test run in a subprocess.
#.........这里部分代码省略.........
示例3: TestResult
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import truncate [as 别名]
class TestResult(unittest.TestResult):
"""Holder for test result information.
Test results are automatically managed by the TestCase and TestSuite
classes, and do not need to be explicitly manipulated by writers of tests.
Each instance holds the total number of tests run, and collections of
failures and errors that occurred among those test runs. The collections
contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
formatted traceback of the error that occurred.
"""
_previousTestClass = None
_moduleSetUpFailed = False
def __init__(self, stream=None, descriptions=None, verbosity=None):
self.failfast = False
self.failures = []
self.errors = []
self.testsRun = 0
self.skipped = []
self.expectedFailures = []
self.unexpectedSuccesses = []
self.shouldStop = False
self.buffer = False
self._stdout_buffer = None
self._stderr_buffer = None
self._original_stdout = sys.stdout
self._original_stderr = sys.stderr
self._mirrorOutput = False
def startTest(self, test):
"Called when the given test is about to be run"
self.testsRun += 1
self._mirrorOutput = False
if self.buffer:
if self._stderr_buffer is None:
self._stderr_buffer = StringIO()
self._stdout_buffer = StringIO()
sys.stdout = self._stdout_buffer
sys.stderr = self._stderr_buffer
def startTestRun(self):
"""Called once before any tests are executed.
See startTest for a method called before each test.
"""
def stopTest(self, test):
"""Called when the given test has been run"""
if self.buffer:
if self._mirrorOutput:
output = sys.stdout.getvalue()
error = sys.stderr.getvalue()
if output:
if not output.endswith('\n'):
output += '\n'
self._original_stdout.write(STDOUT_LINE % output)
if error:
if not error.endswith('\n'):
error += '\n'
self._original_stderr.write(STDERR_LINE % error)
sys.stdout = self._original_stdout
sys.stderr = self._original_stderr
self._stdout_buffer.seek(0)
self._stdout_buffer.truncate()
self._stderr_buffer.seek(0)
self._stderr_buffer.truncate()
self._mirrorOutput = False
def stopTestRun(self):
"""Called once after all tests are executed.
See stopTest for a method called after each test.
"""
@failfast
def addError(self, test, err):
"""Called when an error has occurred. 'err' is a tuple of values as
returned by sys.exc_info().
"""
self.errors.append((test, self._exc_info_to_string(err, test)))
self._mirrorOutput = True
@failfast
def addFailure(self, test, err):
"""Called when an error has occurred. 'err' is a tuple of values as
returned by sys.exc_info()."""
self.failures.append((test, self._exc_info_to_string(err, test)))
self._mirrorOutput = True
@failfast
def addSubTest(self, test, subtest, err):
"""Called at the end of a subtest.
'err' is None if the subtest ended successfully, otherwise it's a
tuple of values as returned by sys.exc_info().
"""
# By default, we don't do anything with successful subtests, but
# more sophisticated test results might want to record them.
#.........这里部分代码省略.........