[英]Printing to stdout and XML file using Python unittest-xml-reporting
我正在使用python的unittest, unittest-xml-reporting的扩展。 它当前捕获stdout并将其存储在xml输出文件中。 真棒! 但是,我也希望将它回显到屏幕上,这样我每次运行测试套件时都不必查看该文件。 涉及的两个主要功能是:
def _patch_standard_output(self):
"""Replace the stdout and stderr streams with string-based streams
in order to capture the tests' output.
"""
(self.old_stdout, self.old_stderr) = (sys.stdout, sys.stderr)
(sys.stdout, sys.stderr) = (self.stdout, self.stderr) = \
(StringIO(), StringIO())
def _restore_standard_output(self):
"Restore the stdout and stderr streams."
(sys.stdout, sys.stderr) = (self.old_stdout, self.old_stderr)
我试过删除
(sys.stdout, sys.stderr) = (self.stdout, self.stderr) = (StringIO(), StringIO())
并替换它
(self.stdout, self.stderr) = (StringIO(), StringIO())
但后来它没有将它添加到xml文件中。 任何帮助表示赞赏。 当我开始工作时,我会很高兴提交拉取请求!
当前版本执行此操作http://pypi.python.org/pypi/unittest-xml-reporting/ 。
我在这里用示例代码尝试了它,测试输出转到xml和stdout都https://github.com/danielfm/unittest-xml-reporting
此外相应的代码现在明显更新: -
class _DelegateIO(object):
"""This class defines an object that captures whatever is written to
a stream or file.
"""
def __init__(self, delegate):
self._captured = StringIO()
self.delegate = delegate
def write(self, text):
self._captured.write(text)
self.delegate.write(text)
def _patch_standard_output(self):
"""Replaces stdout and stderr streams with string-based streams
in order to capture the tests' output.
"""
sys.stdout = _DelegateIO(sys.stdout)
sys.stderr = _DelegateIO(sys.stderr)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.