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 装饰器可以让我们每次参 ...
随机推荐
- react 高效高质量搭建后台系统 系列 —— 表格的封装
其他章节请看: react 高效高质量搭建后台系统 系列 表格 有一种页面在后台系统中比较常见:页面分上下两部分,上部分是 input.select.时间等查询项,下部分是查询项对应的表格数据.包含增 ...
- FreeBSD 安装 fcitx5的配置
link: Chinese Pinyin Package for typing Chinese sudo pkg install -y zh-CJKUnifonts sudo pkg install ...
- day07-SpringMVC底层机制简单实现-03
SpringMVC底层机制简单实现-03 https://github.com/liyuelian/springmvc-demo.git 7.任务6-完成控制器方法获取参数-@RequestParam ...
- Solon2 开发之插件,二、插件扩展机制(Spi)
插件扩展机制,是基于 "插件" + "配置申明" 实现的解耦的扩展机制(类似 Spring Factories.Java Spi):简单.弹性.自由.它的核心作 ...
- Containers feature is disabled. Enable it using the PowerShell script (in an administrative PowerShe
1.问题如题如下图 2.解决办法 以管理员身份运行Window Terminal(Windows PowerShell),输入图上下方所示命令 Enable-WindowsOptionalFeatur ...
- 【TS】函数和函数类型
在使用函数的时候,通常会给函数传值,或者给函数一个返回值调用,这个时候就会涉及到函数类型. 函数类型分为两个方面: 1.函数参数 2.函数返回值 语法: function 函数名( 参数 : 参数类型 ...
- 详解神经网络基础部件BN层
摘要:在深度神经网络训练的过程中,由于网络中参数变化而引起网络中间层数据分布发生变化的这一过程被称为内部协变量偏移(Internal Covariate Shift),而 BN 可以解决这个问题. 本 ...
- pytest基本使用指南
注:关于pytest的基础要点个人已经总结好了, 一. 启动: 在主函数main下启动:pytest.main(['-q','test_demo.py']) 在命令行启动:pytest ./test_ ...
- 了解舵机以及MG996R的控制方法
了解舵机以及MG996R的控制方法 1.舵机基础知识: 舵机是遥控航空.航天模型控制动作,改变方向的重要组成部件,舵机是一种位置(角度)伺服的驱动器. 舵机主要适用于那些需要角度不断变化并可以保持的控 ...
- CF1372D Omkar and Circle
题目传送门 思路 这是一道非常简单的 \(\mathcal *2100\). 既然他样例给的那么简单,说明这是一道结论题. 于是我们可以手玩几组数据试试. 例如 \(3,5,9,8,12\) 这组,发 ...