Pytest: unittest.TestCase / Integration of fixtures fails for capsys/capfd

Created on 15 Jun 2017  路  3Comments  路  Source: pytest-dev/pytest

Hi and thanks for your great work devs!

When I try to integrate capsys or capfd fixtures with unittest.TestCase it throws 'CaptureFixture' object has no attribute '_outerr' error.

Here is my test code:

import pytest
import unittest

class TestClass(unittest.TestCase):

    @pytest.fixture(autouse=True)
    def test_print(self, capfd):
        print('x')
        out, err = capfd.readouterr()
        assert out == 'x'

It is essentially combined use of Support for unittest.TestCase / Integration of fixtures and Capturing of the stdout/stderr output.

Here is the full backtrace:

_____________ ERROR at setup of TestClass.test_print ______________

self = <_pytest.capture.CaptureFixture object at 0x7f8f6ccff6d8>

    def readouterr(self):
        try:
>           return self._capture.readouterr()
E           AttributeError: 'CaptureFixture' object has no attribute '_capture'

/usr/local/lib/python3.5/dist-packages/_pytest/capture.py:206: AttributeError

During handling of the above exception, another exception occurred:

self = <test_print.TestClass testMethod=test_print>, capfd = <_pytest.capture.CaptureFixture object at 0x7f8f6ccff6d8>

    @pytest.fixture(autouse=True)
    def test_print(self, capfd):
        print('x')
>       out, err = capfd.readouterr()

test_print.py:9: 
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <_pytest.capture.CaptureFixture object at 0x7f8f6ccff6d8>

    def readouterr(self):
        try:
            return self._capture.readouterr()
        except AttributeError:
>           return self._outerr
E           AttributeError: 'CaptureFixture' object has no attribute '_outerr'

/usr/local/lib/python3.5/dist-packages/_pytest/capture.py:208: AttributeError

I tested in with newest pytset on Ubuntu 17.04:
This is pytest version 3.1.2, imported from /usr/local/lib/python3.5/dist-packages/pytest.py
This is pytest version 3.1.2, imported from /usr/local/lib/python2.7/dist-packages/pytest.pyc

Do I correctly combine built-in fixtures with unittest.TestCase? Are you planning to support this test case?

Most helpful comment

Thank you for explanation. Temporarily I ended up using following trick:

import pytest
import unittest

class TestClass(unittest.TestCase):

    def test_print(self):
        print('x', end='')
        out, err = self.capfd.readouterr()
        assert out == 'x'

    @pytest.fixture(autouse=True)
    def capfd(self, capfd):
        self.capfd = capfd

All 3 comments

It seems that I misinterpreted what @pytest.fixture(autouse=True) can do. Sorry for that. Still I do not know how to achieve desired effect but I will close this for now to avoid confusion.

Fixtures aren't supported in unittest.TestCase subclasses - if you want to use pytest features, use classes without any inheritance (or inheriting object on Python 2).

Thank you for explanation. Temporarily I ended up using following trick:

import pytest
import unittest

class TestClass(unittest.TestCase):

    def test_print(self):
        print('x', end='')
        out, err = self.capfd.readouterr()
        assert out == 'x'

    @pytest.fixture(autouse=True)
    def capfd(self, capfd):
        self.capfd = capfd
Was this page helpful?
0 / 5 - 0 ratings