Skip to content

Pytest/capsys fixture not capturing stdout #1132

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

Closed
derwolfe opened this issue Oct 12, 2015 · 4 comments
Closed

Pytest/capsys fixture not capturing stdout #1132

derwolfe opened this issue Oct 12, 2015 · 4 comments

Comments

@derwolfe
Copy link

Py.test isn't capturing the output from stdout or stderr.

import sys

stdout = sys.stdout

def test_use_logger(capsys):
    stdout.write("called")
    out, err = capsys.readouterr()
    assert "called" in out

The test case was run and reproduced on OS X 10.11 with pythons 2.7.10 and 3.5.0.

The versions used are:

py==1.4.30
pytest==2.8.2

Thanks!

@derwolfe derwolfe changed the title Pytest fixture not capturing stdout Pytest/capsys fixture not capturing stdout Oct 12, 2015
@derwolfe
Copy link
Author

Also, calling sys.stdout.write and sys.stderr.write directly do seem to work correctly (that is, their output is captured).

@RonnyPfannschmidt
Copy link
Member

capsys will replace the actual object instead of the underlying file descriptor
making a reference to the prior object will of course replace the stream

a solution is in planning, but will at best it 3.0

@The-Compiler
Copy link
Member

As @RonnyPfannschmidt said, you're "saving" sys.stdout before pytest can replace it with the capturing one. The solution pretty much boils down to "don't do that" 😉

Can this be closed?

@derwolfe
Copy link
Author

@RonnyPfannschmidt thanks for your response. Yes, this can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants