English 中文(简体)
使用 Python 单位测试xml- report 打印到 stdout 和 XML 文件
原标题:Printing to stdout and XML file using Python unittest-xml-reporting

我使用 Python 的 unitest, < a href="https://github.com/danielfm/unitest-xml- reporting" rel=“nofollow”>unitst-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 文件 。 感谢任何帮助 。 我很乐意提交一个拉动请求, 当它工作的时候!

最佳回答

目前版本的“ href=”“http://pypi.python.org/pypi/unitest-xml-reporting/” rel=“nofollow”>http://pypi.python.org/pypi/unitest-xml-reporting/ 。

我用这里的样本代码尝试了它, 测试输出到 xml 和 stdout 都 < a href="https://github.com/danielfm/unitest-xml- reporting" rel="nofollow" >https://github.com/danielfm/unitest-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)
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签