码迷,mamicode.com
首页 > 其他好文 > 详细

pytest 参数化升华版

时间:2020-06-20 21:53:04      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:列表   元组   class   范围   test   div   code   函数   参数   

1.@pytest.mark.parametrize()装饰范围

装饰类,则类中所有的测试用例都是用这组参数

装饰测试函数,只有被装饰的函数使用这组参数

 

2.@pytest.mark.parametrize() 装饰的三种方式

import pytest
test_datas = [
  (11, 22, 33),
  (22, 33, 55)
]
 
datas_dict = [
  {"a": 1, "b": 2, "c": 3},
  {"a": 11, "b": 22, "c": 33},
  {"a": 111, "b": 222, "c": 333},
]
 
# 方式一:直接写
@pytest.mark.parametrize("a, b, c", [(1, 2, 3), (4, 5, 9)])
def test_add01(a, b, c):
  res = a + b
  assert res == c
 
# 方式二:参数为列表中嵌套元组
@pytest.mark.parametrize("data", test_datas)
def test_add02(data):
  res = data[0] + data[1]
  assert res == data[2]
 
# 方式三:参数为列表中嵌套字典
@pytest.mark.parametrize("data", datas_dict)
def test_add03(data):
  res = data["a"] + data["b"]
  assert res == data["c"]

 

 

pytest 参数化升华版

标签:列表   元组   class   范围   test   div   code   函数   参数   

原文地址:https://www.cnblogs.com/bigbox/p/13170292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!