pytest logging规范日志输出

自动化测试没有log输出,等于没有进行测试,因为即使发现问题,也没有办法知道是哪里有问题。
pytest在3.3版本开始,就支持了log输出功能,不需要另外下载插件。

通过命令pytest --help,可以看到logging设置的基本介绍:
–no-print-logs disable printing caught logs on failed tests.
–log-level=LOG_LEVEL
logging level used by the logging module
–log-format=LOG_FORMAT
log format as used by the logging module.
–log-date-format=LOG_DATE_FORMAT
log date format as used by the logging module.
–log-cli-level=LOG_CLI_LEVEL
cli logging level.
–log-cli-format=LOG_CLI_FORMAT
log format as used by the logging module.
–log-cli-date-format=LOG_CLI_DATE_FORMAT
log date format as used by the logging module.
–log-file=LOG_FILE path to a file when logging will be written to.
–log-file-level=LOG_FILE_LEVEL
log file logging level.
–log-file-format=LOG_FILE_FORMAT
log format as used by the logging module.
–log-file-date-format=LOG_FILE_DATE_FORMAT
log date format as used by the logging module.

这里不再讲解,直接上例子。
pytest.ini中添加如下代码:

[pytest]
log_cli = true
log_cli_level = DEBUG
log_format = %(asctime)s (%(filename)-16s:%(lineno)-3s) %(levelname)-8s %(message)s
log_date_format = %Y-%M-%D %H:%M:%S

在测试python文件如:test_logging.py中添加如下代码:
import pytest
import logging

log = logging.getLogger(name)

def test_logger():
log.info(“info massage!”)
log.debug(“debug massage!”)
log.error(“error message!”)
log.critical(“critical message!”)
log.warning(“warning message!”)
print(“print message!”)

命令行中运行命令:pytest test_logging.py ,运行结果如下:
在这里插入图片描述
你get到了吗?如果有疑问,欢迎随时联系我!

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页