6

I am trying to use py.tests capsys fixture to capture the standard error stream. However, this doesn't seem to work as advertised. Given this simple test:

from sys import stderr def test_capsys(capsys): print('foo') print('bar', file=stderr) out, err = capsys.readouterr() assert out == 'foo\n' assert err == 'bar\n' 

Produces the following output when run with py.test 2.7.0 on Python 3.4.3:

 def test_capsys(capsys): print('foo') print('bar', file=stderr) out, err = capsys.readouterr() assert out == 'foo\n' > assert err == 'bar\n' E assert '' == 'bar\n' E + bar test_capsys.py:10: AssertionError ----------------------------- Captured stderr call ----------------------------- bar 

The strange thing is that py.test reports the correct contents for the error stream, but capsys doesn't seem to capture it. Am I doing something wrong? Is this a bug?

1 Answer 1

7

The capsys fixture works by replacing sys.stderr with it's own virtual file object. In the code given above, this replacement happens only after the test imports sys.stderr, making it useless. To fix this problem, one can import sys.stderr inside the test.

def test_capsys(capsys): from sys import stderr print('foo') print('bar', file=stderr) out, err = capsys.readouterr() assert out == 'foo\n' assert err == 'bar\n' 

This problem is better described in the documentation of the unittest.mock module.

Sign up to request clarification or add additional context in comments.

1 Comment

Just to add to the answer, and alternative would be to import sys and use sys.stderr in your code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.