4、httprunner参数化

2023-12-30 12:08
文章标签 参数 httprunner

本文主要是介绍4、httprunner参数化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、parameters参数

运行次数是笛卡尔积的计算方式,如下将运行2*2=4次

yml方式

config:name: 登录幕布variables: {}parameters:phone: ['13104978922','13722349423']password: ['123456','2345']verify: false
teststeps:
-   name: /v3/api/user/phone_loginrequest:headers:accept: application/json, text/plain, */*accept-encoding: gzip, deflate, braccept-language: zh-CN,zh;q=0.9content-length: '69'content-type: application/json;charset=UTF-8data-unique-id: 7b70bf17-50f2-470e-ae20-ecbbd0d6190fjwt-token: ''origin: https://mubu.comreferer: https://mubu.com/sec-ch-ua: '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"'sec-ch-ua-mobile: ?0sec-ch-ua-platform: '"Windows"'sec-fetch-dest: emptysec-fetch-mode: corssec-fetch-site: same-siteuser-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36version: 3.0.0-2.0.0.1824x-request-id: 8a061286-b850-40a2-b680-6a0314367fedjson:callbackType: 0password: $passwordphone: $phonemethod: POSTurl: https://api2.mubu.com/v3/api/user/phone_loginvalidate:-   eq:- status_code- 200-   eq:- body.code- 0

py文件方式

# NOTE: Generated By HttpRunner v3.1.6
# FROM: testcases\ddt\create_dir.ymlimport pytest
from httprunner import Parametersfrom httprunner import HttpRunner, Config, Step, RunRequest, RunTestCaseclass TestCaseCreateDir(HttpRunner):@pytest.mark.parametrize("param", Parameters({"newfile": ["新增文件夹1", "新增文件夹2", "新增文件夹3"]}))def test_start(self, param):super().test_start(param)config = Config("HttpRunner实现幕布接口增删改查").verify(False)teststeps = [Step(RunRequest("幕布-新增文件夹").post("https://api2.mubu.com/v3/api/list/create_folder").with_headers(**{"accept": "application/json, text/plain, */*","accept-encoding": "gzip, deflate, br","accept-language": "zh-CN,zh;q=0.9","content-length": "33","content-type": "application/json;charset=UTF-8","data-unique-id": "7b70bf17-50f2-470e-ae20-ecbbd0d6190f","jwt-token": "${get_token()}","origin": "https://mubu.com","referer": "https://mubu.com/","sec-ch-ua": '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',"sec-ch-ua-mobile": "?0","sec-ch-ua-platform": '"Windows"',"sec-fetch-dest": "empty","sec-fetch-mode": "cors","sec-fetch-site": "same-site","user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36","version": "3.0.0-2.0.0.1824","x-request-id": "195dee55-182a-4eb7-a307-714fa3f72dd1",}).with_json({"folderId": "0", "name": "$newfile"}).validate().assert_equal("status_code", 200).assert_equal("body.code", 0)),]if __name__ == "__main__":TestCaseCreateDir().test_start()

二、debugtalk.py方式

直接使用${函数名()} 调用

# NOTE: Generated By HttpRunner v3.1.6
# FROM: har\create_dir.yml
from typing import Dictimport pytest
from httprunner import HttpRunner, Config, Step, RunRequest, Parameters
#取别名,防止pytest 把导入的以Test开头的用例也执行了
from testcases.login_test import TestCaseLogin as Loginclass TestCaseCreateDirDeTalk(HttpRunner):@pytest.mark.parametrize("param", Parameters({"filename": "${get_file_name()}"}))def test_start(self, param):super().test_start(param)config = Config("HttpRunner实现幕布接口-文件夹add操作")\.verify(False).export(*['folder_id','param'])teststeps = [Step(RunRequest("幕布-新增文件夹").post("https://api2.mubu.com/v3/api/list/create_folder").with_headers(**{"accept": "application/json, text/plain, */*","accept-encoding": "gzip, deflate, br","accept-language": "zh-CN,zh;q=0.9","content-length": "33","content-type": "application/json;charset=UTF-8","data-unique-id": "7b70bf17-50f2-470e-ae20-ecbbd0d6190f","jwt-token": "${get_token()}","origin": "https://mubu.com","referer": "https://mubu.com/","sec-ch-ua": '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',"sec-ch-ua-mobile": "?0","sec-ch-ua-platform": '"Windows"',"sec-fetch-dest": "empty","sec-fetch-mode": "cors","sec-fetch-site": "same-site","user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36","version": "3.0.0-2.0.0.1824","x-request-id": "195dee55-182a-4eb7-a307-714fa3f72dd1",}).with_json({"folderId": "0","name": "$filename"}).extract().with_jmespath('body.data.folder.id',"folder_id").validate().assert_equal("status_code", 200).assert_equal("body.code", 0)),]if __name__ == "__main__":TestCaseCreateDirDeTalk().test_start()

三、CSV方式

1、新建文件 testdata/userpwd.csv

phone,password
13822228765,12312
13822508211,baba139262265
13822508676,baba139262144

