Since version 3.3, pytest
supports live logging, meaning that all the log records emitted in tests will be printed to the terminal immediately. The feature is documented under Live Logs https://docs.pytest.org/en/latest/how-to/logging.html#live-logs section. Live logging is disabled by default; to enable it, set log_cli = 1
in the pyproject.toml
1 or pytest.ini
2 config. Live logging supports emitting to terminal and file; the relevant options allow records customizing:
终端:
log_cli_level
log_cli_format
log_cli_date_format
file:
log_file
log_file_level
log_file_format
log_file_date_format
正如所指出的凯文·巴雷 https://stackoverflow.com/users/7505103/k%c3%a9vin-barr%c3%a9 in 这条评论 https://stackoverflow.com/questions/4673373/logging-within-py-test-tests/51633600?noredirect=1#comment91175953_51633600, 压倒一切ini
命令行选项可以通过以下方式完成:
-o OVERRIDE_INI, --override-ini=OVERRIDE_INI
使用“option=value”样式覆盖 ini 选项,例如
-o xfail_strict=True -o cache_dir=cache
所以而不是声明log_cli
in pytest.ini
,您可以简单地调用:
$ pytest -o log_cli=true ...
Examples
用于演示的简单测试文件:
# 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
file
配置在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
supported since version 6.0 and is the best option IMO. See PEP 518 https://www.python.org/dev/peps/pep-0517/ for the specs.
2 Although you can also configure pytest
in setup.cfg
under the [tool:pytest]
section, don't be tempted to do that when you want to provide custom live logging format. Other tools reading setup.cfg
might treat stuff like %(message)s
as string interpolation and fail. The best choice is using pyproject.toml
anyway, but if you are forced to use the legacy ini-style format, stick to pytest.ini
to avoid errors.