其他分享
首页 > 其他分享> > 【pytest】YAML管理测试数据

【pytest】YAML管理测试数据

作者:互联网

#yaml中序列(list)
- admin
- admin2
- admin3

#yaml中键值对(字典)
#1、直接使用花括号
{"message": "update some data!", "code": 0}
#2、键值对
user: admin
pwd: 123456
# data\userinfo.yaml
-
    - "M"
    - "message": "update some data!", "code": 0
-
    - "F"
    - "message": "update some data!", "code": 0
#common\read_yaml.py
import yaml
import os

def readyml(yaml_path):
    #判断yaml文件是否存在
    if not os.path.isfile(yaml_path):
        raise FileNotFoundError('文件路径不存在,请检查路径是否正确:%s'%yaml_path)
    #读取文件
    with open(yaml_path,encoding='utf8') as f:
        yaml_data = f.read()
        data = yaml.safe_load(yaml_data)   #转换成列表、字典对象
    return data
#case\test_param.py
import pytest
import os
from common.read_yaml import readyml

#获取文件绝对路径
cur_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
yaml_path = os.path.join(cur_path,'data','userinfo.yaml')
print(yaml_path)

test_data = readyml(yaml_path)

@pytest.mark.parametrize("test_input,expect", test_data)
def test_001(test_input,expect):
    print(test_input,expect['message'])

标签:yaml,os,测试数据,YAML,pytest,import,test,path,data
来源: https://www.cnblogs.com/xwltest/p/16558066.html