python+接口参数化(ddt和pytest.mark.parametrize())使用
一、ddt(基于unittest)
实例:字典解包【{},{}】
test_data=t.read_excel(mode,case_list)
@ddt
class Interface(unittest.TestCase):
def setUp(self):
logger.info("测试用例开始了。。。。")
@data(*test_data)
@unpack
def test_case(self,id,interface,method,url,data,sql,Expected_code):#直接拆开用
#logger.info("正在执行%s条用例:"%id)
logger.info("正在执行"+"["+str(interface)+"]"+"接口"+str(id)+"条用例")
dict={}
request_result = HttpRequest().http_request(method, url, data)
二、pytest.mark.parametrize()
示例:test.py
import pytest
class TestApi:
#第一种: 列表args解包,输出多个参数
@pytest.mark.parametrize('name,age,a', [["百里", "18","19"], ["星耀", "20","21"]]) # 解包操作,输出多个参数
def test_api_1(self, name, age,a):
print(name, age)
# 第二种 字典 args解包,输出多个参数
t=[{'id': 1, 'module': '注册', 'description': '输入手机号、密码、注册名', 'method': 'get', 'url': 'http://test.lemonban.com//futureloan/mvc/api/member/register','data': {'mobilephone': '18501026696', 'pwd': '123456', 'regname': '666'},'sql': {'my_sql': 'select count(*) from member where mobilephone=%s', 'condition': '18501026696', 'result': 1, 'code': 0},'exception_code': 10001, 'exception_sql_select': 1},
{'id': 2, 'module': '登录', 'description': '输入正确手机号、密码', 'method': 'get', 'url': 'http://test.lemonban.com//futureloan/mvc/api/member/login', 'data': {'mobilephone': '18501026696', 'pwd': '123456'}, 'sql': {'my_sql': 'select count(*) from member where mobilephone=%s', 'condition': '18501026696', 'result': 1, 'code': 0}, 'exception_code': 10001, 'exception_sql_select': 1},
{'id': 3, 'module': '充值', 'description': '输入手机号,充值金额为空', 'method': 'get', 'url': 'http://test.lemonban.com//futureloan/mvc/api/member/recharge', 'data': {'mobilephone': '', 'amount': ''}, 'sql': {'my_sql': 'select LeaveAmount from member where mobilephone=%s', 'result': 'None', 'condition': '', 'code': 0}, 'exception_code': 20103, 'exception_sql_select': 'None'},
{'id': 4, 'module': '充值', 'description': '输入手机号、充值金额', 'method': 'get', 'url': 'http://test.lemonban.com//futureloan/mvc/api/member/recharge', 'data': {'mobilephone': '18501026696', 'amount': 600.0}, 'sql': {'my_sql': 'select LeaveAmount from member where mobilephone=%s', 'result': 600.0, 'condition': '18501026696', 'code': 0}, 'exception_code': 10001, 'exception_sql_select': '600.00'
}]
@pytest.mark.parametrize('caseinfo',t) # 解包操作,输出多个参数
def test_api_11(self,caseinfo):
print(caseinfo["id"],caseinfo["module"])
if __name__ == '__main__':
pytest.main(["-vs", "test.py"])
结果:

