从 3.3 版本开始,pytest
支持实时日志记录,这意味着测试中发出的所有日志记录都将立即打印到终端。该功能记录在实时日志部分。默认情况下禁用实时日志记录;要启用它,log_cli = 1
请在pyproject.toml
1或pytest.ini
2配置中设置。实时日志支持发送到终端和文件;相关选项允许自定义记录:
终端:
log_cli_level
log_cli_format
log_cli_date_format
文件:
log_file
log_file_level
log_file_format
log_file_date_format
注意:log_cli
标志不能从命令行传递,必须在pytest.ini
. 所有其他选项都可以从命令行传递或在配置文件中设置。正如Kévin Barré在此评论中指出的那样,可以通过该选项从命令行覆盖 ini-o/--override
选项。因此,您可以简单地调用,而不是声明log_cli
in pytest.ini
:
$ pytest -o log_cli=true ...
例子
用于演示的简单测试文件:
# test_spam.py
import logging
LOGGER = logging.getLogger(__name__)
def test_eggs():
LOGGER.info('eggs info')
LOGGER.warning('eggs warning')
LOGGER.error('eggs error')
LOGGER.critical('eggs critical')
assert True
如您所见,不需要额外的配置;将根据在命令行中指定或从命令行传递的pytest
选项自动设置记录器。pytest.ini
实时记录到终端、INFO
级别、精美的输出
中的配置pyproject.toml
:
[tool.pytest.ini_options]
log_cli = true
log_cli_level = "INFO"
log_cli_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)"
log_cli_date_format = "%Y-%m-%d %H:%M:%S"
legacy 中的相同配置pytest.ini
:
[pytest]
log_cli = 1
log_cli_level = INFO
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_cli_date_format=%Y-%m-%d %H:%M:%S
运行测试:
$ pytest test_spam.py
=============================== test session starts ================================
platform darwin -- Python 3.6.4, pytest-3.7.0, py-1.5.3, pluggy-0.7.1 -- /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/hoefling/projects/private/stackoverflow/so-4673373, inifile: pytest.ini
collected 1 item
test_spam.py::test_eggs
---------------------------------- live log call -----------------------------------
2018-08-01 14:33:20 [ INFO] eggs info (test_spam.py:7)
2018-08-01 14:33:20 [ WARNING] eggs warning (test_spam.py:8)
2018-08-01 14:33:20 [ ERROR] eggs error (test_spam.py:9)
2018-08-01 14:33:20 [CRITICAL] eggs critical (test_spam.py:10)
PASSED [100%]
============================= 1 passed in 0.01 seconds =============================
实时记录到终端和文件,终端中只有消息和级别,文件CRITICAL
中的精美输出pytest.log
中的配置pyproject.toml
:
[tool.pytest.ini_options]
log_cli = true
log_cli_level = "CRITICAL"
log_cli_format = "%(message)s"
log_file = "pytest.log"
log_file_level = "DEBUG"
log_file_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)"
log_file_date_format = "%Y-%m-%d %H:%M:%S"
legacy 中的相同配置pytest.ini
:
[pytest]
log_cli = 1
log_cli_level = CRITICAL
log_cli_format = %(message)s
log_file = pytest.log
log_file_level = DEBUG
log_file_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_file_date_format=%Y-%m-%d %H:%M:%S
测试运行:
$ pytest test_spam.py
=============================== test session starts ================================
platform darwin -- Python 3.6.4, pytest-3.7.0, py-1.5.3, pluggy-0.7.1 -- /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/hoefling/projects/private/stackoverflow/so-4673373, inifile: pytest.ini
collected 1 item
test_spam.py::test_eggs
---------------------------------- live log call -----------------------------------
eggs critical
PASSED [100%]
============================= 1 passed in 0.01 seconds =============================
$ cat pytest.log
2018-08-01 14:38:09 [ INFO] eggs info (test_spam.py:7)
2018-08-01 14:38:09 [ WARNING] eggs warning (test_spam.py:8)
2018-08-01 14:38:09 [ ERROR] eggs error (test_spam.py:9)
2018-08-01 14:38:09 [CRITICAL] eggs critical (test_spam.py:10)
1 pyproject.toml
自 6.0 版起支持,是 IMO 的最佳选择。有关规格,请参阅PEP 518。
2虽然您也可以pytest
在setup.cfg
该[tool:pytest]
部分下进行配置,但当您想提供自定义实时日志记录格式时,不要试图这样做。其他阅读工具setup.cfg
可能会将诸如字符串插值之类的东西%(message)s
视为失败。最好的选择是pyproject.toml
无论如何都要使用,但如果你被迫使用传统的 ini 样式格式,请坚持pytest.ini
以避免错误。