简体   繁体   English

py.test日志控制

[英]py.test logging control

We have recently switched to py.test for python testing (which is fantastic btw). 我们最近切换到py.test进行python测试(这是非常棒的btw)。 However, I'm trying to figure out how to control the log output (ie the built-in python logging module). 但是,我正在试图弄清楚如何控制日志输出(即内置的python日志记录模块)。 We have pytest-capturelog installed and this works as expected and when we want to see logs we can pass --nologcapture option. 我们安装了pytest-capturelog,这可以按预期工作,当我们想查看日志时,我们可以通过--nologcapture选项。

However, how do you control the logging level (eg info, debug etc.) and also filter the logging (if you're only interested in a specific module). 但是,如何控制日志记录级别(例如信息,调试等)并过滤日志记录(如果您只对特定模块感兴趣)。 Is there existing plugins for py.test to achieve this or do we need to roll our own? 是否有py.test的现有插件来实现这一点,还是我们需要自己动手?

Thanks, Jonny 谢谢,强尼

Installing and using the pytest-capturelog plugin could satisfy most of your pytest/logging needs. 安装和使用pytest-capturelog插件可以满足您的大部分pytest / logging需求。 If something is missing you should be able to implement it relatively easily. 如果缺少某些东西,你应该能够相对容易地实现它。

As Holger said you can use pytest-capturelog : 正如Holger所说,你可以使用pytest-capturelog

def test_foo(caplog):
    caplog.setLevel(logging.INFO)
    pass

If you don't want to use pytest-capturelog you can use a stdout StreamHandler in your logging config so pytest will capture the log output. 如果您不想使用pytest-capturelog,可以在日志配置中使用stdout StreamHandler ,这样pytest将捕获日志输出。 Here is an example basicConfig 这是一个basicConfig示例

logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)

A bit of a late contribution, but I can recommend pytest-logging for a simple drop-in logging capture solution. 这是一个晚期的贡献,但我可以推荐pytest-logging用于简单的嵌入式日志记录捕获解决方案。 After pip install pytest-logging you can control the verbosity of the your logs (displayed on screen) with pip install pytest-logging您可以使用控制pip install pytest-logging的详细程度(显示在屏幕上)

$ py.test -s -v tests/your_test.py
$ py.test -s -vv tests/your_test.py
$ py.test -s -vvvv tests/your_test.py

etc... NB - the -s flag is important, without it py.test will filter out all the sys.stderr information. 等等... NB - -s标志很重要,没有它py.test将过滤掉所有sys.stderr信息。

Pytest now has native support for logging control via the caplog fixture; Pytest现在通过caplog fixture caplog支持日志控制; no need for plugins. 不需要插件。

You can specify the logging level for a particular logger or by default for the root logger: 您可以指定特定记录器的日志记录级别,或者默认情况下为根记录器指定日志记录级别:

import pytest

def test_bar(caplog):
    caplog.set_level(logging.CRITICAL, logger='root.baz')

Pytest also captures log output in caplog.records so you can assert logged levels and messages. Pytest还捕获日志输出caplog.records这样你就可以断言记录的水平和消息。 For further information see the official documentation here and here . 有关详细信息,请参阅此处此处的官方文档。

A bit of an even later contribution: you can try pytest-logger . 稍后的贡献:你可以试试pytest-logger Novelty of this plugin is logging to filesystem: pytest provides nodeid for each test item, which can be used to organize test session logs directory (with help of pytest tmpdir facility and it's testcase begin/end hooks). 这个插件的新颖性是记录到文件系统:pytest为每个测试项提供nodeid,可以用来组织测试会话日志目录(借助pytest tmpdir工具和它的testcase开始/结束挂钩)。

You can configure multiple handlers (with levels) for terminal and filesystem separately and provide own cmdline options for filtering loggers/levels to make it work for your specific test environment - eg by default you can log all to filesystem and small fraction to terminal, which can be changed on per-session basis with --log option if needed. 您可以分别为终端和文件系统配置多个处理程序(具有级别),并提供自己的cmdline选项以过滤记录器/级别,使其适用于您的特定测试环境 - 例如,默认情况下,您可以将所有文件系统和小部分记录到终端,如果需要,可以使用--log选项在每个会话的基础上更改。 Plugin does nothing by default, if user defines no hooks. 如果用户没有定义钩子,插件默认不执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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