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 ...
随机推荐
- Redis Replication & Sentinel
实践目标: Redis Replication 一主:192.168.1.104 双从:192.168.1.101 192.168.1.103 Sentinel:192.168.1.102 系统环境: ...
- Odoo Documentation : Fields
Fields Basic fields class openerp.fields.Field(string=None, **kwargs) The field descriptor contains ...
- Python3.5 安装 & hello world
1.下载安装python https://www.python.org/downloads/release/python-364/ 2.安装成功运行 python shell 3.或者cmd => ...
- php对输入的检测
$data['value'] = trim(stripslashes(htmlspecialchars_decode($value)));
- 组件:组合slot
<!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...
- IDEA快速定位错误快捷键
- 《DSP using MATLAB》Problem 7.33
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
- matlab实现一次性实现多个文件夹图片转化为.mat文件
%这里是主函数:命名为readImg.m; clc;clear; %---read_image; filepath = 'G:\人脸重建\data\src_all\';%图片路径可以根据自己需要修改; ...
- 手写Function.bind函数
if(!Function.prototype.bind){ Function.prototype.bind = function(oThis){ if(typeof this !=="fun ...
- LUOGU P1978 集合
题目描述 集合是数学中的一个概念,用通俗的话来讲就是:一大堆数在一起就构成了集合.集合有如 下的特性: •无序性:任一个集合中,每个元素的地位都是相同的,元素之间是无序的. •互异性:一个集合中,任何 ...