简体   繁体   中英

How to capture logbook stdout output with pytest?

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?

Try using capfd instead of capsys .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM