Writing a pytest function for checking the output on console (stdout)

Viewed 22108

This link gives a description how to use pytest for capturing console outputs. I tried on this following simple code, but I get error

import sys
import pytest
def f(name):
    print "hello "+ name

def test_add(capsys):
    f("Tom")
    out,err=capsys.readouterr()
    assert out=="hello Tom"


test_add(sys.stdout)

Output:

python test_pytest.py 
hello Tom
Traceback (most recent call last):
  File "test_pytest.py", line 12, in <module>
    test_add(sys.stdout)
  File "test_pytest.py", line 8, in test_add
    out,err=capsys.readouterr()
AttributeError: 'file' object has no attribute 'readouterr'

what is wrong and what fix needed? thank you

EDIT: As per the comment, I changed capfd, but I still get the same error

import sys
import pytest
def f(name):
    print "hello "+ name

def test_add(capfd):
    f("Tom")
    out,err=capfd.readouterr()
    assert out=="hello Tom"


test_add(sys.stdout)
2 Answers
Related