2、yml方式

config:name: 登录幕布variables: {}parameters:phone-password: ${P(testdata/userpwd.csv)}verify: false
teststeps:
-   name: /v3/api/user/phone_loginrequest:headers:accept: application/json, text/plain, */*accept-encoding: gzip, deflate, braccept-language: zh-CN,zh;q=0.9content-length: '69'content-type: application/json;charset=UTF-8data-unique-id: 7b70bf17-50f2-470e-ae20-ecbbd0d6190fjwt-token: ''origin: https://mubu.comreferer: https://mubu.com/sec-ch-ua: '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"'sec-ch-ua-mobile: ?0sec-ch-ua-platform: '"Windows"'sec-fetch-dest: emptysec-fetch-mode: corssec-fetch-site: same-siteuser-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36version: 3.0.0-2.0.0.1824x-request-id: 8a061286-b850-40a2-b680-6a0314367fedjson:callbackType: 0password: $passwordphone: $phonemethod: POSTurl: https://api2.mubu.com/v3/api/user/phone_loginvalidate:-   eq:- status_code- 200-   eq:- body.code- 0

2、py文件方式

# NOTE: Generated By HttpRunner v3.1.6
# FROM: testcases\ddt\login_ddt_csv.ymlimport pytest
from httprunner import Parametersfrom httprunner import HttpRunner, Config, Step, RunRequest, RunTestCaseclass TestCaseLoginDdtCsv(HttpRunner):@pytest.mark.parametrize("param", Parameters({"phone-password": "${P(testdata/userpwd.csv)}"}))def test_start(self, param):super().test_start(param)config = Config("登录幕布").verify(False)teststeps = [Step(RunRequest("/v3/api/user/phone_login").post("https://api2.mubu.com/v3/api/user/phone_login").with_headers(**{"accept": "application/json, text/plain, */*","accept-encoding": "gzip, deflate, br","accept-language": "zh-CN,zh;q=0.9","content-length": "69","content-type": "application/json;charset=UTF-8","data-unique-id": "7b70bf17-50f2-470e-ae20-ecbbd0d6190f","jwt-token": "","origin": "https://mubu.com","referer": "https://mubu.com/","sec-ch-ua": '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',"sec-ch-ua-mobile": "?0","sec-ch-ua-platform": '"Windows"',"sec-fetch-dest": "empty","sec-fetch-mode": "cors","sec-fetch-site": "same-site","user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36","version": "3.0.0-2.0.0.1824","x-request-id": "8a061286-b850-40a2-b680-6a0314367fed",}).with_json({"callbackType": 0, "password": "$password", "phone": "$phone"}).validate().assert_equal("status_code", 200).assert_equal("body.code", 0)),]if __name__ == "__main__":TestCaseLoginDdtCsv().test_start()

这篇关于4、httprunner参数化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Linux内核参数配置与验证详细指南

《Linux内核参数配置与验证详细指南》在Linux系统运维和性能优化中,内核参数(sysctl)的配置至关重要,本文主要来聊聊如何配置与验证这些Linux内核参数,希望对大家有一定的帮助... 目录1. 引言2. 内核参数的作用3. 如何设置内核参数3.1 临时设置(重启失效)3.2 永久设置(重启仍生效

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

SpringBoot利用@Validated注解优雅实现参数校验

《SpringBoot利用@Validated注解优雅实现参数校验》在开发Web应用时,用户输入的合法性校验是保障系统稳定性的基础,​SpringBoot的@Validated注解提供了一种更优雅的解... 目录​一、为什么需要参数校验二、Validated 的核心用法​1. 基础校验2. php分组校验3

一文带你了解SpringBoot中启动参数的各种用法

《一文带你了解SpringBoot中启动参数的各种用法》在使用SpringBoot开发应用时,我们通常需要根据不同的环境或特定需求调整启动参数,那么,SpringBoot提供了哪些方式来配置这些启动参... 目录一、启动参数的常见传递方式二、通过命令行参数传递启动参数三、使用 application.pro

基于@RequestParam注解之Spring MVC参数绑定的利器

《基于@RequestParam注解之SpringMVC参数绑定的利器》:本文主要介绍基于@RequestParam注解之SpringMVC参数绑定的利器,具有很好的参考价值,希望对大家有所帮助... 目录@RequestParam注解:Spring MVC参数绑定的利器什么是@RequestParam?@

SpringBoot接收JSON类型的参数方式

《SpringBoot接收JSON类型的参数方式》:本文主要介绍SpringBoot接收JSON类型的参数方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、jsON二、代码准备三、Apifox操作总结一、JSON在学习前端技术时,我们有讲到过JSON,而在

JAVA虚拟机中 -D, -X, -XX ,-server参数使用

《JAVA虚拟机中-D,-X,-XX,-server参数使用》本文主要介绍了JAVA虚拟机中-D,-X,-XX,-server参数使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录一、-D参数二、-X参数三、-XX参数总结:在Java开发过程中,对Java虚拟机(JVM)的启动参数进