pytest-result-log

pytest-result-log 是一个 pytest 插件, 可以将用例的执行结果及错误信息记录到日志中。

假设 pytest.ini 有如下配置:

;pytest.ini

[pytest]

log_file = ./.logs/test.log
log_file_level = info
log_file_format   = %(levelname)-8s %(asctime)s : %(message)s
log_file_date_format  = %H:%M:%S

result_log_level_verbose = info
result_log_level_separator = warning

pytest 执行用例后会自动生成日志文件,下面内容片段:

...(省略更多)
WARNING  16:18:57 : ---------Start: tests/usefixtures/test_mark_usefixtures.yaml::请求fixture---------
INFO     16:18:57 : test status is PASSED (tests/usefixtures/test_mark_usefixtures.yaml::请求fixture):
WARNING  16:18:57 : ----------End: tests/usefixtures/test_mark_usefixtures.yaml::请求fixture----------
WARNING  16:18:57 : ----------------------Start: tests/test_base.yaml::访问百度首页-----------------------
ERROR    16:18:57 : test status is FAILED (tests/test_base.yaml::访问百度首页): Please review verbose content
INFO     16:18:57 : tests/test_base.yaml::访问百度首页 -> :
Step No: 1
Step Content:
--------------------
response:
  status_code: 200
  text: '*baidu*'
--------------------

C:\demo\pytest-yaml-demo\conftest.py:24: AttributeError
'YamlItem' object has no attribute 'resp'
WARNING  16:18:57 : -----------------------End: tests/test_base.yaml::访问百度首页------------------------
...(省略更多)

从结果来看,yaml用例的执行时间、结果、失败原因,以及引发错误的step片段, 均可自动被插件识别,并写入日志。