【问题标题】:Printing to stdout and XML file using Python unittest-xml-reporting使用 Python unittest-xml-reporting 打印到标准输出和 XML 文件
【发布时间】:2012-05-24 22:32:41
【问题描述】:

我正在使用 python 单元测试的扩展,unittest-xml-reporting。它当前捕获标准输出并将其存储在 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 文件中。任何帮助表示赞赏。当我也能正常工作时,我会很高兴提交拉取请求!

【问题讨论】:

    标签: python unit-testing junit jenkins


    【解决方案1】:

    当前版本执行此操作http://pypi.python.org/pypi/unittest-xml-reporting/

    我在这里使用示例代码进行了尝试,测试输出到 xml 和标准输出都 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)
    

    【讨论】:

      猜你喜欢
      • 2013-05-01
      • 1970-01-01
      • 2011-12-15
      • 2016-06-26
      • 2015-08-31
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多