[英]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.