当前位置: 首页 > news >正文

pytest二次开发:生成用例参数

@pytest.fixture是一个装饰器,用于声明一个fixture。Fixture是pytest中的一个核心概念,它提供了一种将测试前的准备代码(如设置测试环境、准备测试数据等)和测试后的清理代码(如恢复测试环境、删除临时文件等)与测试用例分离的方法。通过这种方式,测试用例可以更加专注于测试逻辑本身,而不是测试环境的准备和清理。

pytest使用fixture,实现接受一个函数作为输入,函数内部可以包含条件表达式、循环等复杂逻辑,并返回一个参数列表


import pytest# 直接传入数组
@pytest.fixture(params=[1,2,3])
def param_complex(request):print('--------fixture\t')print(request)print(request.param)return request.param
def test_generatePara(param_complex):print('---------param_complex------- test\t')print(param_complex)assert Truedef generate_param(a, b):params = []ouShu = []jiShu = []for i in range(a,b):if i % 2 == 0:ouShu.append(i)else:jiShu.append(i)params.append(ouShu)params.append(jiShu)return params#  01 函数执行:generate_param(2,6)在pytest解析fixture声明时执行,生成一个包含多个pytest.param对象的列表
#  02 参数化:测试用例使用param_complex_gene fixture ,pytest将pytest.param对象的列表作为params的值,并为列表中的每个pytest.param对象执行一次测试用例
#  03 fixture函数:对于列表中的每个pytest.param对象,pytest都会调用param_complex_gene fixture函数一次,并将当前的pytest.param对象作为request.param传递给这个函数。然后,param_complex_gene函数返回request.param,即当前的测试参数。
@pytest.fixture(params=generate_param(2,6))
def param_complex_gene(request):print('--------fixture param_complex_gene \t')print(request)print(request.param)return request.paramdef test_generatePara1(param_complex_gene):print('---------param_complex_gene------- test\t')print(param_complex_gene)assert True

--------fixture    
<SubRequest 'param_complex' for <Function test_generatePara[1]>>
1
PASSED                        [ 20%]---------param_complex------- test    
1
--------fixture    
<SubRequest 'param_complex' for <Function test_generatePara[2]>>
2
PASSED                        [ 40%]---------param_complex------- test    
2
--------fixture    
<SubRequest 'param_complex' for <Function test_generatePara[3]>>
3
PASSED                        [ 60%]---------param_complex------- test    
3
--------fixture param_complex_gene     
<SubRequest 'param_complex_gene' for <Function test_generatePara1[param_complex_gene0]>>
[2, 4]
PASSED     [ 80%]---------param_complex_gene------- test    
[2, 4]
--------fixture param_complex_gene     
<SubRequest 'param_complex_gene' for <Function test_generatePara1[param_complex_gene1]>>
[3, 5]
PASSED     [100%]---------param_complex_gene------- test    
[3, 5]

======= Global cleanup =======
 

http://www.lryc.cn/news/434011.html

相关文章:

  • 想抹黑华为的 请换一种方式
  • 学习学习学习
  • requestAnimationFrame原理和使用
  • 线程的状态(java)
  • Linux IO模型:IO多路复用
  • [数据集][目标检测]电梯内广告牌电动车检测数据集VOC+YOLO格式2787张4类别
  • MATLAB下载详细教程及下载链接
  • 利用发电量和气象数据分析来判断光伏仿真系统的准确性
  • Model-based RL动态规划(基于价值、基于策略,泛化迭代)
  • 外接串口板,通过串口打开adb模式
  • ssm微信小程序校园失物招领论文源码调试讲解
  • iOS 15推出后利用邮件打开率的7种方法
  • 以太网--TCP/IP协议(一)
  • LeetCode刷题:找到第K大的元素
  • HTML页面配置高德地图,获取位置
  • HTTrack
  • 干货分享|分享一款微软出品的工作效率神器 PowerToys
  • 神经网络的线性部分和非线性部分
  • 微信支付开发避坑指南
  • Qt5.4.1连接odbc驱动操作达梦数据库
  • 计算机组成原理(第一课)
  • 计算机网络练级第一级————认识网络
  • Java基于微信小程序的家庭财务管理系统,附源码
  • P2343 宝石管理系统
  • Spring6梳理6——依赖注入之Setter和构造器注入
  • 【C++】C++入门基础,详细介绍命名空间,缺省参数,函数重载,引用,内联函数等
  • Android使用Room后无法找到字符BR
  • 网络通讯安全基础知识(加密+解密+验签+证书)
  • [数据集][目标检测]石油泄漏检测数据集VOC+YOLO格式6633张1类别
  • 【Oracle篇】全面理解优化器和SQL语句的解析步骤(含执行计划的详细分析和四种查看方式)(第二篇,总共七篇)