Raul2018

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

From:  凌雪 软件测试学习笔记 2023-06-12 21:23 发表于福建

-----------------------------------------------------------------------------------

# 该框架支持两种断言方式,相等和包含。
#
# 先看一下断言的yaml文件编写规范:
#
# validate:
# - equals: {status_code: 200}
# - contains: $ddt{assert_str}
# 其中assert_str和之前用例一样,作为变量,放在对应的data yaml文件中

# DDT数据驱动yaml文件,对应的yaml测试用例文件为:user.yaml
- ['name', 'userName', 'password', 'assert_str']
- ['正确用户名密码登录成功', '*****', '******', '查询成功']
- ['用户名或密码错误', '******', '*******', '帐号或密码错误,请重新输入']
# 断言方法的封装,直接看代码

def assert_result(self, yq_result, sj_result, return_code):
"""
断言方法,支持相等断言和包含断言
:param yq_result:
:param sj_result:
:param return_code:
:return:
"""
all_flag = 0
for yq in yq_result:
for key, value in yq.items():
# print(key,value)
if key == "equals":
flag = self.equals_assert(value, return_code,sj_result)
all_flag = all_flag + flag
elif key == 'contains':
flag = self.contains_assert(value, sj_result)
all_flag = all_flag + flag
else:
# print("框架暂不支持此断言方法")
self.log.warning("框架暂不支持此断言方法")
assert all_flag == 0

def equals_assert(self, value, return_code, sj_result):
"""
相等断言
:param value:
:param return_code:
:param sj_result:
:return:
"""
flag = 0
for assert_key, assert_value in value.items():
# print(assert_key, assert_value)
if assert_key == "status_code":
assert_value == return_code
if assert_value != return_code:
flag = flag+1
# print("断言失败,返回的状态码不等于%s" % assert_value)
self.log.warning("断言失败,返回的状态码不等于%s" % assert_value)
else:
lists = jsonpath.jsonpath(sj_result, '$..%s' % assert_key)
if lists:
if assert_value not in lists:
flag = flag+1
# print("断言失败:" +assert_key+"不等于"+str(assert_value))
self.log.warning("断言失败:" +assert_key+"不等于"+str(assert_value))
else:
flag = flag +1
# print("断言失败:返回的结果不存在:" + assert_key)
self.log.warning("断言失败:返回的结果不存在:" + assert_key)
return flag

def contains_assert(self, value, sj_result):
"""
包含断言
:param value:
:param sj_result:
:return:
"""
flag = 0
if value not in str(sj_result):
flag = flag + 1
# print("断言失败:返回的结果中不包含:" + value)
self.log.warning("断言失败:返回的结果中不包含:" + value)
return flag

# self.log日志相关的方法,不在本节展开,下次另外单独详细介绍。
#
# 然后在standard_yaml方法中调用断言方法:

def standard_yaml(self, caseinfo):
"""
规范yaml测试用例
:param caseinfo:
:return:
"""
caseinfo_keys = caseinfo.keys()
# 判断一级关键字是否包含:name,request,validate
if "name" in caseinfo_keys and "request" in caseinfo_keys and "validate" in caseinfo_keys:
# 判断request下面是否包含:method、url
request_keys = caseinfo["request"].keys()
if "method" in request_keys and "url" in request_keys:
# print("yaml基本结构检查通过")
# pop函数用于移除列表中的一个元素,并且返回该元素的值
method = caseinfo['request'].pop("method")
# print("打印出method % s" % method)
url = caseinfo['request'].pop('url')
# print("打印出url % s" % url)
res = self.send_request(method, url, **caseinfo['request']) # caseinfo需要解包加**
# print("打印出res % s" % res)
return_text = res.text
return_code = res.status_code
return_json = ""
try:
return_json = res.json()
except Exception as e:
print("extract返回的结果不是JSON格式")
# 提取值并写入extract.yaml文件
if "extract" in caseinfo.keys():
for key, value in caseinfo["extract"].items():
if "(.*?)" in value or "(.+?)" in value: # 正则表达式
zz_value = re.search(value, return_text)
if zz_value:
extract_value = {key: zz_value.group(1)}
write_yaml(os.path.dirname(os.getcwd()) + '/run/extract.yaml', extract_value)
else: # jsonpath
js_value = jsonpath.jsonpath(return_json, value)
if js_value:
extract_value = {key: js_value[0]}
write_yaml(os.path.dirname(os.getcwd()) + '/run/extract.yaml', extract_value)
self.assert_result(caseinfo['validate'], return_json, return_code)
# 将yaml文件中的name作为测试报告中用例名称
allure_title(caseinfo['name'])
else:
self.log.error("在request下必须包含method,url")
# print("在request下必须包含method,url")

else:
self.log.error("一级关键字必须包含name,request,validate")
# print("一级关键字必须包含name,request,validate")
posted on 2023-08-21 08:40  Raul2018  阅读(379)  评论(0编辑  收藏  举报