Time will tell.

Pytest 提供了很多钩子方法让我们对测试用例框架进行二次开发,可根据自己的需求进行改造。所以接下来就来学习下pytest_runtest_makereport这个钩子方法,更清晰地了解用例的执行过程,并获取每个用例的执行结果。


pytest_runtest_makereport

来看下相关源码:

from _pytest import runner# 对应源码
def pytest_runtest_makereport(item, call):""" return a :py:class:`_pytest.runner.TestReport` objectfor the given :py:class:`pytest.Item` and:py:class:`_pytest.runner.CallInfo`."""

这里item是测试用例,call是测试步骤,具体过程如下:

  1. 先执行 when=’setup’ 返回setup的执行结果。
  2. 然后执行 when=’call’ 返回call的执行结果。
  3. 最后执行 when=’teardown’ 返回teardown的执行结果。

1、案例

conftest.pypytest_runtest_makereport内容,打印运行过程和运行结果。

# conftest.py 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))

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

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

运行结果:

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 itemtest_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
123
------------------------------------
用例执行结果 <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 ===========================

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

setup -> call -> teardown

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

2、setup和teardown

给用例写个fixture增加用例的前置和后置操作,conftest.py如下:


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

运行结果:

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


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

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

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

test_a.py用例:

def test_a():'''用例描述:test_a'''print("123")assert 1==0


那么此时运行结果就是failed

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

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

teat_a.py用例:

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


最终结果:1 passed, 1 error in 0.16 seconds 。

3、只获取call结果

我们在写用例时,如果保证setupteardown不报错,只关注用例的运行结果,前面的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` objectfor 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 前置操作")yieldprint("teardown 后置操作")

好了,以上就是本章全部内容了,如果你对更多内容、及Python自动化软件测试、面试题、感兴趣的话可以加入我们175317069一起学习。群里会有各项测试学习资源发放,更有行业深潜多年的技术人分析讲解。

最后祝愿你能成为一名优秀的软件测试工程师!

欢迎【点赞】、【评论】、【关注】~

Time will tell.(时间会证明一切)

Python Pytest自动化测试 获取测试用例执行结果相关推荐

  1. python利用unittest进行测试用例执行的几种方式

    利用python进行测试时,测试用例的加载方式有2种:   一种是通过unittest.main()来启动所需测试的测试模块:   一种是添加到testsuite集合中再加载所有的被测试对象,而tes ...

  2. Python Pytest自动化测试 断言失败后续代码继续执行

    Time will tell. 做自动化测试时我们一般会一个用例写多个断言,而当第一个断言失败后,后面的代码就不会执行.这时我们可以引进pytest-assume插件来解决这些问题. 一.安装 pip ...

  3. python+appium自动化测试获取短信+图片验证码

    本篇文章主要讲述的是如何自动获取短信验证码和如何自动获取图片验证码,并写入到对应的输入框中(以下均使用微博的找回密码作为示例) 获取短信验证码的方法有三种,如下所示: 在手机的通知栏中获取短信内容 通 ...

  4. python+appium自动化测试-获取短信+图片验证码

    本篇文章主要讲述的是如何自动获取短信验证码和如何自动获取图片验证码,并写入到对应的输入框中(以下均使用微博的找回密码作为示例) 获取短信验证码的方法有三种,如下所示: 在手机的通知栏中获取短信内容 通 ...

  5. python+appium自动化测试-重复执行测试用例

    在功能测试过程中,经常会遇到一些偶然出现的Bug,需要通过重复执行用例来复现问题,那么,在自动化测试的过程中,对于一些偶然出现的Bug,也可以针对单个用例,或者针对某个模块的用例重复执行多次来复现. ...

  6. Selenium+python怎么搭建自动化测试框架、执行自动化测试用例、生成自动化测试报告、发送测试报告邮件

    目录 一.项目结构介绍 1.mztestpro测试项目 2.bbs目录 3.test_case 二.编写公共模块 三.编写Page Object 四.编写测试用例 五.执行测试用例 小结: 本人在网上 ...

  7. python+requests+unittest+HTMLTestRunner构建接口自动化测试框架(四)测试用例执行

    python+requests+unittest+HTMLTestRunner构建接口自动化测试框架(一)了解基础框架,读取配置文件 python+requests+unittest+HTMLTest ...

  8. Python+pytest+requests 自动化测试框架

    环境准备 本次选用的是Python+Pytest+requests来搭建自动化框架,需要有 Python 环境(3.x 版本),安装pytest和requests,不会安装的自行去网上搜教程. 关于 ...

  9. python基础===利用unittest进行测试用例执行的几种方式

    利用python进行测试时,测试用例的加载方式有2种:   一种是通过unittest.main()来启动所需测试的测试模块:   一种是添加到testsuite集合中再加载所有的被测试对象,而tes ...

  10. 【pytest】Hook 方法之 pytest_collection_modifyitems:修改测试用例执行顺序

    pytest_collection_modifyitems 是在用例收集完毕之后被调用,可以用来调整测试用例执行顺序: 它有三个参数,分别是: session:会话对象: config:配置对象: i ...

最新文章

  1. Pliops XDP(Extreme Data Processor)数据库存储设计的新型加速硬件
  2. 腾讯云连续四年登上KVM开源贡献榜,两项技术获评年度核心突破
  3. fastjson 1.1.71.android 版本发布,优化部分场景性能
  4. 编程小白学python知乎周刊_在知乎上学 Python - 入门篇
  5. 图片加载框架Glide的简单使用
  6. python vscode_VScode || 为VScode配置python环境
  7. python34.dll_python34.dll下载
  8. 请谈下Android消息机制,复习指南
  9. 废水处理计算书 excel_废水监测数据是匿名的吗?
  10. 54include对象
  11. 面试官又整新活,居然问我for循环用i++和++i哪个效率高?
  12. 用c语言编写小于n的所有素数,关于求N以内素数的一点小问题(N小于一亿)
  13. 【计算机组成原理笔记】计算机的基本组成
  14. 白盒测试工具CodeTest
  15. win10系统优化与防范策略
  16. LABjs分析 http://labjs.com/documentation.php#queuescript
  17. 如何撰写商业计划书(精简)
  18. 降低管理成本、增强团队协作
  19. 阿里直播平台的架构演进
  20. 使用Amazon SageMaker RL 和Unity训练强化学习智能体

热门文章

  1. pyqt5优化美化界面代码
  2. Java高并发编程实战2,原子性、可见性、有序性,傻傻分不清
  3. matlab读取scv文件,matlab如何读取csv文件
  4. vb中换行的几种方法
  5. logistic人口模型python代码_用Matlab程序对logistic人口模型进行拟合?
  6. 完全小白向win10安装配置Java运行环境
  7. 计算机vb期末试题及答案,VB期末考试试题及答案
  8. 网络规划设计师水平考试备考资料(4.网络规划与设计)
  9. 状态反馈控制与状态观测器设置以及利用LQR方法求取状态反馈矩阵
  10. 怀旧系列(2)----FoxBase,UCDOS,WPS