Pytest Hooks方法之pytest_runtest_makereport获取测试用例结果

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 ==========================

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/568269.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

服务端和客户端测试连通ip设置记录

若服务器和客户端在同一台电脑&#xff0c;即为本机测试&#xff0c;则测试都用127.0.0.1 若是服务器和客户端在两台电脑&#xff0c;则需要在同一局域网内&#xff0c;并且ip地址填写本机ip。 若是发布在云端测试&#xff0c;则服务端地址为0.0.0.0&#xff1b;客户端地址填…

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

pytest默认执行用例顺序是根据项目下文件名称按ascii码去收集运行的&#xff0c;文件里的用例是从上往下按顺序执行的. pytest_collection_modifyitems 这个函数顾名思义就是收集测试用例、改变用例的执行顺序的。 一、pytest_collection_modifyitems 是测试用例收集完成后&am…

MySQL 8.0开始Group by不再排序

如题所示&#xff0c;mysql 8.0 开始 group by 默认是没有排序的 那mysql 8.0 之前和 8.0 就有可能结果出现不同 需要警惕 查看版本信息 rootmysql3306.sock>[employees]>show variables like %version%; ------------------------------------------------------- | V…

Python操作Jira提交BUG

Jira提供了完善的RESTful API&#xff0c;如果不想直接请求API接口可以使用Python的Jira库来操作Jira jira Python文档https://jira.readthedocs.io/en/latest/ 安装&#xff1a;pip install jira 认证&#xff1a;Jira的访问是有权限的&#xff0c;在访问Jira项目时首先要进…

mysql 导入导出大文件

mysql导入&#xff1a; load data local infile C:/Users/Administrator.USER-20190225BY/Desktop/test.csv into table test fields terminated by\t lines terminated by\n ignore 1 lines (xianlu,chepai,shijian,jing,wei) mysql导出&#xff1a; select * from mysql.te…

Pytest之pytest.assume用例中断言1失败会继续执行后续代码断言2

一般我们做自动化测试时&#xff0c;一个用例会写多个断言&#xff0c;当第一个断言失败后&#xff0c;后面的代码就不会执行了&#xff0c;于是我们引进了pytest-assume插件可以解决断言失败后继续断言的问题。 一、安装依赖包 pip install pytest-assume 二、使用assert进…

Unable to round-trip http request to upstream错误

这几天打开浏览器经常出现Unable to round-trip http request to upstream。提示。 一直以为是网速的问题。今天百度才发现是因为打开了蓝灯。赶紧关掉蓝灯即可

Pytest之pytest-assume同用例多断言,断言1失败会执行后续代码及断言2

一般我们做自动化测试时&#xff0c;一个用例会写多个断言&#xff0c;当第一个断言失败后&#xff0c;后面的代码就不会执行了&#xff0c;于是我们引进了pytest-assume插件可以解决断言失败后继续断言的问题。一、安装依赖包pip install pytest-assume二、使用assert进行断言…

无法打开包括文件: “corecrt.h”: No such file or directory

刚开始安装qt&#xff0c;测试是否安装成功就出现此问题&#xff0c;让人很头大 参考&#xff1a;https://blog.csdn.net/x356982611/article/details/51140807的播客 编译时候使用的是最新的sdk版本10.0.10586.0版本但是它里面没有ucrt目录&#xff0c;问题找到了。 看了下v…

Pytest-ordering自定义用例执行顺序

我们一般在做自动化测试时&#xff0c;用例设计之间应该是可以相互独立执行的&#xff0c;没有一定的前后依赖关系的&#xff0c;如果我们真的有前后依赖&#xff0c;想指定用例的先后顺序&#xff0c;可以用到pytest-ordering插件解决这个问题 1、安装依赖包 pip install pyt…

LNK2019无法解析的外部符号 public: static struct cv::Ptr class cv::xfeatures2d::SURF问题解决

