Pytest日志记录会忽略pytest.ini中的选项 [英] Pytest logging ignores options in pytest.ini

查看:547
本文介绍了Pytest日志记录会忽略pytest.ini中的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个测试:

pytest --capture=no --verbose --rootdir=testing/ testing/tests/docker_test.py

来自/home/user/development/

.该测试检查某些容器是否正在运行,并使用默认的Python 3.6日志记录框架.测试文件中的记录器的配置如下:

from /home/user/development/. The test checks if some containers are running and uses the default logging framework of Python 3.6. The logger inside the test file is configured as follows:

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logging.basicConfig(level=logging.INFO, stream=sys.stdout, format="%(asctime)s %(levelname)s %(message)s")

内部测试中,我正在使用记录器,如下所示:

Inside tests I am using the logger as follows:

logger.info(f"TEST SUCCESSFUL: container {container_name} is running")
logger.info(f"TEST SUCCESSFUL: all required containers are running")

testing(根目录)内部,我有一个文件pytest.ini:

Inside testing (the root directory) I have a file pytest.ini:

[pytest]
log_level = INFO
log_cli_level = INFO
log_format = %(asctime)s %(levelname)s %(message)s
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %H:%M:%S
log_cli_date_format = %H:%M:%S

基本上,我不希望时间戳记中包含任何日期,并且我希望pytest在运行测试时实时登录到命令行.

Basically I do not want any date to be in the timestamp and I want pytest to log live to the command line when I am running the tests.

我想知道asctime代表什么.看起来像"ascii时间".我不需要标准化的时间戳,而是要在pytest.ini中描述的格式.这就是为什么我也尝试使用datedatetimetimestamp而不是asctime的所有原因,导致所有错误.因此,我想asctime是获取时间戳的唯一方法.

For one I am wondering what asctime stands for. It looks like "ascii time". I do not want a standardized timestamp, but instead the format I describe in the pytest.ini. That is why I also tried to use date, datetime and timestamp, instead of asctime, all resulting in an error. So I guess asctime is the only way to get a timestamp.

但是,pytest似乎忽略了我在pytest.ini文件中设置的所有选项,尽管这表明在我运行测试时它找到了该文件:

However, pytest seems to ignore all the options I am setting in my pytest.ini file, although is indicates, that it found the file, when I am running the tests:

cachedir: testing/.pytest_cache
rootdir: /home/user/development/testing, inifile: pytest.ini

如何更改pytest日志记录中的时间戳?

How can I change the timestamp in pytest logging?

推荐答案

我想您缺少的是pytest.ini中的log_cli = 1(或true/yes/etc).除此之外,通过配置为您提供的日志记录将以您在log_cli_format中指定的格式打印.您甚至可以将pytest.ini减少为:

I guess what you are missing is log_cli = 1 (or true/yes/etc) in your pytest.ini. Besides that, with the config you're provided the log records are printed in the format you specified in log_cli_format. You can even reduce the pytest.ini to:

[pytest]
log_cli = 1
log_cli_level = INFO
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_date_format = %H:%M:%S

此外,以上配置将在测试会话中处理根记录器配置,因此您无需在测试中配置记录器以进行实时记录.只需在测试中调用记录器即可:

Also, the above config will take care of the root logger config in test session, so you shouldn't need to configure the logger in tests for live logging. Just call the logger in tests:

import logging

def test_spam():
    logger = logging.getLogger(__name__)
    logger.info('spam')
    logger.warning('eggs')
    logger.error('bacon')

这将打印:

$ pytest
============================== test session starts ================================
platform linux -- Python 3.6.5, pytest-3.4.1, py-1.5.3, pluggy-0.6.0 -- /data/gentoo64/usr/bin/python3.6
cachedir: .pytest_cache
rootdir: /data/gentoo64/home/u0_a82/projects/stackoverflow/so-50677656, inifile: pytest.ini
plugins: mock-1.6.3, cov-2.5.1, flaky-3.4.0
collected 1 item

testing/tests/test_docker.py::test_logs
---------------------------------- live log call ----------------------------------
16:29:12 INFO spam
16:29:13 WARNING eggs
16:29:13 ERROR bacon
PASSED                                                                       [100%]
============================ 1 passed in 1.08 seconds =============================

我想知道asctime代表什么

For one I am wondering what asctime stands for

记录文档有点太简单了:

创建LogRecord的人类可读时间.默认情况下,格式为"2003-07-08 16:49:45,896"(逗号后的数字是时间的毫秒部分).

Human-readable time when the LogRecord was created. By default this is of the form ‘2003-07-08 16:49:45,896’ (the numbers after the comma are millisecond portion of the time).

但是,asctime并不意味着将始终使用

However, asctime does not mean that the records will be always formatted using time.asctime - it's only the default datetime format used when you don't pass your own to the logging.Formatter (second argument in the formatter constructor).

这篇关于Pytest日志记录会忽略pytest.ini中的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