How to capture logbook stdout output with pytest?

382 Views Asked by At

I want to test that logging is working but I cannot capture the logs from logbook with pytest.

Here is a simple example that demos the problem:

import sys
import logbook


logbook.StreamHandler(sys.stdout, level=logbook.INFO).push_application()


def use_print():
    print("This is a dummy message using print")


def test_print(capsys):
    use_print()
    out, err = capsys.readouterr()
    assert err == ""
    assert "dummy" in out


def use_logbook():
    log = logbook.Logger("Dummy Log")
    log.info("This is a dummy message using logbook")


def test_logbook(capsys):
    use_logbook()
    out, err = capsys.readouterr()
    assert err == ""
    assert "dummy" in out

Both print and logbook output to stdout, but pytest only captures the output of stdout when using print but not logbook.

How do I get pytest to capture the stdout of logbook?

1

There are 1 best solutions below

0
Michael Francis On

Try using capfd instead of capsys.