Capture STDOUT and STDERR - capsys



examples/capture/app.py
import sys

def greet(to_out, to_err=None):
    print(to_out)
    if to_err:
        print(to_err, file=sys.stderr)

examples/capture/test_capture.py
import app

def test_myoutput(capsys):
    app.greet("hello", "world")
    out, err = capsys.readouterr()
    assert out == "hello\n"
    assert err == "world\n"

    app.greet("next")
    out, err = capsys.readouterr()
    assert out == "next\n"
    assert err == ""