pytest日志配置

发布时间 2023-11-20 10:37:22作者: 术科术

pytest默认输出日志如下:

 

为了让日志看起来更加舒服,我们可以在pytest.ini文件中增加如下配置

# 是否开启日志true/false
log_cli = true
# 过滤日式级别
log_cli_level = info
# 输出信息
log_cli_format = %(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)
# 格式化时间
log_cli_date_format = %Y-%m-%d %H:%M:%S

 

输出到指定的日志文件配置如下:

   [pytest]
   log_file_path = /path/to/log/file.log
   addopts = --deselect
   

如果对日志文件有较高的自定义要求,那需要用python的logging库自定义了

 

 注意:如果当前IDE设置的默认编码是UTF-8,那么pytest.ini文件里面不可以加任何注释等与配置无关的信息,否则运行报错

            如果当前IDE设置的默认编码是GBK,那么pytest.ini文件里面可以加注释等信息,运行不会报错