pytest文档33-Hooks函数获取用例执行结果(pytest_runtest_makereport)

前言

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

先学习下pytest_runtest_makereport这个钩子方法,可以更清晰的了解用例的执行过程,并获取到每个用例的执行结果。

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 的执行结果

运行案例

conftest.py 写 pytest_runtest_makereport 内容,打印运行过程和运行结果

# 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))

test_a.py写一个简单的用例

def test_a():
    ‘‘‘用例描述:test_a‘‘‘
    print("上海-悠悠")

运行结果如下

D:\soft\code\pytest_jenkins_demo\demo>pytest -s
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1
rootdir: D:\demo
plugins: html-1.19.0,
collected 1 item

test_a.py ------------------------------------
用例执行结果 <pluggy.callers._Result object at 0x0000027C547332B0>
测试报告:<TestReport ‘test_a.py::test_a‘ when=‘setup‘ outcome=‘passed‘>
步骤:setup
nodeid:test_a.py::test_a
description:用例描述:test_a
运行结果: passed
上海-悠悠
------------------------------------
用例执行结果 <pluggy.callers._Result object at 0x0000027C547332B0>
测试报告:<TestReport ‘test_a.py::test_a‘ when=‘call‘ outcome=‘passed‘>
步骤:call
nodeid:test_a.py::test_a
description:用例描述:test_a
运行结果: passed
.------------------------------------
用例执行结果 <pluggy.callers._Result object at 0x0000027C54750A20>
测试报告:<TestReport ‘test_a.py::test_a‘ when=‘teardown‘ outcome=‘passed‘>
步骤:teardown
nodeid:test_a.py::test_a
description:用例描述:test_a
运行结果: passed

========================== 1 passed in 0.06 seconds ===========================

从运行结果可以看出,运行用例的过程会经历三个阶段:setup-call-teardown,每个阶段都会返回的 Result 对象和 TestReport 对象,以及对象属性。

setup和teardown上面的用例默认都没有,结果都是passed。

setup和teardown

给用例写个fixture增加用例的前置和后置操作,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 后置操作")

运行结果如下

setup失败情况

当setup执行失败了,setup的执行结果的failed,后面的call用例和teardown都不会执行了

此时用例的状态是:error, 也就是用例(call)都还没开始执行,就异常了。

call失败情况

如果setup正常执行,但是测试用例call失败了

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

test_a.py用例

def test_a():
    ‘‘‘用例描述:test_a‘‘‘
    print("上海-悠悠")
    assert 1==0

那么此时运行的结果就是failed

teardown失败了

如果setup正常执行,测试用例call正常执行,teardown失败了,这种情况

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

teat_a.py用例

def test_a():
    ‘‘‘用例描述:test_a‘‘‘
    print("上海-悠悠")

最终统计的结果: 1 passed, 1 error in 0.16 seconds

只获取call的结果

我们在写用例的时候,如果保证setup和teardown不报错情况,只关注测试用例本身的运行结果,前面的 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)

    # 3. 从钩子方法的调用结果中获取测试报告
    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 后置操作")

运行结果

原文地址:https://www.cnblogs.com/yoyoketang/p/12609871.html

时间: 2024-08-30 16:53:53

pytest文档33-Hooks函数获取用例执行结果(pytest_runtest_makereport)的相关文章

pytest文档3-pycharm运行pytest

前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻烦,所以很有必要学习如何在pycharm里面运行pytest用例 pycharm运行三种方式 1.以xx.py脚本方式直接执行,当写的代码里面没用到unittest和pytest框架时,并且脚本名称不是以test_开头命名的,此时pycharm会以xx.py脚本方式运行 2.当脚本命名为test_x

JS如何实现文档加载完成后再去执行代码

JS如何实现文档加载完成后再去执行代码:在执行某些操作的时候,需要当文档完全加载完成之后再去执行,否则可能出现意向不到的情况,先看一段代码实例: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.51texiao.cn/" /> <title&g

pytest文档1-环境准备与入门

pytest简介 pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高.根据pytest的官方网站介绍,它具有如下特点: 非常容易上手,入门简单,文档丰富,文档中有很多实例可以参考 能够支持简单的单元测试和复杂的功能测试 支持参数化 执行测试过程中可以将某些测试跳过(skip),或者对某些预期失败的case标记成失败 支持重复执行(rerun)失败的case 支持运行由nose, unittest编写的

python文档字符串(函数使用说明)

关键字: 函数说明.help()函数 1.效果图: 2.代码: # 文档字符串( doc str) 是 函数使用说明 # 用法: 在函数第一行写一个字符串 def fn(*nums): ''' 函数的作用: 计算任意数值的总和 函数的参数: *nums 会接受所有传进来的值,保存到一个元组中(装包) ''' print(nums,type(nums)) #定义一个变量,用来保存总和 result = 0 for n in nums: result += n return result print

自定义Hooks函数获取窗口大小(十一)

其实自定义Hooks函数和用Hooks创建组件很相似,跟我们平时用JavaScript写函数几乎一模一样,可能就是多了些React Hooks的特性,自定义Hooks函数偏向于功能,而组件偏向于界面和业务逻辑.由于差别不大,所以使用起来也是很随意的.如果是小型项目是可以的,但是如果项目足够复杂,这会让项目结构不够清晰.所以学习自定义Hooks函数还是很有必要的. 编写自定义函数 在实际开发中,为了界面更加美观.获取浏览器窗口的尺寸是一个经常使用的功能,这样经常使用的功能,就可以封装成一个自定义H

pytest文档17-fixture之autouse=True

前言 平常写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了.当用例很多的时候,每次都传这个参数,会比较麻烦. fixture里面有个参数autouse,默认是Fasle没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了 调用fixture三种方法 1.函数或类里面方法直接传fixture的函数参数名称 2.使用装饰器@pytest.mark.usefixtures()修饰 3.autouse=True自动使用 用例传f

pytest文档31-allure标记用例级别severity

前言 我们在做功能测试的时候,执行完一轮测试用例,输出测试报告的时候,会有统计缺陷的数量和等级. 在做自动化测试的过程中,当你的测试用例越来越多的时候,如果执行一轮测试发现了几个测试不通过,我们也希望能快速统计出缺陷的等级. pytest结合allure框架可以对用例的等级做详细的划分. 用例等级 allure对用例的等级划分成五个等级 blocker 阻塞缺陷(功能未实现,无法下一步) critical 严重缺陷(功能点缺失) normal 一般缺陷(边界情况,格式错误) minor 次要缺陷

pytest文档5-fixture之conftest.py

前言 前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录,用例3需要先登录.很显然这就无法用setup和teardown来实现了.这就是本篇学习的目的,自定义测试用例的预置条件 fixture优势 1.firture相对于setup和teardown来说应该有以下几点优势 命名方式灵活,不局限于setup和teardown这几个命名 conftest.py 配置里可以实现数

pytest文档9-参数化parametrize

前言 pytest.mark.parametrize装饰器可以实现测试用例参数化. parametrizing 1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子 # content of test_expectation.py # coding:utf-8 import pytest @pytest.mark.parametrize("test_input,expected", [ ("3+5", 8), ("2+4", 6), (&