简体   繁体   English

使用Python unittest-xml-reporting打印到stdout和XML文件

[英]Printing to stdout and XML file using Python unittest-xml-reporting

I am using an extension of python's unittest, unittest-xml-reporting . 我正在使用python的unittest, unittest-xml-reporting的扩展。 It currently captures stdout and stores it in the xml output file. 它当前捕获stdout并将其存储在xml输出文件中。 Awesome! 真棒! But, I also want to echo it to the screen so I don't have to view that file every time I run my test suite. 但是,我也希望将它回显到屏幕上,这样我每次运行测试套件时都不必查看该文件。 The two main functions involved are: 涉及的两个主要功能是:

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)

I tried removing the 我试过删除

(sys.stdout, sys.stderr) = (self.stdout, self.stderr) = (StringIO(), StringIO())

and replace it with 并替换它

(self.stdout, self.stderr) = (StringIO(), StringIO())

but then it did not add it to the xml file. 但后来它没有将它添加到xml文件中。 Any help is appreciated. 任何帮助表示赞赏。 I'll be glad to submit a pull request when I get it working too! 当我开始工作时,我会很高兴提交拉取请求!

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

I tried it with the sample code here and the test output goes to xml and the stdout both https://github.com/danielfm/unittest-xml-reporting 我在这里用示例代码尝试了它,测试输出转到xml和stdout都https://github.com/danielfm/unittest-xml-reporting

Moreover the corresponding code apparently updated now :- 此外相应的代码现在明显更新: -

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM