一、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())使用的更多相关文章

  1. pytest自动化6:pytest.mark.parametrize装饰器--测试用例参数化

    前言:pytest.mark.parametrize装饰器可以实现测试用例参数化. parametrizing 1.  下面是一个简单是实例,检查一定的输入和期望输出测试功能的典型例子 2.  标记单 ...

  2. Pytest系列(9) - 参数化@pytest.mark.parametrize

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest允许在多个级别启 ...

  3. pytest.mark.parametrize()参数化应用二,读取json文件

    class TestEnorll(): def get_data(self): """ 读取json文件 :return: """ data ...

  4. pytest.mark.parametrize()参数化的应用一

    from page.LoginPage import Loginpage import os, sys, pytest base_dir = os.path.dirname(os.path.dirna ...

  5. 5.@pytest.mark.parametrize()数据驱动

    简介: pytest.mark.parametrize 是 pytest 的内置装饰器,它允许你在 function 或者 class 上定义多组参数和 fixture 来实现数据驱动. @pytes ...

  6. [转]Python测试框架对比----unittest, pytest, nose, robot framework对比

      测试框架 什么是框架? 框架(Framework)是整个或部分系统的可重用设计,框架是用来解决代码的组织及运行控制问题的. 在我们编写自动化脚本的时候,经常需要读取配置文件,读取数据文件,发送请求 ...

  7. Python接口自动化测试框架: pytest+allure+jsonpath+requests+excel实现的接口自动化测试框架(学习成果)

    废话 最近在自己学习接口自动化测试,这里也算是完成一个小的成果,欢迎大家交流指出不合适的地方,源码在文末 问题 整体代码结构优化未实现,导致最终测试时间变长,其他工具单接口测试只需要39ms,该框架中 ...

  8. Python 自动化测试框架 unittest 和 pytest 对比

    一.用例编写规则 1.unittest提供了test cases.test suites.test fixtures.test runner相关的类,让测试更加明确.方便.可控.使用unittest编 ...

  9. python 自动化测试框架unittest与pytest的区别

    前言: 有使用过unittest单元测试框架,再使用pytest单元测试框架,就可以明显感觉到pytest比unittest真的简洁.方便很多. unittest与pytest的区别: 主要从用例编写 ...

  10. pytest.6.Parametrize Fixture

    From: http://www.testclass.net/pytest/parametrizing_fixture/ 背景 @pytest.mark.parametrize 装饰器可以让我们每次参 ...

随机推荐

  1. CC1链详解

    前言:这篇文章是对CC1的总结,个人学习,如有不对请多指教.谢谢! 环境:jdk8u71以下,因为在该jdk版本以上这个漏洞已经被修复了 下载链接:https://www.oracle.com/cn/ ...

  2. 初探富文本之CRDT协同算法

    初探富文本之CRDT协同算法 CRDT的英文全称是Conflict-free Replicated Data Type,最初是由协同文本编辑和移动计算而发展的,现在还被用作在线聊天系统.音频分发平台等 ...

  3. mysql03-默认的几个数据库

    https://blog.csdn.net/dj673344908/article/details/80482844 1.查看mysql默认的数据库 在安装好mysql后,登录mysql,执行语句:s ...

  4. 2.5.scrollView和swiper组件的使用

    # scroll-view 可滚动视图区域.用于区域滚动. 需注意在webview渲染的页面中,区域滚动的性能不及页面滚动. 属性说明 属性名 类型 默认值 说明 平台差异说明 scroll-x Bo ...

  5. UBUNTU16.04卸载安装protobuf

    1.卸载 sudo apt-get remove libprotobuf-dev which protoc 然后删除路径即可 2.安装 sudo apt-get install autoconf au ...

  6. rt-thread模糊到清晰系列: timer.c

    #include <rtthread.h> #include <rthw.h> /* hard timer list */ static rt_list_t rt_timer_ ...

  7. echarts的颜色渐变

    官网文档解释 // 线性渐变,前四个参数分别是 x0, y0, x2, y2, //范围从 0 - 1,相当于在图形包围盒中的百分比, //如果 global 为 `true`,则该四个值是绝对的像素 ...

  8. 【KAWAKO】在windows上用CMake和MinGW编译c++工程

    目录 安装CMake 安装MinGW 编写CMakeLists.txt 编译一条龙 安装CMake 在网上随便找个教程照着安装就行了,不再赘述. 安装MinGW 参考这篇博客.从MinGW官网下载的安 ...

  9. Vulhub 漏洞学习之:Docker

    Vulhub 漏洞学习之:Docker 目录 Vulhub 漏洞学习之:Docker 1 docker daemon api 未授权访问漏洞 1.1 漏洞利用过程 1 docker daemon ap ...

  10. 昇腾AI新技能,还能预防猪生病?

    摘要:日前,由华为与武汉伯生科技基于昇腾AI合作研发的"思符(SiFold)蛋白质结构预测平台"正式推出,并成功应用于国药集团动物保健股份有限公司的猪圆环病毒疫苗研发中. 本文分享 ...