pytest提供的钩子(Hooks)方法之pytest_runtest_makereport,可以更清晰的了解用例的执行过程,并获取到每个用例的执行结果。

一、Hook 方法之 pytest_runtest_makereport源码:

@hookspec(firstresult=True)
def pytest_runtest_makereport(item, call):""" return a :py:class:`_pytest.runner.TestReport` objectfor the given :py:class:`pytest.Item <_pytest.main.Item>` and:py:class:`_pytest.runner.CallInfo`.Stops at first non-None result, see :ref:`firstresult` """

二、作用:
对于给定的测试用例(item)和调用步骤(call),返回一个测试报告对象(_pytest.runner.TestReport);

具体表现为:这个钩子方法会被每个测试用例调用 3 次,分别是:
用例的 setup 执行完毕后,调用 1 次,返回 setup 的执行结果;
用例执行完毕之后,调用 1 次,返回测试用例的执行结果;
用例的 teardown 执行完毕后,调用1 次,返回 teardown 的执行结果;

三、conftest.py写入pytest_runtest_makereport 内容如下:

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)

四、test01.py写入测试用例如下:

#!/usr/bin/env python
# _*_coding:utf-8_*_import pytestclass Test(object):def setup(self):print("setup前置操作")def teardown(self):print("teardown后置操作")def test_01(self):"""用例描述"""print("用例1——橙子")if __name__ == '__main__':pytest.main(["-s", "test01.py"])

用例执行后如下:

运行结果如下:
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
collected 1 itemtest01.py setup前置操作
——————————————————————————
用例执行结果 <pluggy.callers._Result object at 0x000002436AA8D898>
测试报告:<TestReport 'test01.py::Test::test_01' when='setup' outcome='passed'>
步骤:setup
nodeid:test01.py::Test::test_01
description:用例描述
执行结果: passed
用例1——橙子
——————————————————————————
用例执行结果 <pluggy.callers._Result object at 0x000002436AA8D898>
测试报告:<TestReport 'test01.py::Test::test_01' when='call' outcome='passed'>
步骤:call
nodeid:test01.py::Test::test_01
description:用例描述
执行结果: passed
.teardown后置操作
——————————————————————————
用例执行结果 <pluggy.callers._Result object at 0x000002436AA8D898>
测试报告:<TestReport 'test01.py::Test::test_01' when='teardown' outcome='passed'>
步骤:teardown
nodeid:test01.py::Test::test_01
description:用例描述
执行结果: passed============================== 1 passed in 0.03s ==============================

五、setup失败情况

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

#!/usr/bin/env python
# _*_coding:utf-8_*_import pytestclass Test(object):def setup(self):print("setup前置操作")raise Exception("setup执行失败了")def teardown(self):print("teardown后置操作")def test_01(self):"""用例描述"""print("用例1——橙子")if __name__ == '__main__':pytest.main(["-s", "test01.py"])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
collected 1 itemtest01.py setup前置操作
——————————————————————————
用例执行结果 <pluggy.callers._Result object at 0x000001CD851B9630>
测试报告:<TestReport 'test01.py::Test::test_01' when='setup' outcome='failed'>
步骤:setup
nodeid:test01.py::Test::test_01
description:用例描述
执行结果: failed
E——————————————————————————
用例执行结果 <pluggy.callers._Result object at 0x000001CD851B9518>
测试报告:<TestReport 'test01.py::Test::test_01' when='teardown' outcome='passed'>
步骤:teardown
nodeid:test01.py::Test::test_01
description:用例描述
执行结果: passed=================================== ERRORS ====================================
_______________________ ERROR at setup of Test.test_01 ________________________self = <test.test01.Test object at 0x000001CD851B1780>def setup(self):print("setup前置操作")
>       raise Exception("setup执行失败了")
E       Exception: setup执行失败了test01.py:10: Exception
============================== 1 error in 0.11s ===============================

六、用例断言失败,用例结果是failed,setup和teardown都是pass的,最终执行结果是failed

