pytest数据参数化
用法 @pytest.mark.parametrize(argnames,argvalues)
argnames:要参数化的变量,string(逗号分割),list,tuple
argvalues:参数化的值,list[list],list[tuple],tuple[list],tuple[tuple]
举例
data.yaml 文件
-
- 10
- 20
-
- 30
- 40
python文件
import pytest
import yaml
@pytest.mark.parametrize("a,b", [(1, 2), (3, 4)])
def test_param(a, b):
print(a+b)
@pytest.mark.parametrize(['a', 'b'], ([5, 6], [7, 8]))
def test_param_2(a, b):
print(a + b)
@pytest.mark.parametrize(('a', 'b'), [[9, 10], [11, 12]])
def test_param_3(a, b):
print(a + b)
@pytest.mark.parametrize(('a', 'b'), yaml.safe_load(open('data.yaml')))
def test_param_4(a, b):
print(a + b)
对应结果
test_python_file.py .3
.7
.11
.15
.19
.23
.30
.70