Pytest日志记录忽略pytest.ini

时间:2018-06-04 09:43:30

标签: python logging pytest

我正在运行的测试:

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

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

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

内部测试我使用记录器如下:

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

testing内(根目录)我有一个文件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在运行测试时能够在命令行中登录。

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

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

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

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

1 个答案:

答案 0 :(得分:4)

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')

This will print:

$ 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 =============================

For one I am wondering what asctime stands for

The logging docs are a bit laconic about it:

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).

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).