#!/usr/bin/env python
# _*_coding:utf-8_*_import pytestclass Test(object):def setup(self):print("setup前置操作")def teardown(self):print("teardown后置操作")def test_01(self):"""用例描述"""print("用例1——橙子")assert 0if __name__ == '__main__':pytest.main(["-s", "test01.py"])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
collected 1 itemtest01.py setup前置操作
——————————————————————————
用例执行结果 <pluggy.callers._Result object at 0x00000251E9988550>
测试报告:<TestReport 'test01.py::Test::test_01' when='setup' outcome='passed'>
步骤:setup
nodeid:test01.py::Test::test_01
description:用例描述
执行结果: passed
用例1——橙子
——————————————————————————
用例执行结果 <pluggy.callers._Result object at 0x00000251E990E2B0>
测试报告:<TestReport 'test01.py::Test::test_01' when='call' outcome='failed'>
步骤:call
nodeid:test01.py::Test::test_01
description:用例描述
执行结果: failed
Fteardown后置操作
——————————————————————————
用例执行结果 <pluggy.callers._Result object at 0x00000251E990E470>
测试报告:<TestReport 'test01.py::Test::test_01' when='teardown' outcome='passed'>
步骤:teardown
nodeid:test01.py::Test::test_01
description:用例描述
执行结果: passed================================== FAILURES ===================================
________________________________ Test.test_01 _________________________________self = <test.test01.Test object at 0x00000251E9981748>def test_01(self):"""用例描述"""print("用例1——橙子")
>       assert 0
E       AssertionErrortest01.py:17: AssertionError
============================== 1 failed in 0.15s ==============================

七、teardown执行失败了,teardown的执行结果的failed,setup和case都是pass的,最终执行结果是1 passed,1 error

#!/usr/bin/env python
# _*_coding:utf-8_*_import pytestclass Test(object):def setup(self):print("setup前置操作")def teardown(self):print("teardown后置操作")assert 0def test_01(self):"""用例描述"""print("用例1——橙子")if __name__ == '__main__':pytest.main(["-s", "test01.py"])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0, rerunfailures-8.0
collected 1 itemtest01.py setup前置操作
——————————————————————————
用例执行结果 <pluggy.callers._Result object at 0x000001A7DF259588>
测试报告:<TestReport 'test01.py::Test::test_01' when='setup' outcome='passed'>
步骤:setup
nodeid:test01.py::Test::test_01
description:用例描述
执行结果: passed
用例1——橙子
——————————————————————————
用例执行结果 <pluggy.callers._Result object at 0x000001A7DF259588>
测试报告:<TestReport 'test01.py::Test::test_01' when='call' outcome='passed'>
步骤:call
nodeid:test01.py::Test::test_01
description:用例描述
执行结果: passed
.teardown后置操作
——————————————————————————
用例执行结果 <pluggy.callers._Result object at 0x000001A7DF1DE9B0>
测试报告:<TestReport 'test01.py::Test::test_01' when='teardown' outcome='failed'>
步骤:teardown
nodeid:test01.py::Test::test_01
description:用例描述
执行结果: failed
E=================================== ERRORS ====================================
______________________ ERROR at teardown of Test.test_01 ______________________self = <test.test01.Test object at 0x000001A7DF251780>def teardown(self):print("teardown后置操作")
>       assert 0
E       AssertionErrortest01.py:13: AssertionError
========================= 1 passed, 1 error in 0.12s ==========================

