pytest-api-framework-alpha 1.0.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- framework/__init__.py +1 -0
- framework/assertion/__init__.py +3 -0
- framework/assertion/retry_assert.py +67 -0
- framework/assertion/webhook_assert.py +129 -0
- framework/client/__init__.py +3 -0
- framework/client/crrm_client.py +155 -0
- framework/client/http_client.py +340 -0
- framework/client/mq_client.py +178 -0
- framework/client/websocket_client.py +414 -0
- framework/conftest.py +526 -0
- framework/core/__init__.py +3 -0
- framework/core/base_class.py +871 -0
- framework/core/extract.py +185 -0
- framework/core/global_attribute.py +187 -0
- framework/core/hooks/__init__.py +3 -0
- framework/core/hooks/filters.py +217 -0
- framework/core/hooks/reporter.py +228 -0
- framework/core/hooks/session.py +345 -0
- framework/core/hooks/sorting.py +84 -0
- framework/core/models.py +82 -0
- framework/core/render_data.py +309 -0
- framework/core/validate.py +648 -0
- framework/db/__init__.py +3 -0
- framework/db/mysql_db.py +226 -0
- framework/db/redis_db.py +319 -0
- framework/exceptions.py +22 -0
- framework/exit_code.py +20 -0
- framework/gen/__init__.py +3 -0
- framework/gen/case_gen.py +1268 -0
- framework/gen/create_app.py +265 -0
- framework/report.py +111 -0
- framework/script.py +35 -0
- framework/settings.py +93 -0
- framework/utils/__init__.py +3 -0
- framework/utils/cognito_util.py +348 -0
- framework/utils/common.py +298 -0
- framework/utils/date_util.py +218 -0
- framework/utils/encrypt.py +448 -0
- framework/utils/lark_util.py +167 -0
- framework/utils/log_util.py +10 -0
- framework/utils/mock_util.py +236 -0
- framework/utils/yaml_util.py +169 -0
- framework/ws_server.py +167 -0
- pytest_api_framework_alpha-1.0.5.dist-info/METADATA +98 -0
- pytest_api_framework_alpha-1.0.5.dist-info/RECORD +48 -0
- pytest_api_framework_alpha-1.0.5.dist-info/WHEEL +5 -0
- pytest_api_framework_alpha-1.0.5.dist-info/licenses/LICENSE +21 -0
- pytest_api_framework_alpha-1.0.5.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
"""
|
|
2
|
+
断言验证模块。
|
|
3
|
+
|
|
4
|
+
提供 Validate 类,支持通过注册的断言函数对 HTTP/WebSocket 响应进行验证。
|
|
5
|
+
预置的断言方式包括:eq(相等)、contains(包含)、startswith、endswith、
|
|
6
|
+
len_eq(长度相等)、is_null(为空)、type_eq(类型判断)、
|
|
7
|
+
数值比较(lt/le/gt/ge)等。
|
|
8
|
+
支持通过 @register 装饰器扩展自定义断言方式。
|
|
9
|
+
"""
|
|
10
|
+
import re
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
import allure
|
|
14
|
+
from box import Box
|
|
15
|
+
from jsonpath import jsonpath
|
|
16
|
+
|
|
17
|
+
from framework.settings import RESPONSE_ATTR
|
|
18
|
+
from framework.utils.log_util import logger
|
|
19
|
+
from framework.core.render_data import RenderData
|
|
20
|
+
from framework.utils.common import an2cn, is_digit
|
|
21
|
+
from typing import Any, Callable
|
|
22
|
+
|
|
23
|
+
# 断言函数注册表,key 为断言方式名称,value 为对应的断言函数
|
|
24
|
+
registry: dict[str, Callable[..., Any]] = {}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def register(name: str) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
|
|
28
|
+
"""
|
|
29
|
+
断言函数注册装饰器。
|
|
30
|
+
|
|
31
|
+
将断言函数注册到全局 registry 中,使得 YAML 中的 validate 配置可以通过名称调用。
|
|
32
|
+
|
|
33
|
+
:param name: 断言方式名称(如 "eq"、"contains")
|
|
34
|
+
:return: 装饰器函数
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
def decorator(func):
|
|
38
|
+
registry[name] = func
|
|
39
|
+
return func
|
|
40
|
+
|
|
41
|
+
return decorator
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class Validate:
|
|
45
|
+
"""
|
|
46
|
+
响应断言器。
|
|
47
|
+
|
|
48
|
+
按 YAML 测试数据中 validate 配置逐条执行断言。
|
|
49
|
+
支持 JSONPath、正则表达式、Box 属性路径三种方式定位实际值。
|
|
50
|
+
支持从上下文 ${...} 引用预期值。
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
# 用于解析 Box 属性路径的正则:匹配 "name" 或 "[0]" 片段
|
|
54
|
+
_PATH_RE = re.compile(r'(\w+)|\[(\d+)]')
|
|
55
|
+
|
|
56
|
+
def __init__(self, data: Any, response: Any) -> None:
|
|
57
|
+
"""
|
|
58
|
+
初始化断言器。
|
|
59
|
+
|
|
60
|
+
:param data: 原始测试数据(含 validate 配置)
|
|
61
|
+
:param response: ResponseUtil 对象(含 .text / .json() 方法)
|
|
62
|
+
"""
|
|
63
|
+
self.data = RenderData(data)
|
|
64
|
+
self.response = response
|
|
65
|
+
self._body = None
|
|
66
|
+
self.registry = registry
|
|
67
|
+
|
|
68
|
+
def _get_body(self) -> Any:
|
|
69
|
+
"""
|
|
70
|
+
懒加载响应 JSON body。
|
|
71
|
+
|
|
72
|
+
:return: 响应 JSON 字典
|
|
73
|
+
"""
|
|
74
|
+
if self._body is None:
|
|
75
|
+
self._body = self.response.json()
|
|
76
|
+
return self._body
|
|
77
|
+
|
|
78
|
+
def valid(self, validates: list[dict]) -> None:
|
|
79
|
+
"""
|
|
80
|
+
按 validate 配置逐条执行断言。
|
|
81
|
+
|
|
82
|
+
validates 格式:[{"eq": "实际表达式, 预期值"}, ...]
|
|
83
|
+
表达式以逗号分隔,前半部分定位实际值,后半部分为预期值。
|
|
84
|
+
对于 is_null / not_null 等无预期值的断言,只有表达式部分。
|
|
85
|
+
|
|
86
|
+
:param validates: 断言配置列表
|
|
87
|
+
"""
|
|
88
|
+
for idx, valid_item in enumerate(validates):
|
|
89
|
+
key, value = next(iter(valid_item.items()))
|
|
90
|
+
valid_list = [i.strip() for i in value.split(",", 1)]
|
|
91
|
+
|
|
92
|
+
actual_expression = valid_list[0]
|
|
93
|
+
expected_result = self.unquote(valid_list[1]) if len(valid_list) > 1 else ""
|
|
94
|
+
|
|
95
|
+
func = self.registry.get(key)
|
|
96
|
+
|
|
97
|
+
try:
|
|
98
|
+
if not func:
|
|
99
|
+
with allure.step(f"不支持的断言方式: {key}"):
|
|
100
|
+
logger.error(f"不支持的断言方式: {key}")
|
|
101
|
+
pytest.fail(f"不支持的断言方式: {key}")
|
|
102
|
+
result = (func(self, actual_expression, expected_result) if len(valid_list) > 1
|
|
103
|
+
else func(self, actual_expression))
|
|
104
|
+
message = (
|
|
105
|
+
f"响应断言({an2cn(idx + 1)}): "
|
|
106
|
+
f"{func.__name__} {actual_expression} → {result}"
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
with allure.step(message):
|
|
110
|
+
logger.info(message)
|
|
111
|
+
|
|
112
|
+
except Exception as e:
|
|
113
|
+
error_msg = (
|
|
114
|
+
f"响应断言({an2cn(idx + 1)}): "
|
|
115
|
+
f"{func.__name__ if func else key} {actual_expression} → {str(e)}"
|
|
116
|
+
)
|
|
117
|
+
with allure.step(error_msg):
|
|
118
|
+
logger.error(error_msg)
|
|
119
|
+
pytest.fail(error_msg)
|
|
120
|
+
|
|
121
|
+
def parse_actual_expression(self, actual_expression: str) -> Any:
|
|
122
|
+
"""
|
|
123
|
+
解析实际值表达式,自动选择提取方式。
|
|
124
|
+
|
|
125
|
+
- $. → JSONPath
|
|
126
|
+
- /.../ → 正则
|
|
127
|
+
- 其他 → Box 属性路径
|
|
128
|
+
|
|
129
|
+
:param actual_expression: 实际值定位表达式
|
|
130
|
+
:return: 提取到的值
|
|
131
|
+
"""
|
|
132
|
+
if actual_expression.lower().startswith("$."):
|
|
133
|
+
return self.exec_jsonpath(actual_expression)
|
|
134
|
+
|
|
135
|
+
if actual_expression.startswith("/") and actual_expression.endswith("/"):
|
|
136
|
+
return self.exec_reg(actual_expression[1:-1])
|
|
137
|
+
|
|
138
|
+
return self.exec_box(actual_expression)
|
|
139
|
+
|
|
140
|
+
def exec_jsonpath(self, expression: str) -> Any:
|
|
141
|
+
"""
|
|
142
|
+
通过 JSONPath 表达式提取实际值。
|
|
143
|
+
|
|
144
|
+
:param expression: JSONPath 表达式,如 "$.data.code"
|
|
145
|
+
:return: 提取结果
|
|
146
|
+
:raises Exception: 表达式错误或响应异常
|
|
147
|
+
"""
|
|
148
|
+
try:
|
|
149
|
+
return jsonpath(self._get_body(), expression)[0]
|
|
150
|
+
|
|
151
|
+
except Exception as e:
|
|
152
|
+
raise Exception(
|
|
153
|
+
f"jsonpath表达式错误或非预期响应内容;"
|
|
154
|
+
f"表达式: {expression}"
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
def exec_reg(self, reg_expression: str) -> Any:
|
|
158
|
+
"""
|
|
159
|
+
通过正则表达式提取实际值。
|
|
160
|
+
|
|
161
|
+
:param reg_expression: 正则表达式(不含定界符)
|
|
162
|
+
:return: 提取结果,数字字符串自动转为 int/float
|
|
163
|
+
:raises Exception: 表达式错误或响应异常
|
|
164
|
+
"""
|
|
165
|
+
try:
|
|
166
|
+
result = re.search(reg_expression, self.response.text, flags=re.S).group()
|
|
167
|
+
return self.to_number(result)
|
|
168
|
+
|
|
169
|
+
except Exception as e:
|
|
170
|
+
raise Exception(
|
|
171
|
+
f"正则表达式错误或非预期响应内容;"
|
|
172
|
+
f"表达式: {reg_expression}"
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
def exec_box(self, expression: str) -> Any:
|
|
176
|
+
"""
|
|
177
|
+
通过 Box 属性路径提取实际值。
|
|
178
|
+
|
|
179
|
+
如果是 RESPONSE_ATTR 中的预定义属性(如 status_code、headers),
|
|
180
|
+
则直接通过 getattr 从响应对象获取。
|
|
181
|
+
|
|
182
|
+
:param expression: 属性路径,如 "data.items[0].id"
|
|
183
|
+
:return: 提取结果
|
|
184
|
+
:raises Exception: 表达式错误或响应异常
|
|
185
|
+
"""
|
|
186
|
+
try:
|
|
187
|
+
if expression in RESPONSE_ATTR:
|
|
188
|
+
return getattr(self.response, expression)
|
|
189
|
+
|
|
190
|
+
return self.get_nested_value(Box(self._get_body()), expression)
|
|
191
|
+
|
|
192
|
+
except Exception as e:
|
|
193
|
+
raise Exception(
|
|
194
|
+
f"box表达式错误或非预期响应内容;"
|
|
195
|
+
f"表达式: {expression}; "
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
@classmethod
|
|
199
|
+
def get_nested_value(cls, obj: Any, attr_path: str) -> Any:
|
|
200
|
+
"""
|
|
201
|
+
按属性路径从对象中逐层取值。
|
|
202
|
+
|
|
203
|
+
:param obj: 起始对象
|
|
204
|
+
:param attr_path: 属性路径字符串,如 "data.items[0].id"
|
|
205
|
+
:return: 取值结果,任意节点不存在则返回 None
|
|
206
|
+
"""
|
|
207
|
+
path_elements = cls._PATH_RE.findall(attr_path)
|
|
208
|
+
try:
|
|
209
|
+
for attr, index in path_elements:
|
|
210
|
+
if attr:
|
|
211
|
+
obj = getattr(obj, attr)
|
|
212
|
+
|
|
213
|
+
if index:
|
|
214
|
+
obj = obj[int(index)]
|
|
215
|
+
|
|
216
|
+
return obj
|
|
217
|
+
|
|
218
|
+
except Exception:
|
|
219
|
+
return None
|
|
220
|
+
|
|
221
|
+
def _resolve_result(self, value: Any) -> Any:
|
|
222
|
+
"""
|
|
223
|
+
解析预期值,如果值是 ${...} 占位符则从 context 中取值。
|
|
224
|
+
|
|
225
|
+
:param value: 原始预期值
|
|
226
|
+
:return: 解析后的值
|
|
227
|
+
"""
|
|
228
|
+
if isinstance(value, str) and value.startswith("${") and value.endswith("}"):
|
|
229
|
+
return self.data.get_attribute(value[2:-1])
|
|
230
|
+
|
|
231
|
+
return value
|
|
232
|
+
|
|
233
|
+
@staticmethod
|
|
234
|
+
def to_number(value: Any) -> Any:
|
|
235
|
+
"""
|
|
236
|
+
将数字字符串转为 int 或 float。
|
|
237
|
+
|
|
238
|
+
:param value: 输入值
|
|
239
|
+
:return: 如果输入是数字字符串则返回 int/float,否则原样返回
|
|
240
|
+
"""
|
|
241
|
+
if not isinstance(value, str) or not is_digit(value):
|
|
242
|
+
return value
|
|
243
|
+
|
|
244
|
+
return float(value) if "." in value or "e" in value.lower() else int(value)
|
|
245
|
+
|
|
246
|
+
@staticmethod
|
|
247
|
+
def unquote(expected_result: str) -> str:
|
|
248
|
+
"""
|
|
249
|
+
去掉预期值外层成对的引号,引号内的内容原样保留(不再 strip)。
|
|
250
|
+
|
|
251
|
+
`- eq: $.data.address, '0x12ab'` 整条在 YAML 里是一个标量,开头的 $ 让 YAML
|
|
252
|
+
不把中间的 '...' 当作引号,于是引号会原样留在预期值里,变成和 "'0x12ab'"
|
|
253
|
+
比较。而用户加引号的本意是「这是个字面量字符串」。
|
|
254
|
+
|
|
255
|
+
加引号同时也是表达「前后空格是值的一部分」的唯一办法——valid() 切完会对两段
|
|
256
|
+
做 strip(),不加引号的空格传不进断言。
|
|
257
|
+
|
|
258
|
+
:param expected_result: 从 YAML 切出来、已 strip 过的预期值
|
|
259
|
+
:return: 去掉外层成对引号后的预期值
|
|
260
|
+
"""
|
|
261
|
+
if (len(expected_result) >= 2
|
|
262
|
+
and expected_result[0] == expected_result[-1]
|
|
263
|
+
and expected_result[0] in ("'", '"')):
|
|
264
|
+
return expected_result[1:-1]
|
|
265
|
+
|
|
266
|
+
return expected_result
|
|
267
|
+
|
|
268
|
+
@staticmethod
|
|
269
|
+
def to_bool(actual_result: Any, expected_result: Any) -> tuple[Any, Any]:
|
|
270
|
+
"""
|
|
271
|
+
实际值是布尔时,把预期值里的 "True"/"true"/"False"/"false" 也转成布尔。
|
|
272
|
+
|
|
273
|
+
YAML 的 validate 写成 "表达式, 预期值",valid() 按逗号切完预期值永远是字符串,
|
|
274
|
+
而 to_number 只认数字串,所以 `eq: $.data.valid, False` 会拿字符串 "False"
|
|
275
|
+
去和真布尔 False 比,永远不相等。
|
|
276
|
+
|
|
277
|
+
只在实际值确实是布尔时才转换,避免把真正要比字符串 "True" 的断言也改掉。
|
|
278
|
+
|
|
279
|
+
:param actual_result: 实际值
|
|
280
|
+
:param expected_result: 预期值
|
|
281
|
+
:return: (actual_result, expected_result)
|
|
282
|
+
"""
|
|
283
|
+
if isinstance(actual_result, bool) and isinstance(expected_result, str):
|
|
284
|
+
lowered = expected_result.strip().lower()
|
|
285
|
+
if lowered in ("true", "false"):
|
|
286
|
+
return actual_result, lowered == "true"
|
|
287
|
+
|
|
288
|
+
return actual_result, expected_result
|
|
289
|
+
|
|
290
|
+
@staticmethod
|
|
291
|
+
def is_empty(value: Any) -> bool:
|
|
292
|
+
"""
|
|
293
|
+
判断值是否为空。
|
|
294
|
+
|
|
295
|
+
:param value: 输入值
|
|
296
|
+
:return: True 表示空(None / "" / [] / {} / () / set())
|
|
297
|
+
"""
|
|
298
|
+
return value in (None, "", [], {}, (), set())
|
|
299
|
+
|
|
300
|
+
def __to_digit(self, actual_result: Any, expected_result: Any) -> tuple[Any, Any]:
|
|
301
|
+
"""
|
|
302
|
+
将实际值和预期值都转为数字。
|
|
303
|
+
|
|
304
|
+
:param actual_result: 实际值
|
|
305
|
+
:param expected_result: 预期值
|
|
306
|
+
:return: (actual_result, expected_result) 数字格式
|
|
307
|
+
"""
|
|
308
|
+
return self.to_number(actual_result), self.to_number(expected_result)
|
|
309
|
+
|
|
310
|
+
@staticmethod
|
|
311
|
+
def __to_str(actual_result: Any, expected_result: Any) -> tuple[str, str]:
|
|
312
|
+
"""
|
|
313
|
+
将实际值和预期值都转为字符串。
|
|
314
|
+
|
|
315
|
+
:param actual_result: 实际值
|
|
316
|
+
:param expected_result: 预期值
|
|
317
|
+
:return: (actual_result, expected_result) 字符串格式
|
|
318
|
+
"""
|
|
319
|
+
if isinstance(actual_result, (int, float)):
|
|
320
|
+
actual_result = str(actual_result)
|
|
321
|
+
|
|
322
|
+
if isinstance(expected_result, (int, float)):
|
|
323
|
+
expected_result = str(expected_result)
|
|
324
|
+
|
|
325
|
+
return actual_result, expected_result
|
|
326
|
+
|
|
327
|
+
@register("eq")
|
|
328
|
+
def assert_equal(self, actual_expression, expected_result):
|
|
329
|
+
"""
|
|
330
|
+
断言相等(==)。
|
|
331
|
+
|
|
332
|
+
:param actual_expression: 实际值表达式
|
|
333
|
+
:param expected_result: 预期值
|
|
334
|
+
:return: 断言结果描述
|
|
335
|
+
"""
|
|
336
|
+
actual_result = self.to_number(self.parse_actual_expression(actual_expression))
|
|
337
|
+
expected_result = self.to_number(self._resolve_result(expected_result))
|
|
338
|
+
actual_result, expected_result = self.to_bool(actual_result, expected_result)
|
|
339
|
+
|
|
340
|
+
# 用 !r 而不是直接插值:类型不一致时(如真布尔 False 对字符串 "False")
|
|
341
|
+
# 两边 str() 出来一模一样,报错信息里看不出差别
|
|
342
|
+
assert actual_result == expected_result, (
|
|
343
|
+
f"实际结果({actual_result!r}) != 预期结果({expected_result!r})"
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
return f"实际结果({actual_result}) == 预期结果({expected_result})"
|
|
347
|
+
|
|
348
|
+
@register("not_eq")
|
|
349
|
+
def assert_not_equal(self, actual_expression, expected_result):
|
|
350
|
+
"""
|
|
351
|
+
断言不相等(!=)。
|
|
352
|
+
|
|
353
|
+
:param actual_expression: 实际值表达式
|
|
354
|
+
:param expected_result: 预期值
|
|
355
|
+
:return: 断言结果描述
|
|
356
|
+
"""
|
|
357
|
+
actual_result = self.to_number(self.parse_actual_expression(actual_expression))
|
|
358
|
+
expected_result = self.to_number(self._resolve_result(expected_result))
|
|
359
|
+
actual_result, expected_result = self.to_bool(actual_result, expected_result)
|
|
360
|
+
|
|
361
|
+
assert actual_result != expected_result, (
|
|
362
|
+
f"实际结果({actual_result!r}) == 预期结果({expected_result!r})"
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
return f"实际结果({actual_result}) != 预期结果({expected_result})"
|
|
366
|
+
|
|
367
|
+
@register("lt")
|
|
368
|
+
def assert_less_than(self, actual_expression, expected_result):
|
|
369
|
+
"""
|
|
370
|
+
断言小于(<)。
|
|
371
|
+
|
|
372
|
+
:param actual_expression: 实际值表达式
|
|
373
|
+
:param expected_result: 预期值
|
|
374
|
+
:return: 断言结果描述
|
|
375
|
+
"""
|
|
376
|
+
actual_result, expected_result = self.__to_digit(
|
|
377
|
+
self.parse_actual_expression(actual_expression),
|
|
378
|
+
self._resolve_result(expected_result)
|
|
379
|
+
)
|
|
380
|
+
|
|
381
|
+
assert actual_result < expected_result, (
|
|
382
|
+
f"实际结果({actual_result}) >= 预期结果({expected_result})"
|
|
383
|
+
)
|
|
384
|
+
|
|
385
|
+
return f"实际结果({actual_result}) < 预期结果({expected_result})"
|
|
386
|
+
|
|
387
|
+
@register("le")
|
|
388
|
+
def assert_less_than_or_equals(self, actual_expression, expected_result):
|
|
389
|
+
"""
|
|
390
|
+
断言小于等于(<=)。
|
|
391
|
+
|
|
392
|
+
:param actual_expression: 实际值表达式
|
|
393
|
+
:param expected_result: 预期值
|
|
394
|
+
:return: 断言结果描述
|
|
395
|
+
"""
|
|
396
|
+
actual_result, expected_result = self.__to_digit(
|
|
397
|
+
self.parse_actual_expression(actual_expression),
|
|
398
|
+
self._resolve_result(expected_result)
|
|
399
|
+
)
|
|
400
|
+
|
|
401
|
+
assert actual_result <= expected_result, (
|
|
402
|
+
f"实际结果({actual_result}) > 预期结果({expected_result})"
|
|
403
|
+
)
|
|
404
|
+
|
|
405
|
+
return f"实际结果({actual_result}) <= 预期结果({expected_result})"
|
|
406
|
+
|
|
407
|
+
@register("gt")
|
|
408
|
+
def assert_greater_than(self, actual_expression, expected_result):
|
|
409
|
+
"""
|
|
410
|
+
断言大于(>)。
|
|
411
|
+
|
|
412
|
+
:param actual_expression: 实际值表达式
|
|
413
|
+
:param expected_result: 预期值
|
|
414
|
+
:return: 断言结果描述
|
|
415
|
+
"""
|
|
416
|
+
actual_result, expected_result = self.__to_digit(
|
|
417
|
+
self.parse_actual_expression(actual_expression),
|
|
418
|
+
self._resolve_result(expected_result)
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
assert actual_result > expected_result, (
|
|
422
|
+
f"实际结果({actual_result}) <= 预期结果({expected_result})"
|
|
423
|
+
)
|
|
424
|
+
|
|
425
|
+
return f"实际结果({actual_result}) > 预期结果({expected_result})"
|
|
426
|
+
|
|
427
|
+
@register("ge")
|
|
428
|
+
def assert_greater_than_or_equals(self, actual_expression, expected_result):
|
|
429
|
+
"""
|
|
430
|
+
断言大于等于(>=)。
|
|
431
|
+
|
|
432
|
+
:param actual_expression: 实际值表达式
|
|
433
|
+
:param expected_result: 预期值
|
|
434
|
+
:return: 断言结果描述
|
|
435
|
+
"""
|
|
436
|
+
actual_result, expected_result = self.__to_digit(
|
|
437
|
+
self.parse_actual_expression(actual_expression),
|
|
438
|
+
self._resolve_result(expected_result)
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
assert actual_result >= expected_result, (
|
|
442
|
+
f"实际结果({actual_result}) < 预期结果({expected_result})"
|
|
443
|
+
)
|
|
444
|
+
|
|
445
|
+
return f"实际结果({actual_result}) >= 预期结果({expected_result})"
|
|
446
|
+
|
|
447
|
+
@register("contains")
|
|
448
|
+
def assert_contains(self, actual_expression, expected_result):
|
|
449
|
+
"""
|
|
450
|
+
断言包含(in)。
|
|
451
|
+
|
|
452
|
+
:param actual_expression: 实际值表达式
|
|
453
|
+
:param expected_result: 预期值
|
|
454
|
+
:return: 断言结果描述
|
|
455
|
+
"""
|
|
456
|
+
actual_result, expected_result = self.__to_str(
|
|
457
|
+
self.parse_actual_expression(actual_expression),
|
|
458
|
+
self._resolve_result(expected_result)
|
|
459
|
+
)
|
|
460
|
+
|
|
461
|
+
assert expected_result in actual_result, (
|
|
462
|
+
f"实际结果({actual_result}) 不包含 预期结果({expected_result})"
|
|
463
|
+
)
|
|
464
|
+
|
|
465
|
+
return f"实际结果({actual_result}) 包含 预期结果({expected_result})"
|
|
466
|
+
|
|
467
|
+
@register("startswith")
|
|
468
|
+
def assert_startswith(self, actual_expression, expected_result):
|
|
469
|
+
"""
|
|
470
|
+
断言以指定前缀开头。
|
|
471
|
+
|
|
472
|
+
:param actual_expression: 实际值表达式
|
|
473
|
+
:param expected_result: 预期前缀
|
|
474
|
+
:return: 断言结果描述
|
|
475
|
+
"""
|
|
476
|
+
actual_result, expected_result = self.__to_str(
|
|
477
|
+
self.parse_actual_expression(actual_expression),
|
|
478
|
+
self._resolve_result(expected_result)
|
|
479
|
+
)
|
|
480
|
+
|
|
481
|
+
assert actual_result.startswith(expected_result), (
|
|
482
|
+
f"实际结果({actual_result}) 不以 预期结果({expected_result}) 开头"
|
|
483
|
+
)
|
|
484
|
+
|
|
485
|
+
return f"实际结果({actual_result}) 以 预期结果({expected_result}) 开头"
|
|
486
|
+
|
|
487
|
+
@register("endswith")
|
|
488
|
+
def assert_endswith(self, actual_expression, expected_result):
|
|
489
|
+
"""
|
|
490
|
+
断言以指定后缀结尾。
|
|
491
|
+
|
|
492
|
+
:param actual_expression: 实际值表达式
|
|
493
|
+
:param expected_result: 预期后缀
|
|
494
|
+
:return: 断言结果描述
|
|
495
|
+
"""
|
|
496
|
+
actual_result, expected_result = self.__to_str(
|
|
497
|
+
self.parse_actual_expression(actual_expression),
|
|
498
|
+
self._resolve_result(expected_result)
|
|
499
|
+
)
|
|
500
|
+
|
|
501
|
+
assert actual_result.endswith(expected_result), (
|
|
502
|
+
f"实际结果({actual_result}) 不以 预期结果({expected_result}) 结尾"
|
|
503
|
+
)
|
|
504
|
+
|
|
505
|
+
return f"实际结果({actual_result}) 以 预期结果({expected_result}) 结尾"
|
|
506
|
+
|
|
507
|
+
@register("len_eq")
|
|
508
|
+
def assert_length_equals(self, actual_expression, expected_result):
|
|
509
|
+
"""
|
|
510
|
+
断言长度相等。
|
|
511
|
+
|
|
512
|
+
:param actual_expression: 实际值表达式(应为列表或字符串)
|
|
513
|
+
:param expected_result: 预期长度
|
|
514
|
+
:return: 断言结果描述
|
|
515
|
+
"""
|
|
516
|
+
actual_result = self.parse_actual_expression(actual_expression)
|
|
517
|
+
expected_result = int(self._resolve_result(expected_result))
|
|
518
|
+
|
|
519
|
+
assert len(actual_result) == expected_result, (
|
|
520
|
+
f"实际长度({len(actual_result)}) != 预期长度({expected_result})"
|
|
521
|
+
)
|
|
522
|
+
|
|
523
|
+
return f"实际长度({len(actual_result)}) == 预期长度({expected_result})"
|
|
524
|
+
|
|
525
|
+
@register("len_lt")
|
|
526
|
+
def assert_length_less_than(self, actual_expression, expected_result):
|
|
527
|
+
"""
|
|
528
|
+
断言长度小于。
|
|
529
|
+
|
|
530
|
+
:param actual_expression: 实际值表达式(应为列表或字符串)
|
|
531
|
+
:param expected_result: 预期长度
|
|
532
|
+
:return: 断言结果描述
|
|
533
|
+
"""
|
|
534
|
+
actual_result = self.parse_actual_expression(actual_expression)
|
|
535
|
+
expected_result = int(self._resolve_result(expected_result))
|
|
536
|
+
|
|
537
|
+
assert len(actual_result) < expected_result, (
|
|
538
|
+
f"实际长度({len(actual_result)}) >= 预期长度({expected_result})"
|
|
539
|
+
)
|
|
540
|
+
|
|
541
|
+
return f"实际长度({len(actual_result)}) < 预期长度({expected_result})"
|
|
542
|
+
|
|
543
|
+
@register("len_le")
|
|
544
|
+
def assert_length_less_than_or_equals(self, actual_expression, expected_result):
|
|
545
|
+
"""
|
|
546
|
+
断言长度小于等于。
|
|
547
|
+
|
|
548
|
+
:param actual_expression: 实际值表达式(应为列表或字符串)
|
|
549
|
+
:param expected_result: 预期长度
|
|
550
|
+
:return: 断言结果描述
|
|
551
|
+
"""
|
|
552
|
+
actual_result = self.parse_actual_expression(actual_expression)
|
|
553
|
+
expected_result = int(self._resolve_result(expected_result))
|
|
554
|
+
|
|
555
|
+
assert len(actual_result) <= expected_result, (
|
|
556
|
+
f"实际长度({len(actual_result)}) > 预期长度({expected_result})"
|
|
557
|
+
)
|
|
558
|
+
|
|
559
|
+
return f"实际长度({len(actual_result)}) <= 预期长度({expected_result})"
|
|
560
|
+
|
|
561
|
+
@register("len_gt")
|
|
562
|
+
def assert_length_greater_than(self, actual_expression, expected_result):
|
|
563
|
+
"""
|
|
564
|
+
断言长度大于。
|
|
565
|
+
|
|
566
|
+
:param actual_expression: 实际值表达式(应为列表或字符串)
|
|
567
|
+
:param expected_result: 预期长度
|
|
568
|
+
:return: 断言结果描述
|
|
569
|
+
"""
|
|
570
|
+
actual_result = self.parse_actual_expression(actual_expression)
|
|
571
|
+
expected_result = int(self._resolve_result(expected_result))
|
|
572
|
+
|
|
573
|
+
assert len(actual_result) > expected_result, (
|
|
574
|
+
f"实际长度({len(actual_result)}) <= 预期长度({expected_result})"
|
|
575
|
+
)
|
|
576
|
+
|
|
577
|
+
return f"实际长度({len(actual_result)}) > 预期长度({expected_result})"
|
|
578
|
+
|
|
579
|
+
@register("len_ge")
|
|
580
|
+
def assert_length_greater_than_or_equals(self, actual_expression, expected_result):
|
|
581
|
+
"""
|
|
582
|
+
断言长度大于等于。
|
|
583
|
+
|
|
584
|
+
:param actual_expression: 实际值表达式(应为列表或字符串)
|
|
585
|
+
:param expected_result: 预期长度
|
|
586
|
+
:return: 断言结果描述
|
|
587
|
+
"""
|
|
588
|
+
actual_result = self.parse_actual_expression(actual_expression)
|
|
589
|
+
expected_result = int(self._resolve_result(expected_result))
|
|
590
|
+
|
|
591
|
+
assert len(actual_result) >= expected_result, (
|
|
592
|
+
f"实际长度({len(actual_result)}) < 预期长度({expected_result})"
|
|
593
|
+
)
|
|
594
|
+
|
|
595
|
+
return f"实际长度({len(actual_result)}) >= 预期长度({expected_result})"
|
|
596
|
+
|
|
597
|
+
@register("is_null")
|
|
598
|
+
def assert_is_null(self, actual_expression):
|
|
599
|
+
"""
|
|
600
|
+
断言值为空(None / "" / [] / {} / () / set())。
|
|
601
|
+
|
|
602
|
+
:param actual_expression: 实际值表达式
|
|
603
|
+
:return: 断言结果描述
|
|
604
|
+
"""
|
|
605
|
+
actual_result = self.parse_actual_expression(actual_expression)
|
|
606
|
+
assert self.is_empty(actual_result), (
|
|
607
|
+
f"实际结果({actual_result}) 不是空值"
|
|
608
|
+
)
|
|
609
|
+
return f"实际结果({actual_result})"
|
|
610
|
+
|
|
611
|
+
@register("not_null")
|
|
612
|
+
def assert_not_null(self, actual_expression):
|
|
613
|
+
"""
|
|
614
|
+
断言值不为空。
|
|
615
|
+
|
|
616
|
+
:param actual_expression: 实际值表达式
|
|
617
|
+
:return: 断言结果描述
|
|
618
|
+
"""
|
|
619
|
+
actual_result = self.parse_actual_expression(actual_expression)
|
|
620
|
+
assert not self.is_empty(actual_result), (
|
|
621
|
+
f"实际结果({actual_result}) 是空值"
|
|
622
|
+
)
|
|
623
|
+
return f"实际结果({actual_result})"
|
|
624
|
+
|
|
625
|
+
@register("type_eq")
|
|
626
|
+
def assert_type(self, actual_expression, type_name):
|
|
627
|
+
"""
|
|
628
|
+
断言实际值的类型。
|
|
629
|
+
|
|
630
|
+
支持类型名称:str、int、float、bool、list、dict、None(null)。
|
|
631
|
+
|
|
632
|
+
:param actual_expression: 实际值表达式
|
|
633
|
+
:param type_name: 期望的类型名称(不区分大小写)
|
|
634
|
+
:return: 断言结果描述
|
|
635
|
+
"""
|
|
636
|
+
actual_result = self.parse_actual_expression(actual_expression)
|
|
637
|
+
type_name = type_name.lower()
|
|
638
|
+
actual_type = (
|
|
639
|
+
"null"
|
|
640
|
+
if actual_result is None
|
|
641
|
+
else type(actual_result).__name__
|
|
642
|
+
)
|
|
643
|
+
|
|
644
|
+
assert actual_type == type_name or actual_type in type_name, (
|
|
645
|
+
f'实际类型({actual_type}) != 预期类型({type_name})'
|
|
646
|
+
)
|
|
647
|
+
|
|
648
|
+
return f'实际类型({actual_type}) == 预期类型({type_name})'
|
framework/db/__init__.py
ADDED