Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get capfd of run in fixture #4663

Closed
gaborbernat opened this issue Jan 22, 2019 · 2 comments
Closed

get capfd of run in fixture #4663

gaborbernat opened this issue Jan 22, 2019 · 2 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@gaborbernat
Copy link
Contributor

During tox-dev/tox#1145 (comment) it came up that pytest injects different instance for capfd on the fixture and on test function. Any way to get the call/run capfd inside a fixture? (use case: the fixture provides a helper function that automatically collects output during the run, and ideally would like to avoid the need for the user to pass it through from the call site). Thanks!

@nicoddemus nicoddemus added the type: question general question, might be closed after 2 weeks of inactivity label Jan 22, 2019
@nicoddemus
Copy link
Member

Hi @gaborbernat!

I'm short on time so I didn't follow the discussion in tox-dev/tox#1145, but the short example below shows that you get the same capfd instance in fixtures and test functions:

import pytest

@pytest.fixture
def fix(capfd):
    print('setup')
    yield
    print('teardown')
    assert 0, repr(capfd.readouterr().out)

def test_foo(fix, capfd):
    print('call')

This prints:

________________________ ERROR at teardown of test_foo ________________________

capfd = <_pytest.capture.CaptureFixture object at 0x044E4790>

    @pytest.fixture
    def fix(capfd):
        print('setup')
        yield
        print('teardown')
>       assert 0, repr(capfd.readouterr().out)
E       AssertionError: u'setup\ncall\nteardown\n'
E       assert 0

.tmp\test-foo.py:8: AssertionError
---------------------------- Captured stdout setup ----------------------------
setup
---------------------------- Captured stdout call -----------------------------
call

Also, I've made sure both capfd are the same instance by printing their id() (not shown here).

Can you please clarify what you mean, perhaps with a short example? Thanks! 🙇

@gaborbernat
Copy link
Contributor Author

Indeed, this works as presented above with latest, maybe it was some bug in some older version. Anyway can no longer replicate, so thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

2 participants