从3.3版开始,pytest
支持实时日志记录,这意味着测试中发出的所有日志记录都将立即打印到终端。该功能记录在“实时日志”部分下。默认情况下,实时日志记录处于禁用状态;要启用它,请log_cli = 1
在pytest.ini
配置1中进行设置。实时日志支持向终端和文件发射;相关选项允许记录定制:
终奌站:
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évinBarré在此注释中指出的那样,可以通过命令行选项从命令行覆盖ini-o/--override
选项。因此,您无需调用log_cli
in pytest.ini
,而只需声明:
$ pytest -o log_cli=true ...
例子
用于演示的简单测试文件:
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
级别,高级输出
配置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
文件中显示奇特的输出
配置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尽管您可以pytest
在部分中setup.cfg
进行配置[tool:pytest]
,但是当您要提供自定义实时日志记录格式时,请不要尝试这样做。其他工具读取setup.cfg
可能会将诸如%(message)s
字符串插值之类的内容视为失败。使用pytest.ini
以避免错误。