何为数据驱动
mark.parametrize实现数据驱动
被测试对象:加法计算器
def add(a, b):
return a + b
测试数据:
data:
- [ 1,2,3 ]
- [ 0.5,0.3,0.8 ]
- [ -0.1,0.1,0 ]
ids: ["int add test","float add test","int float test"]
测试用例:
import yaml
import pytest
import logging
# 被测对象
def add(a, b):
return a + b
def get_yaml():
# 打开一个文件,生成文件流对象
with open("./add_data.yaml") as f:
# 使用yaml解析文件流,生成一个Python可识别的数据类型
data = yaml.load(f)
print(data)
return data
def test_get_yaml():
get_yaml()
# 测试脚本
class TestAdd:
def setup_class(self):
pass
def teardown_class(self):
pass
@pytest.mark.add
@pytest.mark.parametrize("a, b,expect",get_yaml()["data"],ids=get_yaml()["ids"])
def test_add(self, a, b, expect):
assert expect == add(a, b)
运行结果: