Printing to stdout and XML file using Python report unittest-xml

I am using the python extension unittest, unittest-xml-reporting . It currently writes stdout and saves it in the output xml file. Awesome! But I also want to give it to the screen, so I do not need to view this file every time I run my test suite. Two main functions:

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 to remove

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

and replace it with

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

but then he did not add it to the xml file. Any help is appreciated. I will be happy to submit a pull request when I earn it too!

+3
source share
1 answer

The current version does this 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)
+1

All Articles