当前位置: 代码迷 >> 综合 >> pytest(3): 收集测试结果pytest_runtest_makereport
  详细解决方案

pytest(3): 收集测试结果pytest_runtest_makereport

热度:16   发布时间:2023-11-21 17:40:26.0

1.pytest_runtest_makereport

先看下相关的源码,在_pytest/runner.py下,可以导入之后,点进去查看

from _pytest import runner# 对应源码
def pytest_runtest_makereport(item, call):""" return a :py:class:`_pytest.runner.TestReport` objectfor the given :py:class:`pytest.Item` and:py:class:`_pytest.runner.CallInfo`."""

这里item是测试用例,call是测试步骤,具体执行过程如下:

  • 先执行when='setup' 返回setup 的执行结果
  • 然后执行when='call' 返回call 的执行结果
  • 最后执行when='teardown'返回teardown 的执行结果

pytest  收集测试结果

# conftest.py 
import pytest@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):print('------------------------------------')# 获取钩子方法的调用结果out = yieldprint('用例执行结果', out)# 3. 从钩子方法的调用结果中获取测试报告report = out.get_result()print('测试报告:%s' % report)print('步骤:%s' % report.when)print('nodeid:%s' % report.nodeid)print('description:%s' % str(item.function.__doc__))print(('运行结果: %s' % report.outcome))

输出:

--------------pytest_runtest_makereport----------------------
用例执行结果 <pluggy.callers._Result object at 0x000001CCA355DCD0>
测试报告:<TestReport 'test_demo1.py::test_01[5-6-7]' when='call' outcome='passed'>
步骤:call
nodeid:test_demo1.py::test_01[5-6-7]
description:None
运行结果: passed

2.pytest_terminal_summary

#conftest.pyimport time
from _pytest import terminal
# 上海-悠悠def pytest_terminal_summary(terminalreporter, exitstatus, config):'''收集测试结果'''print("===============pytest_terminal_summary===================")print(terminalreporter.stats)print("total:", terminalreporter._numcollected)print('passed:', len(terminalreporter.stats.get('passed', [])))print('failed:', len(terminalreporter.stats.get('failed', [])))print('error:', len(terminalreporter.stats.get('error', [])))print('skipped:', len(terminalreporter.stats.get('skipped', [])))# terminalreporter._sessionstarttime 会话开始时间duration = time.time() - terminalreporter._sessionstarttimeprint('total times:', duration, 'seconds')

输出:

===============pytest_terminal_summary===================
{'warnings': [WarningReport(message="C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pyreadline\\py3k_compat.py:8: DeprecationWarning: Using or importing the ABCs fro
m 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working\n  return isinstance(x, collections.Callable)\n", no
deid='', fslocation=('C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pyreadline\\py3k_compat.py', 8))], 'error': [<TestReport 'test_demo1.py::test_01[1-2-3]' when='s
etup' outcome='failed'>, <TestReport 'test_demo1.py::test_01[5-6-7]' when='setup' outcome='failed'>, <TestReport 'test_demo1.py::test_01[r-t-u]' when='setup' outcome=
'failed'>], '': [<TestReport 'test_demo1.py::test_01[1-2-3]' when='teardown' outcome='passed'>, <TestReport 'test_demo1.py::test_01[5-6-7]' when='teardown' outcome='p
assed'>, <TestReport 'test_demo1.py::test_01[r-t-u]' when='teardown' outcome='passed'>]}
total: 3
passed: 0
failed: 0
error: 3
skipped: 0
total times: 0.07572793960571289 seconds

参考:

API Reference — pytest documentation (官方文档)

pytest文档33-Hooks函数获取用例执行结果(pytest_runtest_makereport) - 云+社区 - 腾讯云