从3.3版开始,pytest支持实时日志记录,这意味着测试中发出的所有日志记录都将立即打印到终端。该特性在Live Logs部分中有文档说明。默认情况下禁用实时日志记录;要启用它,请在pyproject中设置log_cli = 1。Toml1或pytest。ini2配置。实时日志记录支持发送到终端和文件;相关选项允许自定义记录:
终端:
log_cli_level
log_cli_format
log_cli_date_format
文件:
log_file
log_file_level
log_file_format
log_file_date_format
正如Kévin Barré在这条评论中指出的那样,从命令行覆盖ini选项可以通过以下方式完成:
-o OVERRIDE_INI,——OVERRIDE_INI =OVERRIDE_INI
用"option=value"样式重写ini选项。
-o xfail_strict=True -o cache_dir=cache
因此,在pytest.ini中不用声明log_cli,你可以简单地调用:
$ 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"
在遗留的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"
在遗留的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。
虽然你也可以在setup.cfg的[tool:pytest]部分中配置pytest,但是当你想要提供自定义的实时日志格式时,不要尝试这样做。其他读取setup.cfg的工具可能会把%(message)s这样的东西当作字符串插值而失败。最好的选择是使用pyproject。Toml,但是如果你被迫使用传统的ini格式,坚持使用pytest.ini以避免错误。