pytest+yaml 数字驱动
关于Yaml的相关介绍可以参考下边连接:
YAML 入门教程 | 菜鸟教程 (runoob.com)
以下是简单的例子
实例1:
test_data.yml 文件
- - 1 - 2 - 3
测试代码
import pytest import yaml
class TestDemo():
@pytest.mark.parametrize('a, b, c', yaml.safe_load(open('./test_data.yml')))
def test_add1(self,a, b ,c):
assert a + b == c
执行结果
实例2:
test_data.yaml文件
- [1,2,3] - [2,3,5] - [3,4,7]
代码
import pytest import yaml class TestDemo(): def get_date(): with open('./test_data.yml','rb') as f: data = yaml.safe_load(f) return data @pytest.mark.parametrize('a, b, c', get_date()) def test_add(self, a, b, c): assert a + b == c
执行结果
实例3:
test_data.yml文件
datas: - [1,2,3] - ['a', 'b', 'ab'] ids: ['int','string']
代码
import pytest import yaml class TestDemo(): def get_date(): with open('./test_data.yml','rb') as f: total = yaml.safe_load(f) print(total) return total['datas'],total['ids'] @pytest.mark.parametrize('a, b, c', get_date()[0], ids=get_date()[1]) def test_add(self, a, b, c): assert a + b == c
执行结果