mirror of https://gitee.com/a529548204/apitest.git
96 lines
3.3 KiB
Python
96 lines
3.3 KiB
Python
# coding:utf-8
|
|
import os
|
|
|
|
from config.confManage import dir_manage
|
|
from scripts.readYamlFile import ini_yaml
|
|
from scripts import root_path
|
|
import time
|
|
|
|
def getFilePathList(rootpath, filetype):
|
|
filepath = []
|
|
# 获取所有文件下的子文件名称
|
|
for root, dirs, files in os.walk(rootpath):
|
|
# 过滤所有空文件
|
|
if files:
|
|
for file in files:
|
|
path = os.path.join(root, file)
|
|
# 判断只返回 yaml 的文件
|
|
if filetype in path:
|
|
filepath.append(str(path).replace("\\", "/"))
|
|
return filepath
|
|
|
|
|
|
def write_case():
|
|
testname = dir_manage('${test_name}$')
|
|
casepath = root_path + dir_manage('${test_suite}$') + dir_manage('${case_dir}$') + "/" + testname
|
|
datapath = root_path + dir_manage('${test_suite}$') + dir_manage('${data_dir}$') + "/" + testname
|
|
filepathlist = getFilePathList(datapath, ".yml")
|
|
for filepath in filepathlist:
|
|
|
|
file = str(filepath.split(datapath)[-1]) # 处理出文件名及路径
|
|
filename = str(filepath.split("/")[-1].split(".")[0]) # 测试用例名称
|
|
filedata = ini_yaml(file) # 测试数据
|
|
midp = file.split(filename + ".yml")[0] # 分层路径获取
|
|
case = casepath + midp + "test_" + filename + ".py" # 用例路径
|
|
if not os.path.exists(casepath + midp):
|
|
os.makedirs(casepath + midp)
|
|
if not os.path.exists(case):
|
|
|
|
t1 =time.strftime("%Y/%m/%d %H:%M")
|
|
with open(case, 'w', encoding='utf-8') as f:
|
|
f.write(f"""# coding:utf-8
|
|
\"\"\"
|
|
@author: jing
|
|
@contact: 529548204@qq.com
|
|
@file: test_{filename}.py
|
|
@time: {t1}
|
|
\"\"\"
|
|
from test_suite.testcase.{testname} import *
|
|
|
|
paramData = ini_yaml("{file}")
|
|
|
|
class Test_{filename}(object):""")
|
|
for item in filedata:
|
|
order = filedata[item]["order"]
|
|
|
|
f.write(f"""
|
|
@allure.story("Test_{filename}")
|
|
@pytest.mark.parametrize('casedata', paramData["{item}"]["case"],
|
|
ids=[i["info"] for i in paramData["{item}"]["case"]])
|
|
@pytest.mark.flaky(reruns=1, reruns_delay=1)
|
|
@pytest.mark.run(order={order})""")
|
|
|
|
if filedata[item]["token"]:
|
|
f.write(f"""
|
|
def test_{item}(self, casedata, setup_Login):
|
|
casedata["headers"]["{filedata[item]["token"]}"] = setup_Login""") # token类型是 Authorization
|
|
else:
|
|
f.write(f"""
|
|
def test_{item}(self, casedata):""")
|
|
f.write("""
|
|
res, restime = apisend(host=casedata["host"], address=casedata["address"], method=casedata["method"],
|
|
headers=casedata["headers"],
|
|
data=casedata["data"], rel=casedata["relevance"])
|
|
asserting(hope_res=casedata["assert"], real_res=res, re_time=restime)\n""")
|
|
|
|
|
|
# def write_case(f):
|
|
# for i in f:
|
|
#
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# ym_path = r'thirdUrl.yml'
|
|
# pagenames = "third_pages_1.py"
|
|
|
|
# write_case(ym_path,pagenames)
|
|
|
|
# ym_path = r'urlData.yml'
|
|
# pagenames = "saasWeb_pages_1.py"
|
|
write_case()
|
|
|
|
# l = getFilePathList(r"D:\apitest\test_suite\datas\saasWeb", ".yml")
|
|
# l2 = getFilePathList(r"D:\apitest\test_suite\datas\saasWeb", ".yml")
|
|
# # print(str(l[0]).split("\\")[-1])
|
|
# print(l)
|