Skip to content

Using capsys in fixtures: AttributeError: 'CaptureFixture' object has no attribute '_capture' #2709

Closed
@hroncok

Description

@hroncok

Recently, I had a workshop about parametric tests and fixtures in pytest. As one of the examples I tried to use the capsys fixture inside my own and failed. I wonder if it's a bug or documented behavior I couldn't find.

First, working code

def test_print(capsys):
    print(123, 456, 789)
    out, err = capsys.readouterr()
    assert not err.strip()
    assert '5' in out
$ python -m pytest demo.py 
... 1 passed in 0.01 seconds ...

Broken code

Now I don't like multiple assertions in one test, so let's create a fixture and split it:

import pytest

OUT = 0
ERR = 1


@pytest.fixture
def captured_print(capsys):
    print(123, 456, 789)
    out, err = capsys.readouterr()
    return out.strip(), err.strip()


def test_print_no_stderr(captured_print):
    assert not captured_print[ERR]


def test_print_stdout_contains_5(captured_print):
    assert '5' in captured_print[OUT]

I think this should work. At least, it feels like a natural way of doing it. But it blows up:

$ python -m pytest demo.py 
... AttributeError: 'CaptureFixture' object has no attribute '_capture' ...
Full output
============================= test session starts ==============================
platform linux -- Python 3.6.2, pytest-3.2.2.dev17+g539523cf, py-1.4.34, pluggy-0.4.0
rootdir: /home/churchyard/Dokumenty/pytest, inifile: tox.ini
collected 2 items                                                               

demo.py EE

==================================== ERRORS ====================================
____________________ ERROR at setup of test_print_no_stderr ____________________

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

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

../_pytest/capture.py:208: AttributeError

During handling of the above exception, another exception occurred:

capsys = <_pytest.capture.CaptureFixture object at 0x7fe560f30c50>

    @pytest.fixture
    def captured_print(capsys):
        print(123, 456, 789)
>       out, err = capsys.readouterr()

demo.py:10: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

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

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

../_pytest/capture.py:210: AttributeError
---------------------------- Captured stdout setup -----------------------------
123 456 789
________________ ERROR at setup of test_print_stdout_contains_5 ________________

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

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

../_pytest/capture.py:208: AttributeError

During handling of the above exception, another exception occurred:

capsys = <_pytest.capture.CaptureFixture object at 0x7fe560f6a358>

    @pytest.fixture
    def captured_print(capsys):
        print(123, 456, 789)
>       out, err = capsys.readouterr()

demo.py:10: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

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

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

../_pytest/capture.py:210: AttributeError
---------------------------- Captured stdout setup -----------------------------
123 456 789
=========================== 2 error in 0.06 seconds ============================

System information

$ python --version
Python 3.6.2

$ pip freeze
py==1.4.34
-e git://github.com/pytest-dev/pytest.git@539523cfee4c49a765569abcf68134b1255eedb5#egg=pytest

# also happens on 3.2.1

$ uname
Linux

$ uname -m
x86_64

$ head -n2 /etc/os-release 
NAME=Fedora
VERSION="26 (Twenty Six)"

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: capturerelated to the capture builtin pluginstatus: help wanteddevelopers would like help from experts on this topictype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions