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 装饰器可以让我们每次参 ...
随机推荐
- Html音频播放代码
页面代码: <html> <head> <script src="https://code.jquery.com/jquery-3.1.1.min.js&quo ...
- Backbone 网络-ResNet 论文解读
摘要 残差网络(ResNet)的提出是为了解决深度神经网络的"退化"(优化)问题. 有论文指出,神经网络越来越深的时候,反传回来的梯度之间的相关性会越来越差,最后接近白噪声.即更深 ...
- 基于SpringBoot实现操作GaussDB(DWS)的项目实战
摘要:本文就使用springboot结合mybatis plus在项目中实现对GaussDB(DWS)的增删改查操作. 本文分享自华为云社区<基于SpringBoot实现操作GaussDB(DW ...
- 【KAWAKO】docker暴力上手
目录 从docker hub拉取镜像 根据镜像创建容器,同时把本地目录挂载到容器 进入容器 停止容器 删除停止的容器 从docker hub拉取镜像 进入docker hub,搜索自己喜欢的镜像. 复 ...
- 单例 Bean 的线程安全问题
最近面试遇到一个问题:单例 Bean 的线程安全问题怎么解决的. 之前了解但是没有深究它的解决方法.大部分时候我们并没有在项目中使用多线程,所以很少有人会关注这个问题. 大部分 Bean 实际都是无状 ...
- 一文详解ATK Loss论文复现与代码实战
摘要:该方法的主要思想是使用数值较大的排在前面的梯度进行反向传播,可以认为是一种在线难例挖掘方法,该方法使模型讲注意力放在较难学习的样本上,以此让模型产生更好的效果. 本文分享自华为云社区<AT ...
- Spring整合Mybatis原理
Spring整合Mybatis原理 目录 Spring整合Mybatis原理 1.@MapperScan注解发挥作用 1.1.导入MapperScannerRegistrar类 1.1.2.执行Imp ...
- 把逗号分隔的String字符串转List<Integer>
把逗号分隔的String字符串转int集合 集合或数组转变为逗号分隔的字符串的几种方式 1.自己编码实现 2.org.apache.commons.lang3.StringUtils 3.String ...
- VUE学习-插槽
插槽 匿名插槽 子组件设置匿名插槽 <script type="text/x-template" id="custom-comp"> <div ...
- neo4j与jdk版本【转】
根据官网的介绍,目前应该是只有这三个版本相当于主流. Version3.5 使用java1.8 Version4.0+ 官方人员说了目前只支持JDK11 Neo4j Desktop 桌面版本的已经包括 ...