import pytest
from api_pytest.common.yaml_util import Yamlutil
class Test_Login:
@pytest.mark.parametrize('args',['肖战','王一博','陈情令'])
def test_login(self,args):
print(args)
@pytest.mark.parametrize('name,age',[['肖战',30],['路飞',16]])
def test_plan(self,name,age):
print(name,age)
@pytest.mark.parametrize('caseinfo',Yamlutil().read_testcases_yaml('../data_yaml/user_pas.yaml'))
def test_get_usr(self,caseinfo):
print(caseinfo)
if __name__=="__main__":
pytest.Test_Login()
其中yaml文件读取代码:
import os
import yaml
from api_pytest.controller.log import loger
class Yamlutil():
def read_extract_yaml(self,key):
# 打开当前工作目录下的extract.yaml文件,以只读模式打开
with open(os.getcwd()+'/extract.yaml', mode='r', encoding='utf-8') as file:
value = yaml.load(stream=file,Loader=yaml.FullLoader)
loger.info("读取yaml文件中"+key)
return value[key];
def write_extra_yaml(self,data):
with open(os.getcwd()+'/extract.yaml', mode='a',encoding='utf-8') as file:
yaml.dump(data=data,stream=file,allow_unicode=True)
loger.info("写入yaml文件中"+data)
def clear_extract_yaml(self):
with open(os.getcwd()+'/extract.yaml', mode='w',encoding='utf-8') as file:
file.truncate()
loger.info("清空yaml数据")
def read_testcases_yaml(self,yaml_name):
# 打开当前工作目录下的extract.yaml文件,以只读模式打开
with open(yaml_name, mode='r', encoding='utf-8') as file:
value = yaml.load(stream=file,Loader=yaml.FullLoader)
loger.info("读取yaml文件中")
return value;
if __name__=="__main__":
data={"key8n":"肖战"}
Yamlutil().write_extra_yaml(data)
a=Yamlutil().read_extract_yaml("key5n")
print(a)
注意哦:读取文件的路径不要搞错了。后续还可能会修改。
数据驱动还有Excel文件,后续更新
补昨天的代码