httprunner用例结构(前后置)

2024-03-08 22:52
文章标签 结构 用例 httprunner

本文主要是介绍httprunner用例结构(前后置),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

说明:httprunner 结合 pytest 的前后置方式

1. 用例级别前后置

1.1. setup teardown

class TestCaseRefTestcase(HttpRunner):# 用例级别前后置def setup(self):logger.warning("------用例级别前置")def teardown(self):logger.warning("------用例级别后置")config = (Config("request methods testcase: reference testcase").variables(**{"foo1": "testsuite_config_bar1","expect_foo1": "testsuite_config_bar1","expect_foo2": "config_bar2",}).base_url("https://postman-echo.com").verify(False))teststeps = [Step(RunTestCase("request with functions").with_variables(**{"foo1": "testcase_ref_bar1", "expect_foo1": "testcase_ref_bar1"}).call(Requests).export(*["foo3"])),Step(RunRequest("post form data").with_variables(**{"foo1": "bar1"}).post("/post").with_headers(**{"User-Agent": "${get_user_agent()}","Content-Type": "application/x-www-form-urlencoded",}).with_data("foo1=$foo1&foo2=$foo3").validate().assert_equal("status_code", 200).assert_equal("body.form.foo1", "bar1").assert_equal("body.form.foo2", "bar21")),]if __name__ == "__main__":TestCaseRefTestcase().test_start()

2. 类级别前后置

2.1. setup_class

    def setup_class(self):logger.warning("------类级别前置")

2.2. teardown_class

    def teardown_class(self):logger.warning("------类级别后置")

3. 模块级别前后置

3.1. conftest.py

import uuid
from typing import Listimport pytest
from httprunner import Config, Step
from loguru import logger@pytest.fixture(scope="session", autouse=True)
def session_fixture(request):"""setup and teardown each task"""logger.warning(f"-------conftest模块级别前置---------")total_testcases_num = request.node.testscollectedtestcases = []for item in request.node.items:testcase = {"name": item.cls.config.name,"path": item.cls.config.path,"node_id": item.nodeid,}testcases.append(testcase)logger.debug(f"collected {total_testcases_num} testcases: {testcases}")yieldlogger.debug(f"teardown task fixture")logger.warning(f"-------conftest模块级别后置---------")# teardown task# TODO: upload task summary@pytest.fixture(scope="function", autouse=True)
def testcase_fixture(request):"""setup and teardown each testcase"""logger.warning(f"-------conftest函数级别前置---------")config: Config = request.cls.configteststeps: List[Step] = request.cls.teststepslogger.debug(f"setup testcase fixture: {config.name} - {request.module.__name__}")def update_request_headers(steps, index):for teststep in steps:if teststep.request:index += 1teststep.request.headers["X-Request-ID"] = f"{prefix}-{index}"elif teststep.testcase and hasattr(teststep.testcase, "teststeps"):update_request_headers(teststep.testcase.teststeps, index)# you can update testcase teststep like thisprefix = f"HRUN-{uuid.uuid4()}"update_request_headers(teststeps, 0)yieldlogger.warning(f"-------conftest函数级别前置---------")logger.debug(f"teardown testcase fixture: {config.name} - {request.module.__name__}")summary = request.instance.get_summary()logger.debug(f"testcase result summary: {summary}")

4. 优先级顺序效果展示

testcases\debug\ref_testcase_test.py 2024-03-01 17:55:21.271 | WARNING  | testcases.debug.conftest:session_fixture:12 - -------conftest模块级别前置---------
2024-03-01 17:55:21.273 | DEBUG    | testcases.debug.conftest:session_fixture:23 - collected 1 testcases: [{'name': 'request methods testcase: reference testcase', 'path': 'D:\\httprunner\\demo\\testcases\\debug\\ref_testcase_test.py', 'node_id': 'testcases/debug/ref_testcase_test.py::TestCaseRefTestcase::test_start'}]
2024-03-01 17:55:21.274 | WARNING  | testcases.debug.ref_testcase_test:setup_class:18 - ------类级别前置
2024-03-01 17:55:21.275 | WARNING  | testcases.debug.conftest:testcase_fixture:37 - -------conftest函数级别前置---------
2024-03-01 17:55:21.275 | DEBUG    | testcases.debug.conftest:testcase_fixture:41 - setup testcase fixture: request methods testcase: reference testcase - testcases.debug.ref_testcase_test
2024-03-01 17:55:21.276 | WARNING  | testcases.debug.ref_testcase_test:setup:25 - ------用例级别前置

这篇关于httprunner用例结构(前后置)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/788669

相关文章

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

Java集合中的链表与结构详解

《Java集合中的链表与结构详解》链表是一种物理存储结构上非连续的存储结构,数据元素的逻辑顺序的通过链表中的引用链接次序实现,文章对比ArrayList与LinkedList的结构差异,详细讲解了链表... 目录一、链表概念与结构二、当向单链表的实现2.1 准备工作2.2 初始化链表2.3 打印数据、链表长

创建springBoot模块没有目录结构的解决方案

《创建springBoot模块没有目录结构的解决方案》2023版IntelliJIDEA创建模块时可能出现目录结构识别错误,导致文件显示异常,解决方法为选择模块后点击确认,重新校准项目结构设置,确保源... 目录创建spChina编程ringBoot模块没有目录结构解决方案总结创建springBoot模块没有目录

SpringBoot利用树形结构优化查询速度

《SpringBoot利用树形结构优化查询速度》这篇文章主要为大家详细介绍了SpringBoot利用树形结构优化查询速度,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一个真实的性能灾难传统方案为什么这么慢N+1查询灾难性能测试数据对比核心解决方案:一次查询 + O(n)算法解决

Oracle查询表结构建表语句索引等方式

《Oracle查询表结构建表语句索引等方式》使用USER_TAB_COLUMNS查询表结构可避免系统隐藏字段(如LISTUSER的CLOB与VARCHAR2同名字段),这些字段可能为dbms_lob.... 目录oracle查询表结构建表语句索引1.用“USER_TAB_COLUMNS”查询表结构2.用“a

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

如何使用Maven创建web目录结构

《如何使用Maven创建web目录结构》:本文主要介绍如何使用Maven创建web目录结构的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录创建web工程第一步第二步第三步第四步第五步第六步第七步总结创建web工程第一步js通过Maven骨架创pytho

Python循环结构全面解析

《Python循环结构全面解析》循环中的代码会执行特定的次数,或者是执行到特定条件成立时结束循环,或者是针对某一集合中的所有项目都执行一次,这篇文章给大家介绍Python循环结构解析,感兴趣的朋友跟随... 目录for-in循环while循环循环控制语句break语句continue语句else子句嵌套的循

Python+PyQt5实现文件夹结构映射工具

《Python+PyQt5实现文件夹结构映射工具》在日常工作中,我们经常需要对文件夹结构进行复制和备份,本文将带来一款基于PyQt5开发的文件夹结构映射工具,感兴趣的小伙伴可以跟随小编一起学习一下... 目录概述功能亮点展示效果软件使用步骤代码解析1. 主窗口设计(FolderCopyApp)2. 拖拽路径

使用Java实现通用树形结构构建工具类

《使用Java实现通用树形结构构建工具类》这篇文章主要为大家详细介绍了如何使用Java实现通用树形结构构建工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录完整代码一、设计思想与核心功能二、核心实现原理1. 数据结构准备阶段2. 循环依赖检测算法3. 树形结构构建4. 搜索子