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

output capture broken when sys.stdout passed to print() #2178

Closed
Laharah opened this issue Jan 9, 2017 · 2 comments
Closed

output capture broken when sys.stdout passed to print() #2178

Laharah opened this issue Jan 9, 2017 · 2 comments

Comments

@Laharah
Copy link

Laharah commented Jan 9, 2017

##Environ:
python 3.6, OS X 10.12.2, pytest 3.0.5
##pip list:

pip (9.0.1)
py (1.4.32)
pytest (3.0.5)
setuptools (32.3.1)
wheel (0.29.0)

When the print function is passed file=sys.stdout (which is listed as the default argument in the python docs). Output capture with capsys and capfd fails. capfd works only if the -s flag is passed to py.test

##Example:

# func.py
import sys

def log(msg, file=sys.stdout):
    print('log msg: {}'.format(msg), file=file, flush=True)
###
# test_func.py
import pytest
from func import log

def test_log(capfd):
    log('test')
    out, err = capfd.readouterr()
    assert out == 'log msg: test\n'

I came across it testing a logging decorator that had the option to write to a file. I tried every variation of capfd/capsys.disable() as well to no avail. I couldn't find this issue anywhere else, and a quick test on py2.7 shows the same behavior.

@RonnyPfannschmidt
Copy link
Member

Your module level default prevents the test level capture Form applying, usw None as default and re-obtain stdout in each use

@Laharah
Copy link
Author

Laharah commented Jan 10, 2017

thanks for the heads up, that makes sense.

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

2 participants