Nose/unittest – how write result in separate streams?

108 views
Skip to first unread message

Katerina Romanchuk

unread,
May 24, 2013, 3:01:05 PM5/24/13
to nose-...@googlegroups.com
Hello! Guys, I need your help. I have problem -   unittest/nose sent result in one stream, but I want to split stdout and stderr streams. TextTestRunner - is test runner class that displays results in textual form. It prints out the names of tests as they are run, errors as they occur, and a summary of the results at the end of the test run.

By default stream is stderr:
def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1):

And tests result is look like:
Test 1 ... ok
Test 2 ... FAIL
Test 3 ... ok
Test4 ... FAIL


I can redirect stream in  stdout:

And result is:
Test 1 ... ok
Test 2 ... FAIL
Test 3 ... ok
Test 5 ... FAIL



-------------------------------------------------------------------------------------------------------------------
I want have result look like this:

Test 1 ... ok
Test 2 ... FAIL
Test 3 ... ok
Test 4 ... FAIL


I have read a lot of solution for this problem in internet – but I don’t want to use some complicated decision and overwrite TextTestRunner.
If you know some easy way – please share knowledge with me.

Maybe is some way to do this via fixtures (setup and teardown methods)

-------------------------------------------------------------------------------------------------------------------

import unittest

class Tests(unittest.TestCase):

    def setUp(self):
        pass

    def test_1(self):
        """Test 1"""
        self.assertEquals(1, 1)

    def test_2(self):
        """Test 2"""
        self.assertEquals(1, 2)

    def test_3(self):
        """Test 3"""
        self.assertEquals(1, 1)

    def test_4(self):
        """Test 4"""
        self.assertEquals(1, 1)

    def test_5(self):
        """Test 5"""
        self.assertEquals(1, 7)

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromName('stream' )
    #unittest.TextTestRunner(verbosity=2).run(suite)
    unittest.TextTestRunner(verbosity=2, stream=sys.stdout).run(suite)




Katerina Romanchuk

unread,
Jun 2, 2013, 3:32:52 AM6/2/13
to nose-...@googlegroups.com

I have tried to redirect streams with CustomRunner, but result is wrong - all results write in one stream.

Help me please.

-------------------------------------------------------------------------
import unittest
from StringIO import StringIO
import ctypes
import sys

class CustomRunner(unittest.TextTestRunner):

    def __init__(self, verbosity=2):
        unittest.TextTestRunner.__init__(self, verbosity=verbosity)
        #self.held, sys.stdout = sys.stdout, StringIO()
        #sys.stderr = sys.stdout

        sys.stdout = _DelegateIO(sys.stdout)
        sys.stderr = _DelegateIO(sys.stderr)

        sys.stdout = sys.stdout.delegate
        sys.stderr = sys.stderr.delegate

    def run(self, test):
        r = unittest.TextTestRunner.run(self, test)

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 __getattr__(self, attr):
        return getattr(self._captured, attr)


class Tests(unittest.TestCase):

    def setUp(self):
        pass

    def test_1(self):
        """Test 1"""
        self.assertEquals(1, 1)

    def test_2(self):
        """Test 2"""
        self.assertEquals(1, 2)

    def test_3(self):
        """Test 3"""
        self.assertEquals(1, 1)

    def test_4(self):
        """Test 4"""
        self.assertEquals(1, 1)

    def test_5(self):
        """Test 5"""
        self.assertEquals(1, 7)

if __name__ == '__main__':

    suite = unittest.TestLoader().loadTestsFromName('python_stream')
    CustomRunner().run(suite)
    #unittest.TextTestRunner(verbosity=2, stream=sys.stdout).run(suite)

python_stream.py
Reply all
Reply to author
Forward
0 new messages