我正在试图找出如何在我的doctest中忽略structlog的日志输出。pytest似乎能够忽略日志记录中的日志,但不能忽略structlog (至少本机不这样)。
一个例子如下:
import logging
import structlog
LOGGING_LOGGER = logging.getLogger(__name__)
STRUCTLOG_LOGGER = structlog.getLogger(__name__)
def func_with_logging():
"""
>>> func_with_logging()
1
"""
LOGGING_LOGGER.debug("ABC")
return 1
def func_with_structlog():
"""
>>> func_with_structlog()
1
"""
STRUCTLOG_LOGGER.debug("ABC")
return 1以上只会导致structlog文档测试失败。
========================================= test session starts =========================================
platform linux -- Python 3.9.4, pytest-7.1.2, pluggy-1.0.0
rootdir: /home/bbeattie/test
collected 2 items
myfile.py .F [100%]
============================================== FAILURES ===============================================
________________________________ [doctest] myfile.func_with_structlog _________________________________
019
020 >>> func_with_structlog()
Expected:
1
Got:
2022-05-07 18:32.13 [debug ] ABC
1
/home/bbeattie/test/myfile.py:20: DocTestFailure
======================================= short test summary info =======================================
FAILED myfile.py::myfile.func_with_structlog
===================================== 1 failed, 1 passed in 0.03s =====================================我是不是漏掉了一些明显的火药呢?
发布于 2022-05-08 15:37:43
您可以将structlog配置为不登录到stdout,也可以使用标准库日志将其配置为日志,以便pytest的日志助手工作。
https://www.structlog.org/en/stable/testing.html中的自动夹具应该是实现第一种方法的最简单方法,而对于后者,“structlog中的呈现”方法应该有效。添加您自己的自动安装夹具,为其配置structlog (会话范围应该很好,不需要为每个测试重新配置)。
https://stackoverflow.com/questions/72157482
复制相似问题