原因是lib文件没有引入 opencv_xfeatures2d320d.lib opencv_features2d320d.lib 在配置opencv时&#xff0c;应把D:\Program Files (x86)\opencv3\opencv\build\x64\vc14\lib下所有的lib文件都添加到链接器的输入中 也有可能是你下载的opencv库中没有这个lib&#xff0c;而你…

Pytest参数选项在脚本中和命令行用法详解

运行pytest可以指定目录和文件&#xff0c;如果不指定&#xff0c;pytest会搜索当前目录及其子目录中以test_开头或以_test结尾得测试函数。我们把pytest搜索测试文件和测试用例的过程称为测试搜索&#xff08;test discovery&#xff09;。只要遵循pytest的命名规则&#xff0…

Win7下OpenCV3.2.0+VS2015配置

参考自&#xff1a;https://blog.csdn.net/qq_22812319/article/details/78335880 一、预备知识 1、下载opencv时&#xff0c;opencv与vs版本是有固定关系的。 2、vs一般使用release版本。 3、首先在下载的openCV解压目录opencv/build/x64/下查看文件夹名&#xff0c;如下图…

Pytest标记预期失败得测试用例@pytest.mark.xfail()

pytest除了测试函数中使用这个方法pytest.xfail()外&#xff0c;xfail还有一种使用方法。就是pytest.mark.xfail()标记预期会失败的用例&#xff0c;即期望测试用例是失败的&#xff0c;但是不会影响测试用例的的执行。 标记的用例运行后&#xff0c;断言失败&#xff0c;所以…

win7 vs2015配置qt5.11

1、下载vs&#xff0c;和qt。 qt百度网盘下载链接&#xff1a;https://pan.baidu.com/s/12bUt31_mXGvgc3tdJRkkkw 提取码&#xff1a;2swk 下载完成之后&#xff0c;直接进行安装&#xff0c;下一步。 安装的时候要根据自己的VS版本选择相应的msvc&#xff08;VC运行库&…

LNK2019 无法解析的外部符号 __imp_CommandLineToArgvW,该符号在函数 WinMain 中被引用

在VS2017中新建Qt GUI 工程&#xff0c;无法编译&#xff0c;报链接错误 LNK2019 无法解析的外部符号 __imp_CommandLineToArgvW&#xff0c;该符号在函数 WinMain 中被引用 经比对&#xff0c;是lib配置问题。如歌在Qt Creator中创建工程然后再在VS2017中打开&#xff0c;则…

原创 | 开源AI测试专题、Jmeter测试专题

开源 AI 测试专题震惊&#xff01;AI成功落地自动化测试AI 软件测试工具 Mabl 快速概览AI测试工具 Mabl - 测试用例自动自愈Sauce Labs 的持续 UI 自动化测试云疫情中诞生的 AI 测试工具&#xff1a;ai-webdriverAI 测试又多新花招&#xff1f;ai-webdriver-1.0.1 独家更新一眼…

Qt 二级菜单无法输入中文

一、qt中二级菜单无法输入中文 解决办法&#xff1a;写到其他地方再粘贴复制过来即可ok 二、qt界面设置后&#xff0c;vs中没有反应 解决方法&#xff1a;在vs中寻找 ui_projectname.h projectname即为你的项目名&#xff0c;查看其中是否已有你qt中已经声明的控件&#x…

Python深层解析json数据之JsonPath

我们在做接口自动化时&#xff0c;一般接口响应的都是json数据体&#xff0c;对响应数据进行提取使用或断言&#xff0c;当数据量很大或层级很深时&#xff0c;就会变得很麻烦&#xff0c;于是就可以用到jsonpath模块&#xff0c;解决json路径深取值难的问题。一、 jsonpath介绍…

VS2015+qt5.11入门(实现计算机的加法和登录操作)

1、在vs中新建qt项目 2、打开Form Files中的.ui文件。即打开qt creator界面 3、在界面中添加两个label&#xff0c;两个LineEdit&#xff0c;一个计算按钮。点击保存 控件右击-->改变对象名称可以直接改变控件名。&#xff08;会映射显示在vs中的ui_projectname.h中&…