pytest_runtest_makereport钩子函数获取测试用例执行结果

前言

pytest测试框架提供的很多钩子函数方便我们对测试框架进行二次开发,可以根据自己的需求进行改造。

例如:钩子方法:pytest_runtest_makereport ,可以更清晰的了解测试用例的执行过程,并获取到每个测试用例的执行结果。

pytest_runtest_makereport方法简介

先看下相关的源码,在 _pytest/runner.py 文件下,可以导入之后查看:

pytest_runtest_makereport钩子函数获取测试用例执行结果_第1张图片

源码:

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`.
    """

装饰器 pytest.hookimpl(hookwrapper=True, tryfirst=True) 解释:

@pytest.hookimpl(hookwrapper=True)装饰的钩子函数,有以下两个作用:

1、可以获取到测试用例不同执行阶段的结果(setup,call,teardown)

2、可以获取钩子方法 pytest_runtest_makereport(item, call) 的调用结果(yield返回一个测试用例执行后的result对象)和调用结果result对象中的测试报告(返回一个report对象)

pytest_runtest_makereport(item, call) 钩子函数参数解释:

1、 item 是测试用例对象;

2、 call 是测试用例的测试步骤;具体执行过程如下:

①先执行 when="setup" ,返回setup用例前置操作函数的执行结果。

②然后执行 when="call" ,返回call测试用例的执行结果。

③最后执行 when="teardown" ,返回teardown用例后置操作函数的执行结果。

第一个案例

conftest.py 文件编写pytest_runtest_makereport 钩子方法,打印运行过程和运行结果。

# conftest.py 

import pytest

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    print('------------------------------------')

    # 获取钩子方法的调用结果,返回一个result对象
    out = yield
    print('用例执行结果', out)

    # 从钩子方法的调用结果中获取测试报告
    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))

test_a.py 写一个简单的用例:

def test_a():

    '''用例描述:test_a'''

    print("123")

运行结果:

pytest_runtest_makereport钩子函数获取测试用例执行结果_第2张图片

pytest_runtest_makereport钩子函数获取测试用例执行结果_第3张图片

结果分析:

从结果可以看到,测试用例的执行过程会经历3个阶段:

setup -> call -> teardown

每个阶段会返回 Result 对象和 TestReport 对象,以及对象属性。(setupteardown上面的用例默认没有,结果都是passed。)

第二个案例

给用例写个 fixture() 函数增加测试用例的前置和后置操作; conftest.py 如下:

import pytest

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    print('------------------------------------')

    # 获取钩子方法的调用结果
    out = yield
    print('用例执行结果', out)

    # 从钩子方法的调用结果中获取测试报告
    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.fixture(scope="session", autouse=True)
def fix_a():
    print("setup 前置操作")
    yield 
    print("teardown 后置操作")

运行结果:

pytest_runtest_makereport钩子函数获取测试用例执行结果_第4张图片

pytest_runtest_makereport钩子函数获取测试用例执行结果_第5张图片

第三个案例

fixture() 函数的 setup 前置函数在执行时异常,即 setup 执行结果为 failed ,则后面的 call 测试用例与 teardown 后置操作函数都不会执行。

此时的状态是 error ,也就是代表测试用例还没开始执行就已经异常了。(在执行前置操作函数的时候就已经发生异常)

import pytest


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    print('------------------------------------')

    # 获取钩子方法的调用结果
    out = yield
    print('用例执行结果', out)

    # 从钩子方法的调用结果中获取测试报告
    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.fixture(scope="session", autouse=True)
def fix_a():
    print("setup 前置操作")
    assert 1 == 2
    yield
    print("teardown 后置操作")

运行结果:

pytest_runtest_makereport钩子函数获取测试用例执行结果_第6张图片

pytest_runtest_makereport钩子函数获取测试用例执行结果_第7张图片

第四个案例

setup 前置操作函数正常执行,测试用例 call 执行发生异常。

此时的测试用例执行结果为 failed

#  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.fixture(scope="session", autouse=True)
def fix_a():
    print("setup 前置操作")
    yield
    print("teardown 后置操作")
#  test_a.py

def test_a():
    """用例描述:test_a"""

    print("123")
    assert 1 == 0

运行结果:

pytest_runtest_makereport钩子函数获取测试用例执行结果_第8张图片

pytest_runtest_makereport钩子函数获取测试用例执行结果_第9张图片

第五个案例

setup 前置操作函数正常执行,测试用例 call 正常执行, teardown 后置操作函数执行时发生异常。

测试用例正常执行,但是测试结果中会有 error ,因为 teardown 后置操作函数在执行时发生异常

# conftest.py

import pytest


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    print('------------------------------------')

    # 获取钩子方法的调用结果
    out = yield
    print('用例执行结果', out)

    # 从钩子方法的调用结果中获取测试报告
    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.fixture(scope="session", autouse=True)
def fix_a():
    print("setup 前置操作")
    yield
    print("teardown 后置操作")
    raise Exception("teardown 失败了")
#  test_a.py

def test_a():
    '''用例描述:test_a'''
    print("123")

运行结果:

pytest_runtest_makereport钩子函数获取测试用例执行结果_第10张图片

pytest_runtest_makereport钩子函数获取测试用例执行结果_第11张图片

pytest_runtest_makereport钩子函数获取测试用例执行结果_第12张图片

第六个案例:只获取call结果

场景:编写测试用例时,在保证 setup 前置操作函数和 teardown 后置操作函数不报错的前提下,我们一般只需要关注测试用例的执行结果,即只需要获取测试用例执行call的结果。

解决办法:因为前面的 pytest_runtest_makereport 钩子方法执行了三次。所以在打印测试报告的相关数据之气可以加个判断: if report.when == "call" 。

import pytest
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`.
    """
'''


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    print('------------------------------------')

    # 获取钩子方法的调用结果
    out = yield
    # print('用例执行结果:', out)

    # 从钩子方法的调用结果中获取测试报告
    report = out.get_result()

    if report.when == "call":
        print('测试报告:%s' % report)
        print('步骤:%s' % report.when)
        print('nodeid:%s' % report.nodeid)
        print('description:%s' % str(item.function.__doc__))
        print(('运行结果: %s' % report.outcome))


@pytest.fixture(scope="session", autouse=True)
def fix_a():
    print("setup 前置操作")
    yield
    print("teardown 后置操作")

运行结果:

pytest_runtest_makereport钩子函数获取测试用例执行结果_第13张图片

pytest_runtest_makereport钩子函数获取测试用例执行结果_第14张图片

conftest.py 去除pytest_runtest_makereport 钩子方法,正常执行测试用例

# conftest.py

import pytest

@pytest.fixture(scope="session", autouse=True)
def fix_a():
    print("setup 前置操作")
    yield
    print("teardown 后置操作")
# test_a.py

def test_a():
    """用例描述:test_a"""

    print("123")

运行结果:

pytest_runtest_makereport钩子函数获取测试用例执行结果_第15张图片

我这里给你们分享一下我所积累和真理的文档和学习资料有需要是领取就可以了

1、学习思路和方法

这个大纲涵盖了目前市面上企业百分之99的技术,这个大纲很详细的写了你该学习什么内容,企业会用到什么内容。总共十个专题足够你学习

pytest_runtest_makereport钩子函数获取测试用例执行结果_第16张图片

2、想学习却无从下手,该如何学习?

这里我准备了对应上面的每个知识点的学习资料、可以自学神器,已经项目练手。

pytest_runtest_makereport钩子函数获取测试用例执行结果_第17张图片

pytest_runtest_makereport钩子函数获取测试用例执行结果_第18张图片

3、软件测试/自动化测试【全家桶装】学习中的工具、安装包、插件....

pytest_runtest_makereport钩子函数获取测试用例执行结果_第19张图片

pytest_runtest_makereport钩子函数获取测试用例执行结果_第20张图片

pytest_runtest_makereport钩子函数获取测试用例执行结果_第21张图片

4、有了安装包和学习资料,没有项目实战怎么办,我这里都已经准备好了往下看

pytest_runtest_makereport钩子函数获取测试用例执行结果_第22张图片

最后送上一句话:
世界的模样取决于你凝视它的目光,自己的价值取决于你的追求和心态,一切美好的愿望,不在等待中拥有,而是在奋斗中争取。
如果我的博客对你有帮助、如果你喜欢我的文章内容,请 “点赞” “评论” “收藏” 一键三连哦!

pytest_runtest_makereport钩子函数获取测试用例执行结果_第23张图片

你可能感兴趣的:(pytest框架,测试用例,pytest)