具体应用:
第一种:列表【[],[]】 args解包
test_data_list = t.read_data()
class Test_insterface:
#COOKIES=None # 全局变量
@allure.description('测试接口:采用正向和反向用例测试')
@pytest.mark.parametrize('id,module,description,method,url,data,sql,exception_code,exception_sql_select', test_data_list)
def test_case_ykt(self,id,module,description,method,url,data,sql,exception_code,exception_sql_select):
logging.info("正在运行{}接口:第{}条测试用例".format(module, id))
logging.info("测试数据:{},{},{}".format(method, url, data))
result_dict = {} #创建空字典,用于存在请求返回结果的数据
第二种:字典【{},{}】 args解包
test_data=t.read_excel(mode,case_list)
class Test_interFaceCase:
@pytest.mark.parametrize('caseinfo',test_data)#获取数据test_data的方式是字典格式,不能caseinfo数据拆开
def test_interface(self,caseinfo):
id=caseinfo["id"] #因为数据是字典格式[{}],需要拆开使用
module=caseinfo["module"]
method=caseinfo["method"]
url=caseinfo["url"]
data=caseinfo["data"] logging.info("正在运行{}接口:第{}条测试用例".format(module, id))
logging.info("测试数据:{},{},{}".format(method, url, data))
python+接口参数化(ddt和pytest.mark.parametrize())使用的更多相关文章
- pytest自动化6:pytest.mark.parametrize装饰器--测试用例参数化
前言:pytest.mark.parametrize装饰器可以实现测试用例参数化. parametrizing 1. 下面是一个简单是实例,检查一定的输入和期望输出测试功能的典型例子 2. 标记单 ...
- Pytest系列(9) - 参数化@pytest.mark.parametrize
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest允许在多个级别启 ...
- pytest.mark.parametrize()参数化应用二,读取json文件
class TestEnorll(): def get_data(self): """ 读取json文件 :return: """ data ...
- pytest.mark.parametrize()参数化的应用一
from page.LoginPage import Loginpage import os, sys, pytest base_dir = os.path.dirname(os.path.dirna ...
- 5.@pytest.mark.parametrize()数据驱动
简介: pytest.mark.parametrize 是 pytest 的内置装饰器,它允许你在 function 或者 class 上定义多组参数和 fixture 来实现数据驱动. @pytes ...
- [转]Python测试框架对比----unittest, pytest, nose, robot framework对比
测试框架 什么是框架? 框架(Framework)是整个或部分系统的可重用设计,框架是用来解决代码的组织及运行控制问题的. 在我们编写自动化脚本的时候,经常需要读取配置文件,读取数据文件,发送请求 ...
- Python接口自动化测试框架: pytest+allure+jsonpath+requests+excel实现的接口自动化测试框架(学习成果)
废话 最近在自己学习接口自动化测试,这里也算是完成一个小的成果,欢迎大家交流指出不合适的地方,源码在文末 问题 整体代码结构优化未实现,导致最终测试时间变长,其他工具单接口测试只需要39ms,该框架中 ...
- Python 自动化测试框架 unittest 和 pytest 对比
一.用例编写规则 1.unittest提供了test cases.test suites.test fixtures.test runner相关的类,让测试更加明确.方便.可控.使用unittest编 ...
- python 自动化测试框架unittest与pytest的区别
前言: 有使用过unittest单元测试框架,再使用pytest单元测试框架,就可以明显感觉到pytest比unittest真的简洁.方便很多. unittest与pytest的区别: 主要从用例编写 ...
- pytest.6.Parametrize Fixture
From: http://www.testclass.net/pytest/parametrizing_fixture/ 背景 @pytest.mark.parametrize 装饰器可以让我们每次参 ...
随机推荐
- USACO 2023 January Contest, Bronze Problem 3. Moo Operations
这道题目灰常简单,我们先从最简单的3个字符串开始 有以下几种情况: 可以看到,只有在中间是O的情况下才有可能变成MOO 辣么我们不妨在在s串中枚举这个中间 O 每枚举到一个就看看能不能用他的本身操作次 ...
- JavaScript 、三个点、 ...、点点点 是什么语法
笔者在学习ts函数式的时候见到这样的写法,这个语法是es6的扩展运算符,可以在函数调用/数组构造时, 将数组表达式或者string在语法层面展开:还可以在构造字面量对象时, 将对象表达式按key-va ...
- 6、Collections工具类
1.Collections工具类介绍 Collections 是一个操作 Set.List 和 Map 等集合的工具类 Collections 中提供了一系列静态的方法对集合元素进行排序.查询和修改等 ...
- 逆天的Zstack-工控机上测试
放假前一直在服务器上折腾Zstack跑数据库,调IOC.正好手头有个32G内存,intel i7处理器的工控机,就试试装Zstack跑跑看,想着即使重负荷的跑不了,跑跑docker之类的也行.装成功后 ...
- JZOJ 2936. 【NOIP2012模拟8.9】逐个击破
题面 各大 \(OJ\) 都有 分析 从结果入手:所有被敌方军团占领的城市都是分开的 而按最小代价删去若干条边,则剩下的图必然是若干个联通子图组成的 那么我们要使花费最小,可以是留下的边最大 并查集合 ...
- 【ACR2015】依那西普按需维持治疗策略有效抑制RA骨破坏进展
标签: 类风湿关节炎; 依那西普; 药物减停; 复发重治 对RA疾病复发患者, 依那西普按需治疗与持续足剂量治疗是否存在疗效差异? Inui K, et al. ACR 2015. Presentat ...
- Swiper第一页与最后一页禁止滑动
resistanceRatio抵抗率.边缘抵抗力的大小比例.值越小抵抗越大越难将slide拖离边缘,0时完全无法拖离. mounted: function() { let _this = this; ...
- el-select 获取change点击index
<el-select> <el-option v-for="(item, index) in optionlist" @click.native ="h ...
- LeetCode-689 三个无重叠子数组的最大和
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-sum-of-3-non-overlapping-subarrays 题目描述 ...
- vue3学习
资料: http://docs.vikingship.xyz/ 声明类型: 1.使用ref可以声明基础数据 2.使用reactive声明对象数据 3.computed可以返回一个不变的响应式 ...