Pytest Hooks方法之pytest_runtest_makereport获取测试用例结果相关推荐

  1. Pytest Hooks方法之pytest_collection_modifyitems改变测试用例执行顺序

    pytest默认执行用例顺序是根据项目下文件名称按ascii码去收集运行的,文件里的用例是从上往下按顺序执行的. pytest_collection_modifyitems 这个函数顾名思义就是收集测 ...

  2. 【EventBus】事件通信框架 ( 订阅方法注册 | 检查订阅方法缓存 | 反射获取订阅类中的订阅方法 )

    文章目录 一.检查订阅方法缓存 二.反射获取订阅类中的订阅方法 三.完整代码示例 一.检查订阅方法缓存 注册订阅者时 , 只传入一个订阅者类对象 , 其它信息都需要通过反射获取 ; 1. 获取订阅者类 ...

  3. php获取ip几种方法区别,php获取客户端IP地址的几种方法

    php获取客户端IP地址的几种方法 阅读php获取客户端IP地址的几种方法, 1:来看看代码: echo "(1)浏览当前页面的用户的 IP 地址为:"; echo $_SERVE ...

  4. 抓取一台电脑linux,教程方法;用来获取Linux主机信息的5个常用命令电脑技巧-琪琪词资源网...

    琪琪词资源网-教程方法;用来获取Linux主机信息的5个常用命令电脑技巧,以下是给大家带来的教程方法;用来获取Linux主机信息的5个常用命令,大家可以了解一下哦! 有些时候Linux 系统管理员在接 ...

  5. php返回类中方法,php如何获取类中所有的方法名

    php获取类中所有的方法名的方法:可以利用[get_class_methods()]函数来获取,[get_class_methods()]函数可以返回指定类中所有的方法名,并且会将方法名保存到数组中. ...

  6. 判断ajax获取是否为空,使用paginate方法分页无法判断获取的数据是否为空

    问题:使用paginate方法分页无法判断获取的数据是否为空,在模板里面无法判断数据是否为空,比如在商品列表当中,当没有商品时无法判断生成的对象为空,所有就什么都不显示了. 解决办法: $newsDa ...

  7. 你会采取什么方法改进你的测试用例_软件测试面试时常考的几道面试题

    问:你在测试中发现了一个  bug ,但是开发经理认为这不是一个  bug ,你应该怎样解决. 1.将问题提交到缺陷管理库里面进行备案. 2.要获取判断的依据和标准: 根据需求说明书.产品说明.设计文 ...

  8. pytest合集(8)— 测试用例和夹具的参数化

    一.测试用例参数化 pytest.mark.parametrize() 语法糖: parametrize(argnames, argvalues, indirect=False, ids=None,  ...

  9. 使用RunTime添加动态方法、方法交换、获取所有属性来重写归档解档

    #import "JZGMMMMModel.h" #import <objc/message.h> @implementation JZGMMMMModel //**动 ...

最新文章

  1. Microsoft patterns practices Enterprise Library released
  2. Assembly.Load()不能加载程序集的问题
  3. spark sql and hive 3g数据测试
  4. Shell多线程操作及线程数控制实例
  5. CodeForces:372(div1)div373(div2)
  6. 毕啸南专栏 | 对话李开复:AI科学家的转型之路
  7. 数据挖掘面试题之梯度提升树
  8. 支持向量机SVM原理(参数解读和python脚本)
  9. 人工势场法matlab讲解_【机器人路径规划】人工势场法
  10. c语言实现freqspace函数,滤波器程序
  11. html页面禁止保存图片,JavaScript 禁止用户保存图片的实现代码
  12. 苹果Mac电脑的复制粘贴不能用了
  13. 推荐系统-协同过滤在Spark中的实现
  14. [转]Cookie详解
  15. 【react】---redux-actions的基本使用---【巷子】
  16. holdpwd php,PHPMyWind后台管理界面的SQL注入漏洞 - 网站安全
  17. BIM土建插件墙齐梁板操作,实现墙、柱的顶部、底部对齐
  18. SOC2- 安全性、可用性、进程完整性、机密性和隐私性五大控制属性
  19. PowerDesigner16.5如何导出表到word的方法
  20. 数学基础知识之Sin、cos画圆

热门文章

  1. keras训练完以后怎么预测_农村小孩只有户口,没有承包地,以后怎么养老?看完我安心了...
  2. android预加载app,Android欢迎页预加载首页数据
  3. 数据结构之线性表:单链表
  4. MySQL 高级 ———— MySQL逻辑架构图简介
  5. MySQL————表维护相关低频操作总结
  6. 安卓与Linux共存,Android和Linux重新合并成一个操作系统
  7. linux mp3长度,得到一个wma或mp3文件,如何用最简单的方法得到它的长度信息呢?...
  8. Altium Designer20原理图库制作
  9. js中的if与Java中的if_JS直接if参数的用法JS中!和!!区别
  10. 《零基础》MySQL UNION 操作符(十七)