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

1.pytest_runtest_makereport

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

from _pytest import runner

# 对应源码
def pytest_runtest_makereport(item, call):
    """ return a :py:class:`_pytest.runner.TestReport` object
    for 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 = yield
    print('用例执行结果', 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.py

import 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._sessionstarttime
    print('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) - 云+社区 - 腾讯云

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值