yaml 格式数据驱动
python yaml
- 文档地址:https://pyyaml.org/wiki/PyYAMLDocumentation
- load() 将yaml格式的数据转换成字典结构
demo:
yaml_str = """
default: test
test/dev.com:
dev: 10.10.10.1
test: 10.10.10.2
"""
print(load(yaml_str))
- dump() 将字典结构的数据转换成yaml格式
demo:
env = {
"test/dev.com": {
"dev": "10.10.10.100",
"test": "10.10.10.200",
},
"default": "test",
}
print(dump(env))
结合 pytest 进行参数化,数据驱动
demo:
"""
array:
- 3
- 2
- 1
"""
@pytest.mark.parametrize("num", load(open("demo.yaml", "r"))["array"]) # 结合 pytest 参数化方式,从文件中读取数据
def test_yaml_by_pytest(num):
assert num > 1
数据驱动方法
- 从外部文件读取数据,例如从Excel文件读取数据,进而对用例进行参数化
- 配置化,从配置化文件中读取数据 例如test_yaml_by_pytest()中load()加载配置文件
- 关键步骤参数化