pytest fixture 利用 params参数实现用例集合
@pytest.fixture有一个params参数,接受一个列表,列表中每个数据都可以作为用例的输入。也就说有多少数据,就会形成多少用例。
如下面例子,就形成3条用例
test_parametrizing.py
import pytest seq=["case1","case2","case3"] @pytest.fixture(scope="module",params=seq)
def params(request):
return request.param def test_params(params):
print(params)
assert "case" in params
执行命令:
pytest -rA test_parametrizing.py
执行结果:

正常情况params参数不能动态取值的,如果需要动态取值,可以采用在params的序列值中放入函数的方法解决
import uuid
import pytest def duuid():
return str(uuid.uuid4()) duuids=[]
duuids.append(duuid()) @pytest.fixture(scope="function", params=duuids)
def gparams(request):
p = request.param
return p def guuid():
def _uuid():
return str(uuid.uuid4())
return _uuid @pytest.fixture(scope="function", params=[guuid()])
def funcparam(request):
p = request.param
return p() @pytest.fixture(scope="function", params=[guuid(), guuid()])
def funcparams(request):
p = request.param
return p() def test_params1(gparams):
print("test_params1:"+gparams)
#fb3dc70e-273f-4622-bedb-72b3967e9296 def test_params1_1(gparams):
print("test_params1_1:"+gparams)
#fb3dc70e-273f-4622-bedb-72b3967e9296 def test_params2(funcparam):
print("test_params2:"+funcparam)
#92bc5bf6-0dbe-44d0-b402-29f57971949e def test_params2_2(funcparam):
print("test_params2:" + funcparam)
#2d42e226-47f1-47d5-9561-557ab2a1115e def test_params3(funcparams):
print("test_params3:"+funcparams)
#05e9e475-fb6e-4a02-8f47-dd14bdea53f4
#520bd717-7d95-4281-94f7-d7ba45f88db1
在用例test_params2和test_params2_2的执行结果中可以看到,在scope为function的情况下,两条用例获取到不同的UUID值。
pytest fixture 利用 params参数实现用例集合的更多相关文章
- 求任意长度数组的最大值(整数类型)。利用params参数实现任意长度的改变。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Pytest fixture及conftest详解
前言 fixture是在测试函数运行前后,由pytest执行的外壳函数.fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集.配置测试前系统的初始状态.为批量测试提供数据源等 ...
- pytest文档28-重复执行用例(pytest-repeat)
前言 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来. 自动化运行用例时候,也会出现偶然的bug,可以针对单个用例, ...
- Pytest学习笔记11-重复执行用例插件pytest-repeat
前言 我们在平时做测试的时候,经常会遇到一些偶现的bug,通常我们会多次执行来复现此类bug,那么在自动化测试的时候,如何多次运行某个或某些用例呢,我们可以使用pytest-repeat这个插件来帮助 ...
- pytest--fixture基本使用(主要用来进行测试环境的初始化和清理,fixture中的params参数还可以用来进行参数化)
fixture fixture修饰器来标记固定的工厂函数,在其他函数,模块,类或整个工程调用它时会被激活并优先执行,通常会被用于完成预置处理和重复操作. 方法: fixture(scope=" ...
- Pytest测试框架(三):pytest fixture 用法
xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...
- Pytest(6)重复运行用例pytest-repeat
前言 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来. 自动化运行用例时候,也会出现偶然的bug,可以针对单个用例, ...
- 【又长见识了】函数传参,params参数,ref和out参数详解
一.原来函数这样传参 先看一个函数和函数调用. static void Main(string[] args) { ; Test(num);//局部变量在使用之前赋值 //Test(10); //直接 ...
- pytest文档47-allure报告添加用例失败截图
前言 使用 selenium 做 web 自动化的时候,很多小伙伴希望用例失败的时候能截图,把异常截图展示到allure报告里面. pytest 有个很好的钩子函数 pytest_runtest_ma ...
随机推荐
- Hbase实验:java创建和删除table
开启zookeeper.hadoop.hbase: 打开eclipse创一个java project,然后导入所需jar包: 写好java代码,运行create,然后去hbase shell里查看: ...
- Leetcode134. Gas Station加油站
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其中的一个加 ...
- grant
# 添加超级用户 grant all privileges on *.* to 'dump_tmp'@'10.10.10.10' identified by 'dump_tmp'; grant all ...
- vue.js_03_vue.js的样式和修饰符
1.vue.js的样式 <body> <div id="app"> <h1 :style="styleObj1">这是一个h ...
- js匿名函数与闭包作用
http://www.jb51.net/article/79238.htm 1 闭包允许内层函数引用父函数中的变量,但是该变量是最终值 当mouseover事件调用监听函数时,首先在匿名函数( fun ...
- PHP--时间搜索插件封装
/** * 时间搜索插件封装 * anthor qinpeizhou * @param $timeset 时间格式 * @param $time sql语句中所需要搜索的time字段名称 * @par ...
- 关于UML类图的一点理解(转)
首先我们定义一个非常简单的Person类,代码如下: public class Person { private String name; private int age =1; public Str ...
- Python基础-列表、元组、字典、字符串(精简解析),全网最齐全。
一.列表 =====================================================1.列表的定义及格式: 列表是个有序的,可修改的,元素用逗号隔开,用中括号包围的序列 ...
- 浅谈Python小数据池
什么是小数据池 小数据池是python中提高效率的一种方式,固定数据类型的相同值使用同一内存地址. id 用于获取开辟空间的内存地址 代码块 一个文件,一个模块,一个函数,一个类,终端中的每一行代码都 ...
- HDFS数据读写过程