修复断言contains 实际结果是jsonpath匹配所有节点

This commit is contained in:
遥爸 2023-02-22 16:42:50 +08:00
parent 972aae073b
commit 004c4fd656
8 changed files with 67 additions and 13 deletions

View File

@ -23,7 +23,7 @@ platform_index_01:
fields: "id,categoryName,childCategories"
thirdPartySource: JEKUNAUTO
clientSource: "0"
dependence_case: True
dependence_case: False
# 依赖的数据
dependence_case_data:
- case_id: user_info_01
@ -46,4 +46,58 @@ platform_index_01:
type: ==
value: "车身护理"
AssertType:
sql:
sql:
platform_property_01:
host: ${{host()}}
url: /v1/service/service/property
method: get
detail: 获取商城筛选字段
headers:
Connection: 'keep-alive'
clientSource: '0'
areaCode: '440105'
content-type: 'application/json'
requestType: params
is_run:
data:
fields: "goodsProperty,isNeedMatchCar,matchProperty,sort"
thirdPartySource: JEKUNAUTO
carLicense:
frontServiceId: 1
dependence_case:
dependence_case_data:
assert:
status_code: 200
data:
jsonpath: $.data.*
type: contains
value: "价格从高至低"
AssertType:
sql:
#platform-goods-list_01:
# host: ${{host()}}
# url: /v1/service/service/platform-goods-list
# method: get
# detail: 获取商品详情
# headers:
# Connection: 'keep-alive'
# clientSource: '0'
# areaCode: '440105'
# content-type: 'application/json'
# requestType: params
# is_run:
# data:
# fields: "goodsProperty,isNeedMatchCar,matchProperty,sort"
# thirdPartySource: JEKUNAUTO
# carLicense:
# frontServiceId: 1
# dependence_case:
# dependence_case_data:
#
# assert:
# status_code: 200
# sql:

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023-02-21 17:00:49
# @Time : 2023-02-22 15:45:30
import allure
@ -12,7 +12,7 @@ from utils.read_files_tools.regular_control import regular
from utils.requests_tool.teardown_control import TearDownHandler
case_id = ['platform_index_01']
case_id = ['platform_index_01', 'platform_property_01']
TestData = GetTestCase.case_data(case_id)
re_data = regular(str(TestData))

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023-02-21 17:00:49
# @Time : 2023-02-22 15:45:30
import allure

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023-02-21 17:00:49
# @Time : 2023-02-22 15:45:30
import allure

View File

@ -23,6 +23,7 @@ class Assert:
# literal_eval()函数会判断需要计算的内容计算后是不是合法的python类型如果是则进行计算否则不计算就是去''
self.assert_data = ast.literal_eval(cache_regular(str(assert_data)))
# 返回一个字典,{Key:value} value的类型是一个函数或者是一个@staticmethod的静态方法
# {'equals': <function equals at 0x000001F226474670>,'less_than': <function less_than at 0x000001F2264772E0>, 'less_than_or_equals': <function ......'endswith': <function endswith at 0x000001F226477AC0>}
self.functions_mapping = load_module_functions(assert_type)
@staticmethod
@ -115,6 +116,8 @@ class Assert:
resp_data=resp_data,
message=message
)
if len(resp_data) > 1:
str(resp_data)
# 判断assertType为空的情况下则走响应断言
elif assert_types is None:
@ -143,13 +146,10 @@ class Assert:
assert status_code == values
else:
assert_value = self.assert_data[key]['value'] # 获取 yaml 文件中的期望value值
print("---------yaml的断言数据assert_value:{}----------------".format(assert_value))
assert_jsonpath = self.assert_data[key]['jsonpath'] # 获取到 yaml断言中的jsonpath的数据
assert_types = self.assert_data[key]['AssertType']
print("-----------assert_types:{}----------------".format(assert_types))
# 从yaml获取jsonpath拿到对象的接口响应数据
resp_data = jsonpath(json.loads(response_data), assert_jsonpath)
print("-----------响应数据resp_data:{}----------------".format(resp_data))
message = self._message(value=values)
# jsonpath 如果数据获取失败会返回False判断获取成功才会执行如下代码
if resp_data is not False:

View File

@ -110,8 +110,8 @@ def length_less_than_or_equals(
def contains(check_value: Any, expect_value: Any, message: Text = ""):
"""判断期望结果内容包含在实际结果中"""
assert isinstance(
check_value, (list, tuple, dict, str, bytes)
), "expect_value 需要为 list/tuple/dict/str/bytes 类型"
check_value, (list, tuple, dict, str, bytes, Text)
), "expect_value 需要为 list/tuple/dict/str/bytes Text 类型"
assert expect_value in check_value, message

View File

@ -44,6 +44,7 @@ def load_module_functions(module) -> Dict[Text, Callable]:
原理把字典对象转成可遍历对象判断里面是否有fuction再组装成字典返回
vars()内置函数参数一个字典对象返回一个字典
itemns()操作字典函数字典不可遍历items返回一个可遍历的对面dict_items([('name', 'jack'), ('item', '1')])
module_functions:{'equal':<function equals at 0X000***>'less_than':<function less_than at 0x00**>}
"""
module_functions = {}
@ -224,7 +225,6 @@ class AllureAttachmentType(Enum):
PDF = "pdf"
# todo 分析断言,增加断言种类
@unique
class AssertMethod(Enum):
"""断言类型"""

View File

@ -254,7 +254,7 @@ class CaseData:
self.raise_value_null_error(case_id=case_id, data_name="dependence_case")
) from exc
# TODO 对 dependence_case_data 中的值进行验证
# 对 dependence_case_data 中的值进行验证
def get_dependence_case_data(
self,
case_id: Text,