1、参数化的本质:相同的步骤,但测试数据不同,比如登录的场景

import math
import pytest

# 方式一:分离出List
def list_Test():
list = [
[2, 2, 4],
[2, 3, 8],
[1, 9, 1],
[0, 9, 0],
]
return list
@pytest.mark.parametrize('a,b,expect',list_Test())
def test_add_one(a,b,expect):
# 断言 -- 某数的平方=expect对象
assert math.pow(a,b) == expect

# 方式二:分离出tuple
def tuple_Test():
tuple = [
(2, 2, 4),
(2, 3, 8),
(1, 9, 1),
(0, 9, 0),
]
return tuple
@pytest.mark.parametrize('a1,b1,expect1',tuple_Test())
def test_add_two(a1,b1,expect1):
# 断言 -- 某数的平方=expect对象
assert math.pow(a1,b1) == expect1

# 方式三:分离出dict
def dict_Test():
dict = [
{'a': 2, 'b': 2, 'expect': 4},
{'a': 2, 'b': 3, 'expect': 8},
{'a': 2, 'b': 4, 'expect': 16},
{'a': 1, 'b': 9, 'expect': 1},
]
return dict
@pytest.mark.parametrize('datas',dict_Test())
def test_add_three(datas):
# 断言 -- 某数的平方=expect对象
assert math.pow(datas['a'],datas['b']) == datas['expect']

# 方式四: 使用pytest.param方法进行分离
def param_Test():
param = [
pytest.param(1, 1, 1,id='one'),
pytest.param(2, 2, 4,id='two'),
pytest.param(3, 3, 27,id='three')
]
return param
# ids参数默认为None,用于定义测试用例的名称
@pytest.mark.parametrize('a,b,expect',param_Test())
def test_add_four(a,b,expect):
# 断言 -- 某数的平方=expect对象
assert math.pow(a,b) == expect

if __name__ == '__main__':
pytest.main(['-v','-s','test_mark.param.py'])

运行代码结果如下:

2、固件request

在Pytest的测试框架中,参数化也会使⽤到pytest内置的固件request,通过request.param来获取参数,对上面的案例代码进行修改,fixture参数列表中request也是内建fixture
import pytest
import math

def datas():
dict_param = [
{'a': 2, 'b': 2, 'expect': 4},
{'a': 2, 'b': 3, 'expect': 8},
{'a': 2, 'b': 4, 'expect': 16},
]
return dict_param

@pytest.fixture(params=datas())
def getParams(request):
return request.param

def test_math_pow(getParams):
# 断言 -- 某数的平方=expect对象
assert math.pow(getParams['a'],getParams['b']) == getParams['expect']

if __name__ == '__main__':
pytest.main(['-v','-s','test_mark.param.py'])

代码运行结果如下:

最新文章

  1. .NET Socket TCP 50W在线连接交互测试
  2. 一台电脑安装两个xampp的方法
  3. linux 第一次获得root权限
  4. Eclipse中的TreeViewer类和ListViewer类
  5. ExtJs中处理时间,出现NaN-NaN-NaN的解决方式
  6. Python开源异步并发框架
  7. cvc-complex-type.2.4.c: The matching wildcard...
  8. tp框架---验证码详解
  9. 查询数据库游标使用情况以及sql
  10. 剑指offer二之替换空格
  11. Django 高并发负载均衡
  12. php中点击网页不跳转执行程序
  13. Exp4 恶意代码分析 20164321 王君陶
  14. laydata 点击日期闪现
  15. [Javascript] Closure Cove, Common mistake
  16. 4.rabbitmq 路由
  17. 2007: [Noi2010]海拔
  18. python字符串排序方法
  19. 如何开启Apache Rewrite功能
  20. 牛叉之nc命令

热门文章

  1. Geo-CNN的三维点云
  2. 为什么edge AI是一个无需大脑的人
  3. python工业互联网应用实战18—前后端分离模式之jquery vs vue
  4. 「题解」HDU-4015 Mario and Mushrooms
  5. HashMap源码解析和设计解读
  6. 【C++】共用体\联合体(union)
  7. Java必学MySQL数据库应用场景
  8. 『心善渊』Selenium3.0基础 — 2、Selenium测试框架环境搭建(Windows)
  9. 『无为则无心』Python基础 — 11、Python中的数据类型转换
  10. Kubernetes之job