首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何忽略在doctest上的pytest中生成的structlog调试/info/etc?

如何忽略在doctest上的pytest中生成的structlog调试/info/etc?
EN

Stack Overflow用户
提问于 2022-05-08 01:36:58
回答 1查看 78关注 0票数 0

我正在试图找出如何在我的doctest中忽略structlog的日志输出。pytest似乎能够忽略日志记录中的日志,但不能忽略structlog (至少本机不这样)。

一个例子如下:

代码语言:javascript
复制
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文档测试失败。

代码语言:javascript
复制
========================================= 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 =====================================

我是不是漏掉了一些明显的火药呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-08 15:37:43

您可以将structlog配置为不登录到stdout,也可以使用标准库日志将其配置为日志,以便pytest的日志助手工作。

https://www.structlog.org/en/stable/testing.html中的自动夹具应该是实现第一种方法的最简单方法,而对于后者,“structlog中的呈现”方法应该有效。添加您自己的自动安装夹具,为其配置structlog (会话范围应该很好,不需要为每个测试重新配置)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72157482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档