Python 2.7 Unit test: Assert logger warning thrown

前端 未结 2 1458
逝去的感伤
逝去的感伤 2021-01-08 00:41

I\'m trying to write a Unit Test for a piece of python code that raises a warning via logger.warn(\'...\') under certain conditions. How do I assert that this w

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-08 01:11

    In your unit test setup, add a logging handler which buffers records, and remove it during teardown. You can use as a basis a couple of utility classes, TestHandler and Matcher, which are part of the Python test infrastructure. (The link is to Python's default branch, but the classes should be usable in other Python versions). For information on how to use these classes, see this post.

提交回复
热议问题