How to use logging, pytest fixture and capsys?

后端 未结 3 589
灰色年华
灰色年华 2021-01-04 19:14

I am trying to unit-test some algorithm that uses logging library.

I have a fixture that creates a logger.

In my 1st test case, I do not use

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-04 19:55

    As of pytest 3.3, the functionality of capturing log message was added to pytest core. This is supported by the caplog fixture:

    def test_baz(caplog):
        func_under_test()
        for record in caplog.records:
            assert record.levelname != 'CRITICAL'
        assert 'wally' not in caplog.text
    

    More information can be found at the documentation

提交回复
热议问题