mirror of https://gitee.com/a529548204/apitest.git
增加验证关联 支持多接口结果验证
This commit is contained in:
parent
f1c7c5b29c
commit
f789fd4e02
25
README.md
25
README.md
|
@ -21,25 +21,28 @@
|
|||
"Content-Type": "application/json"
|
||||
}
|
||||
data: {
|
||||
"username": "finsiot",
|
||||
"password": "finsiot"
|
||||
"username": "name",
|
||||
"password": "*****"
|
||||
}
|
||||
assert:
|
||||
jsonpath:
|
||||
- {
|
||||
"path": "$.msg",
|
||||
"value": "Success.",
|
||||
"asserttype": "=="
|
||||
"path": "$.data.expense_trend[0].peak_hour.peak_hour",
|
||||
"value": "$json($.data.trend[0].data.peak_hour)$",
|
||||
"asserttype": "==",
|
||||
"relevance": "web电费统计"
|
||||
}
|
||||
- {
|
||||
"path": "$.code",
|
||||
"value": 0,
|
||||
"asserttype": "=="
|
||||
"asserttype": "==",
|
||||
"relevance":
|
||||
}
|
||||
- {
|
||||
"path": "$.data.id",
|
||||
"value": 196,
|
||||
"asserttype": "=="
|
||||
"asserttype": "==",
|
||||
"relevance":
|
||||
}
|
||||
sqlassert:
|
||||
- {
|
||||
|
@ -63,8 +66,14 @@
|
|||
host: 'host'
|
||||
address: '/v1/apps/login/'
|
||||
method: 'post
|
||||
relevance: true # 关联验证
|
||||
|
||||
|
||||
###关联验证
|
||||
多接口
|
||||

|
||||
多接口结果合并成一个字典传给 third_datas
|
||||
单接口
|
||||
两种方法 同多接口类似 或者直接传结果给third_data
|
||||
### 其他
|
||||
jsonpath断言中value支持根据正则关联其他接口数据
|
||||

|
||||
|
|
|
@ -44,6 +44,7 @@ class apiSend(object):
|
|||
"""
|
||||
|
||||
url = str(self.http_type) + "://" + host_manage(hos='${{{}}}$'.format(host)) + address
|
||||
logging.info("请求地址:%s" % "" + url)
|
||||
logging.info("请求地址:%s" % "" + str(address))
|
||||
logging.info("请求头: %s" % str(header))
|
||||
|
||||
|
|
|
@ -69,9 +69,10 @@ def assert_json(data, key=None, value=None, asserttype="KEY"):
|
|||
logging.error("断言类型错误, 请选择断言类型.")
|
||||
|
||||
|
||||
def assert_text(hope_res, real_res,third_data=None):
|
||||
def assert_text(hope_res, real_res,third_data=None,third_datas=None):
|
||||
"""
|
||||
文本判断
|
||||
:param third_datas:
|
||||
:param third_data:
|
||||
:param hope_res: 期望结果
|
||||
:param real_res: 实际结果
|
||||
|
@ -83,12 +84,21 @@ def assert_text(hope_res, real_res,third_data=None):
|
|||
r_res = jsonpath.jsonpath(real_res, h_res["path"])[0]
|
||||
if h_res["asserttype"] == "==":
|
||||
try:
|
||||
h_res["value"] = replace_random(str(h_res["value"]),res=third_data)
|
||||
with allure.step("json断言判断相等"):
|
||||
allure.attach(name="期望结果", body=str(h_res))
|
||||
allure.attach(name='实际实际结果', body=str(r_res))
|
||||
assert str(r_res) == str(h_res["value"])
|
||||
logging.info("json断言通过, 期望结果{0}, 实际结果{1}".format(h_res, r_res))
|
||||
if not third_datas:
|
||||
h_res["value"] = replace_random(str(h_res["value"]),res=third_data)
|
||||
with allure.step("json断言判断相等"):
|
||||
allure.attach(name="期望结果", body=str(h_res))
|
||||
allure.attach(name='实际实际结果', body=str(r_res))
|
||||
assert str(r_res) == str(h_res["value"])
|
||||
logging.info("json断言通过, 期望结果{0}, 实际结果{1}".format(h_res, r_res))
|
||||
elif third_datas:
|
||||
if h_res["relevance"]:
|
||||
h_res["value"] = replace_random(str(h_res["value"]), res=third_datas[h_res["relevance"]])
|
||||
with allure.step("json断言判断相等"):
|
||||
allure.attach(name="期望结果", body=str(h_res))
|
||||
allure.attach(name='实际实际结果', body=str(r_res))
|
||||
assert str(r_res) == str(h_res["value"])
|
||||
logging.info("json断言通过, 期望结果{0}, 实际结果{1}".format(h_res, r_res))
|
||||
except AssertionError:
|
||||
logging.error("json断言未通过, 期望结果{0}, 实际结果{1}".format(h_res, r_res))
|
||||
raise
|
||||
|
@ -166,9 +176,18 @@ def assert_sql(hope_res, real_res):
|
|||
db.close()
|
||||
|
||||
|
||||
def asserting(hope_res, real_res, re_time=None,third_data=None):
|
||||
def asserting(hope_res, real_res, re_time=None,third_data=None,third_datas=None):
|
||||
"""
|
||||
|
||||
:param hope_res: 期望结果
|
||||
:param real_res: 实际结果
|
||||
:param re_time: 实际响应时间
|
||||
:param third_data: 依赖数据
|
||||
:param third_datas: 依赖数据组
|
||||
:return:
|
||||
"""
|
||||
if hope_res["jsonpath"]:
|
||||
assert_text(hope_res, real_res,third_data)
|
||||
assert_text(hope_res, real_res,third_data,third_datas)
|
||||
if hope_res["sqlassert"]:
|
||||
assert_sql(hope_res, real_res)
|
||||
if hope_res["time"]:
|
||||
|
|
|
@ -18,11 +18,12 @@ def getFilePathList(path, filetype):
|
|||
return pathList
|
||||
|
||||
|
||||
def write_case(_file):
|
||||
def write_case(_file,pagename):
|
||||
path = dir_manage(dir_manage('${pro_dir}$') + dir_manage('${test_suite}$') + dir_manage('${page_dir}$'))
|
||||
|
||||
with open(path + r"saasApp_pages_1.py", 'w+', encoding='utf-8') as f:
|
||||
f.write("""# coding:utf-8\nfrom test_suite.page import *\n\nurlData = ini_yaml("urlData.yml")\n\n""")
|
||||
with open(path + r"{}".format(pagename), 'w+', encoding='utf-8') as f:
|
||||
# with open(path + r"saasApp_pages_1.py", 'w+', encoding='utf-8') as f:
|
||||
# f.write("""# coding:utf-8\nfrom test_suite.page import *\n\nurlData = ini_yaml("urlData.yml")\n\n""")
|
||||
f.write("""# coding:utf-8\nfrom test_suite.page import *\n\nurlData = ini_yaml("{}")\n\n""".format(_file))
|
||||
yml_data = ini_yaml(_file)
|
||||
|
||||
for item in yml_data.items():
|
||||
|
@ -36,11 +37,22 @@ def {testtitle}(casedata):""".format(testtitle=item[0]))
|
|||
"""
|
||||
logging.info("{}".format(casedata["info"]))
|
||||
res, restime = apisend(host=data["host"], address=data["address"], method=data["method"], headers=casedata["headers"],
|
||||
data=casedata["data"])
|
||||
return res, restime\n\n"""
|
||||
data=casedata["data"])"""
|
||||
|
||||
)
|
||||
if item[1]["relevance"]:
|
||||
f.write(
|
||||
"""
|
||||
return {casedata["info"]:res}, restime\n\n"""
|
||||
)
|
||||
else :
|
||||
f.write(
|
||||
"""
|
||||
return res, restime\n\n"""
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
ym_path = r'urlData.yml'
|
||||
write_case(ym_path)
|
||||
ym_path = r'thirdUrl.yml'
|
||||
pagenames = "third_pages_1.py"
|
||||
write_case(ym_path,pagenames)
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
# coding:utf-8
|
||||
"""
|
||||
@author: 井松
|
||||
@contact: 529548204@qq.com
|
||||
@file: third_pages.py
|
||||
@time: 2021/9/17 13:48
|
||||
"""
|
||||
from test_suite.page import *
|
||||
|
||||
urlData = ini_yaml("thirdUrl.yml")
|
||||
|
||||
|
||||
def weblogin(casedata):
|
||||
data = urlData["weblogin"]
|
||||
logging.info("{}".format(casedata["info"]))
|
||||
res, restime = apisend(host=data["host"], address=data["address"], method=data["method"], headers=casedata["headers"],
|
||||
data=casedata["data"])
|
||||
return res, restime
|
||||
|
||||
|
||||
def webalarm(casedata):
|
||||
data = urlData["webalarm"]
|
||||
logging.info("{}".format(casedata["info"]))
|
||||
|
@ -16,9 +19,19 @@ def webalarm(casedata):
|
|||
data=casedata["data"])
|
||||
return res, restime
|
||||
|
||||
|
||||
def webFees(casedata):
|
||||
data = urlData["webFees"]
|
||||
logging.info("{}".format(casedata["info"]))
|
||||
res, restime = apisend(host=data["host"], address=data["address"], method=data["method"], headers=casedata["headers"],
|
||||
data=casedata["data"])
|
||||
return res, restime
|
||||
return {casedata["info"]:res}, restime
|
||||
|
||||
|
||||
def webPower(casedata):
|
||||
data = urlData["webPower"]
|
||||
logging.info("{}".format(casedata["info"]))
|
||||
res, restime = apisend(host=data["host"], address=data["address"], method=data["method"], headers=casedata["headers"],
|
||||
data=casedata["data"])
|
||||
return {casedata["info"]:res}, restime
|
||||
|
||||
|
|
|
@ -32,22 +32,13 @@ class Test_power(object):
|
|||
def test_powerFees_Fees(self, third_login, setup_login, casedata):
|
||||
|
||||
casedata["headers"]["Authorization"] = "JWT " + setup_login["data"]["token"]
|
||||
webdata = thirdData["webFees"][0]
|
||||
webdata["headers"]["Authorization"] = "JWT " + third_login["data"]["token"]
|
||||
webres = webFees(webdata)[0]
|
||||
# 电费断言
|
||||
res, restime = powerFees(casedata)
|
||||
asserting(hope_res=casedata["assert"], real_res=res, re_time=restime,third_data=webres)
|
||||
|
||||
@allure.story("Test_powerFees")
|
||||
@pytest.mark.parametrize('casedata', paramData["powerFees"], ids=[i["info"] for i in paramData["powerFees"]])
|
||||
@pytest.mark.flaky(reruns=1, reruns_delay=1)
|
||||
@pytest.mark.run(order=1)
|
||||
def test_powerFees_Power(self, third_login, setup_login, casedata):
|
||||
casedata["headers"]["Authorization"] = "JWT " + setup_login["data"]["token"]
|
||||
webdata = thirdData["webFees"][0]
|
||||
webdata["headers"]["Authorization"] = "JWT " + third_login["data"]["token"]
|
||||
webres = webFees(webdata)[0]
|
||||
thirdData["webFees"][0]["headers"]["Authorization"] = "JWT " + third_login["data"]["token"]
|
||||
thirdData["webPower"][0]["headers"]["Authorization"] = "JWT " + third_login["data"]["token"]
|
||||
webFeesRes = webFees(thirdData["webFees"][0])[0]
|
||||
webPowerRes = webPower(thirdData["webPower"][0])[0]
|
||||
third_datas = {**webFeesRes,**webPowerRes}
|
||||
# 电费断言
|
||||
res, restime = powerFees(casedata)
|
||||
asserting(hope_res=casedata["assert"], real_res=res, re_time=restime, third_data=webres)
|
||||
asserting(hope_res=casedata["assert"], real_res=res, re_time=restime,third_datas=third_datas)
|
||||
|
||||
|
|
Loading…
Reference in New Issue