3

我正在使用 python 的 unittest 的扩展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 文件中。任何帮助表示赞赏。当我得到它的工作时,我会很高兴提交一个拉取请求!

4

1 回答 1

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-01-10T04:54:36.520 回答