其他分享
首页 > 其他分享> > pytest常用hook函数

pytest常用hook函数

作者:互联网

pytest_runtest_makereport

说明:收集每个用例三个阶段的执行结果

  1. 先执行when='setup' 返回前置的执行结果
  2. 然后执行when='call' 返回用例步骤的执行结果
  3. 最后执行when='teardown'返回后置的执行结果

参数:

# 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

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

pytest_collection_modifyitems

说明:用于收集测试用例,且在测试用例收集完毕之后被调用。

参数:

def pytest_collection_modifyitems(items):
    """
    用于收集测试用例
    :return:
    """
    # 测试用例收集完成时,将收集到的item的name和nodeid的中文显示在控制台上
    for item in items:
        print(item.name)
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        print(item.nodeid)
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")
    # 打印收集到的所有用例
    print("收集到的所有用例:", items)
    # 通过改变items列表用例位置来改变用例执行顺序,如调换前面两条用例的顺序
    items[0],items[1] = items[1],items[0]

标签:函数,item,items,terminalreporter,hook,pytest,print,用例
来源: https://www.cnblogs.com/roronoazoro77/p/16586670.html