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

capsys fails to capture the output by stdout #2314

Closed
hellock opened this issue Mar 16, 2017 · 1 comment
Closed

capsys fails to capture the output by stdout #2314

hellock opened this issue Mar 16, 2017 · 1 comment
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@hellock
Copy link

hellock commented Mar 16, 2017

Environment:

  • OS: macOS 10.12.3
  • Python version: 2.7.13/3.6.0
  • pytest version: 3.0.7

I find that the outputs by sys.stdout can be captured, but those by stdout will not.

Example:

# a.py
import sys

def func1():
    sys.stdout.write('a')
    sys.stdout.flush()

# b.py
from sys import stdout

def func2():
    stdout.write('a')
    stdout.flush()

# test.py
def test_func1(capsys):
    func1()
    out, err = capsys.readouterr()
    assert out == 'a'  # ok

def test_func2(capsys):
    func2()
    out, err = capsys.readouterr()
    assert out == 'a'  # AssertionError, out is '' other than 'a'
@RonnyPfannschmidt
Copy link
Member

this is expected python behavior, alias by import wont be corrected - we cant search all objects in the active python process for stdout just to be sure

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