pytest-dsl 0.3.1__py3-none-any.whl → 0.4.0__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.
@@ -1,4 +1,10 @@
1
1
  import allure
2
+ import time
3
+ import random
4
+ import string
5
+ import subprocess
6
+ import datetime
7
+ import logging
2
8
  from pytest_dsl.core.keyword_manager import keyword_manager
3
9
 
4
10
 
@@ -15,3 +21,323 @@ def print_content(**kwargs):
15
21
  ])
16
22
  def return_result(**kwargs):
17
23
  return kwargs.get('result')
24
+
25
+
26
+ @keyword_manager.register('等待', [
27
+ {'name': '秒数', 'mapping': 'seconds', 'description': '等待的秒数,可以是小数'}
28
+ ])
29
+ def wait_seconds(**kwargs):
30
+ """等待指定的秒数
31
+
32
+ Args:
33
+ seconds: 等待的秒数,可以是小数表示毫秒级等待
34
+ """
35
+ seconds = float(kwargs.get('seconds', 0))
36
+ with allure.step(f"等待 {seconds} 秒"):
37
+ time.sleep(seconds)
38
+ return True
39
+
40
+
41
+ @keyword_manager.register('获取当前时间', [
42
+ {'name': '格式', 'mapping': 'format', 'description': '时间格式,例如 "%Y-%m-%d %H:%M:%S",默认返回时间戳'},
43
+ {'name': '时区', 'mapping': 'timezone', 'description': '时区,例如 "Asia/Shanghai",默认为本地时区'}
44
+ ])
45
+ def get_current_time(**kwargs):
46
+ """获取当前时间
47
+
48
+ Args:
49
+ format: 时间格式,如果不提供则返回时间戳
50
+ timezone: 时区,默认为本地时区
51
+
52
+ Returns:
53
+ str: 格式化的时间字符串或时间戳
54
+ """
55
+ time_format = kwargs.get('format')
56
+ timezone = kwargs.get('timezone')
57
+
58
+ # 获取当前时间
59
+ if timezone:
60
+ import pytz
61
+ try:
62
+ tz = pytz.timezone(timezone)
63
+ current_time = datetime.datetime.now(tz)
64
+ except Exception as e:
65
+ allure.attach(
66
+ f"时区设置异常: {str(e)}",
67
+ name="时区设置异常",
68
+ attachment_type=allure.attachment_type.TEXT
69
+ )
70
+ current_time = datetime.datetime.now()
71
+ else:
72
+ current_time = datetime.datetime.now()
73
+
74
+ # 格式化时间
75
+ if time_format:
76
+ try:
77
+ result = current_time.strftime(time_format)
78
+ except Exception as e:
79
+ allure.attach(
80
+ f"时间格式化异常: {str(e)}",
81
+ name="时间格式化异常",
82
+ attachment_type=allure.attachment_type.TEXT
83
+ )
84
+ result = str(current_time)
85
+ else:
86
+ # 返回时间戳
87
+ result = str(int(current_time.timestamp()))
88
+
89
+ return result
90
+
91
+
92
+ @keyword_manager.register('生成随机字符串', [
93
+ {'name': '长度', 'mapping': 'length', 'description': '随机字符串的长度,默认为8'},
94
+ {'name': '类型', 'mapping': 'type',
95
+ 'description': '字符类型:字母(letters)、数字(digits)、字母数字(alphanumeric)、全部(all),默认为字母数字'}
96
+ ])
97
+ def generate_random_string(**kwargs):
98
+ """生成随机字符串
99
+
100
+ Args:
101
+ length: 随机字符串的长度
102
+ type: 字符类型:字母、数字、字母数字、全部
103
+
104
+ Returns:
105
+ str: 生成的随机字符串
106
+ """
107
+ length = int(kwargs.get('length', 8))
108
+ char_type = kwargs.get('type', 'alphanumeric').lower()
109
+
110
+ # 根据类型选择字符集
111
+ if char_type == 'letters':
112
+ chars = string.ascii_letters
113
+ elif char_type == 'digits':
114
+ chars = string.digits
115
+ elif char_type == 'alphanumeric':
116
+ chars = string.ascii_letters + string.digits
117
+ elif char_type == 'all':
118
+ chars = string.ascii_letters + string.digits + string.punctuation
119
+ else:
120
+ # 默认使用字母数字
121
+ chars = string.ascii_letters + string.digits
122
+
123
+ # 生成随机字符串
124
+ result = ''.join(random.choice(chars) for _ in range(length))
125
+
126
+ with allure.step(f"生成随机字符串: 长度={length}, 类型={char_type}"):
127
+ allure.attach(
128
+ f"生成的随机字符串: {result}",
129
+ name="随机字符串",
130
+ attachment_type=allure.attachment_type.TEXT
131
+ )
132
+
133
+ return result
134
+
135
+
136
+ @keyword_manager.register('生成随机数', [
137
+ {'name': '最小值', 'mapping': 'min', 'description': '随机数的最小值,默认为0'},
138
+ {'name': '最大值', 'mapping': 'max', 'description': '随机数的最大值,默认为100'},
139
+ {'name': '小数位数', 'mapping': 'decimals', 'description': '小数位数,默认为0(整数)'}
140
+ ])
141
+ def generate_random_number(**kwargs):
142
+ """生成随机数
143
+
144
+ Args:
145
+ min: 随机数的最小值
146
+ max: 随机数的最大值
147
+ decimals: 小数位数,0表示整数
148
+
149
+ Returns:
150
+ int/float: 生成的随机数
151
+ """
152
+ min_value = float(kwargs.get('min', 0))
153
+ max_value = float(kwargs.get('max', 100))
154
+ decimals = int(kwargs.get('decimals', 0))
155
+
156
+ if decimals <= 0:
157
+ # 生成整数
158
+ result = random.randint(int(min_value), int(max_value))
159
+ else:
160
+ # 生成浮点数
161
+ result = round(random.uniform(min_value, max_value), decimals)
162
+
163
+ with allure.step(f"生成随机数: 范围=[{min_value}, {max_value}], 小数位数={decimals}"):
164
+ allure.attach(
165
+ f"生成的随机数: {result}",
166
+ name="随机数",
167
+ attachment_type=allure.attachment_type.TEXT
168
+ )
169
+
170
+ return result
171
+
172
+
173
+ @keyword_manager.register('字符串操作', [
174
+ {'name': '操作', 'mapping': 'operation',
175
+ 'description': '操作类型:拼接(concat)、替换(replace)、分割(split)、大写(upper)、小写(lower)、去空格(strip)'},
176
+ {'name': '字符串', 'mapping': 'string', 'description': '要操作的字符串'},
177
+ {'name': '参数1', 'mapping': 'param1', 'description': '操作参数1,根据操作类型不同而不同'},
178
+ {'name': '参数2', 'mapping': 'param2', 'description': '操作参数2,根据操作类型不同而不同'}
179
+ ])
180
+ def string_operation(**kwargs):
181
+ """字符串操作
182
+
183
+ Args:
184
+ operation: 操作类型
185
+ string: 要操作的字符串
186
+ param1: 操作参数1
187
+ param2: 操作参数2
188
+
189
+ Returns:
190
+ str: 操作结果
191
+ """
192
+ operation = kwargs.get('operation', '').lower()
193
+ string = str(kwargs.get('string', ''))
194
+ param1 = kwargs.get('param1', '')
195
+ param2 = kwargs.get('param2', '')
196
+
197
+ result = string
198
+
199
+ if operation == 'concat':
200
+ # 拼接字符串
201
+ result = string + str(param1)
202
+ elif operation == 'replace':
203
+ # 替换字符串
204
+ result = string.replace(str(param1), str(param2))
205
+ elif operation == 'split':
206
+ # 分割字符串
207
+ result = string.split(str(param1))
208
+ if param2 and param2.isdigit():
209
+ # 如果提供了索引,返回指定位置的元素
210
+ index = int(param2)
211
+ if 0 <= index < len(result):
212
+ result = result[index]
213
+ elif operation == 'upper':
214
+ # 转大写
215
+ result = string.upper()
216
+ elif operation == 'lower':
217
+ # 转小写
218
+ result = string.lower()
219
+ elif operation == 'strip':
220
+ # 去空格
221
+ result = string.strip()
222
+ else:
223
+ # 未知操作,返回原字符串
224
+ allure.attach(
225
+ f"未知的字符串操作: {operation}",
226
+ name="字符串操作错误",
227
+ attachment_type=allure.attachment_type.TEXT
228
+ )
229
+
230
+ with allure.step(f"字符串操作: {operation}"):
231
+ allure.attach(
232
+ f"原字符串: {string}\n操作: {operation}\n参数1: {param1}\n参数2: {param2}\n结果: {result}",
233
+ name="字符串操作结果",
234
+ attachment_type=allure.attachment_type.TEXT
235
+ )
236
+
237
+ return result
238
+
239
+
240
+ @keyword_manager.register('日志', [
241
+ {'name': '级别', 'mapping': 'level',
242
+ 'description': '日志级别:DEBUG, INFO, WARNING, ERROR, CRITICAL,默认为INFO'},
243
+ {'name': '消息', 'mapping': 'message', 'description': '日志消息内容'}
244
+ ])
245
+ def log_message(**kwargs):
246
+ """记录日志
247
+
248
+ Args:
249
+ level: 日志级别
250
+ message: 日志消息内容
251
+ """
252
+ level = kwargs.get('level', 'INFO').upper()
253
+ message = kwargs.get('message', '')
254
+
255
+ # 获取日志级别
256
+ log_level = getattr(logging, level, logging.INFO)
257
+
258
+ # 记录日志
259
+ logging.log(log_level, message)
260
+
261
+ with allure.step(f"记录日志: [{level}] {message}"):
262
+ allure.attach(
263
+ f"日志级别: {level}\n日志消息: {message}",
264
+ name="日志记录",
265
+ attachment_type=allure.attachment_type.TEXT
266
+ )
267
+
268
+ return True
269
+
270
+
271
+ @keyword_manager.register('执行命令', [
272
+ {'name': '命令', 'mapping': 'command', 'description': '要执行的系统命令'},
273
+ {'name': '超时', 'mapping': 'timeout', 'description': '命令执行超时时间(秒),默认为60秒'},
274
+ {'name': '捕获输出', 'mapping': 'capture_output', 'description': '是否捕获命令输出,默认为True'}
275
+ ])
276
+ def execute_command(**kwargs):
277
+ """执行系统命令
278
+
279
+ Args:
280
+ command: 要执行的系统命令
281
+ timeout: 命令执行超时时间(秒)
282
+ capture_output: 是否捕获命令输出
283
+
284
+ Returns:
285
+ dict: 包含返回码、标准输出和标准错误的字典
286
+ """
287
+ command = kwargs.get('command', '')
288
+ timeout = float(kwargs.get('timeout', 60))
289
+ capture_output = kwargs.get('capture_output', True)
290
+
291
+ with allure.step(f"执行命令: {command}"):
292
+ try:
293
+ # 执行命令
294
+ result = subprocess.run(
295
+ command,
296
+ shell=True,
297
+ timeout=timeout,
298
+ capture_output=capture_output,
299
+ text=True
300
+ )
301
+
302
+ # 构建结果字典
303
+ command_result = {
304
+ 'returncode': result.returncode,
305
+ 'stdout': result.stdout if capture_output else '',
306
+ 'stderr': result.stderr if capture_output else ''
307
+ }
308
+
309
+ # 记录执行结果
310
+ allure.attach(
311
+ f"命令: {command}\n返回码: {result.returncode}\n"
312
+ f"标准输出: {result.stdout if capture_output else '未捕获'}\n"
313
+ f"标准错误: {result.stderr if capture_output else '未捕获'}",
314
+ name="命令执行结果",
315
+ attachment_type=allure.attachment_type.TEXT
316
+ )
317
+
318
+ return command_result
319
+
320
+ except subprocess.TimeoutExpired:
321
+ # 命令执行超时
322
+ allure.attach(
323
+ f"命令执行超时: {command} (超时: {timeout}秒)",
324
+ name="命令执行超时",
325
+ attachment_type=allure.attachment_type.TEXT
326
+ )
327
+ return {
328
+ 'returncode': -1,
329
+ 'stdout': '',
330
+ 'stderr': f'Command timed out after {timeout} seconds'
331
+ }
332
+ except Exception as e:
333
+ # 其他异常
334
+ allure.attach(
335
+ f"命令执行异常: {str(e)}",
336
+ name="命令执行异常",
337
+ attachment_type=allure.attachment_type.TEXT
338
+ )
339
+ return {
340
+ 'returncode': -1,
341
+ 'stdout': '',
342
+ 'stderr': str(e)
343
+ }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pytest-dsl
3
- Version: 0.3.1
3
+ Version: 0.4.0
4
4
  Summary: A DSL testing framework based on pytest
5
5
  Author: Chen Shuanglin
6
6
  License: MIT
@@ -8,13 +8,13 @@ Project-URL: Homepage, https://github.com/felix-1991/pytest-dsl
8
8
  Project-URL: Bug Tracker, https://github.com/felix-1991/pytest-dsl/issues
9
9
  Classifier: Framework :: Pytest
10
10
  Classifier: Programming Language :: Python :: 3
11
- Classifier: Programming Language :: Python :: 3.8
12
11
  Classifier: Programming Language :: Python :: 3.9
13
12
  Classifier: Programming Language :: Python :: 3.10
14
13
  Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
15
  Classifier: License :: OSI Approved :: MIT License
16
16
  Classifier: Operating System :: OS Independent
17
- Requires-Python: >=3.8
17
+ Requires-Python: >=3.9
18
18
  Description-Content-Type: text/markdown
19
19
  License-File: LICENSE
20
20
  Requires-Dist: pytest>=7.0.0
@@ -26,6 +26,7 @@ Requires-Dist: jsonpath-ng>=1.5.0
26
26
  Requires-Dist: requests>=2.28.0
27
27
  Requires-Dist: lxml>=4.9.0
28
28
  Requires-Dist: jsonschema>=4.17.0
29
+ Requires-Dist: pytz>=2023.3
29
30
  Dynamic: license-file
30
31
 
31
32
  # pytest-dsl: 强大的关键字驱动测试自动化框架
@@ -330,6 +331,31 @@ pytest test_api.py
330
331
  pytest -v --alluredir=./reports
331
332
  ```
332
333
 
334
+ ### 使用Allure生成和查看报告
335
+
336
+ pytest-dsl已与Allure报告框架集成,可以生成美观、交互式的测试报告。
337
+
338
+ ```bash
339
+ # 运行测试并生成Allure报告数据
340
+ pytest --alluredir=./allure-results
341
+
342
+ # 生成HTML报告并启动本地服务器查看
343
+ allure serve ./allure-results
344
+
345
+ # 或生成HTML报告到指定目录
346
+ allure generate ./allure-results -o ./allure-report
347
+ # 然后可以打开 ./allure-report/index.html 查看报告
348
+ ```
349
+
350
+ Allure报告会自动包含以下信息:
351
+ - 测试步骤和执行状态
352
+ - HTTP请求和响应详情
353
+ - 断言结果和失败原因
354
+ - 测试执行时间和性能数据
355
+ - 测试标签和分类信息
356
+
357
+ 通过Allure报告,您可以更直观地分析测试结果,快速定位问题。
358
+
333
359
  ## 更多功能
334
360
 
335
361
  ### 断言重试功能
@@ -59,10 +59,10 @@ pytest_dsl/keywords/__init__.py,sha256=5aiyPU_t1UiB2MEZ6M9ffOKnV1mFT_2YHxnZvyWaB
59
59
  pytest_dsl/keywords/assertion_keywords.py,sha256=H0vNCvfG3h8R3ST6C5sVTEH8OTWj3x94cAzqSNBBRLI,22827
60
60
  pytest_dsl/keywords/global_keywords.py,sha256=RZcJ9ksfXZaRvds4247LFXu-8a0LFqTvVaD4n98GUrk,1410
61
61
  pytest_dsl/keywords/http_keywords.py,sha256=oGSdLYEGypYxTw6UFA1iHPaE6hRmeaoLfEoXbP_sIaI,25543
62
- pytest_dsl/keywords/system_keywords.py,sha256=3D6htXwoW2w0B3Ywtw1EBqffkGHAaUBoapG4-oJDJQQ,496
63
- pytest_dsl-0.3.1.dist-info/licenses/LICENSE,sha256=Rguy8cb9sYhK6cmrBdXvwh94rKVDh2tVZEWptsHIsVM,1071
64
- pytest_dsl-0.3.1.dist-info/METADATA,sha256=X4FcqaJTrg8r17F20OJascFP77r4ZKYZ47m-k2KILIk,11924
65
- pytest_dsl-0.3.1.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
66
- pytest_dsl-0.3.1.dist-info/entry_points.txt,sha256=BWrYOFO9UgaDUTdS_W0G6Gl9YAj490cetXJsXrcjruA,94
67
- pytest_dsl-0.3.1.dist-info/top_level.txt,sha256=4CrSx4uNqxj7NvK6k1y2JZrSrJSzi-UvPZdqpUhumWM,11
68
- pytest_dsl-0.3.1.dist-info/RECORD,,
62
+ pytest_dsl/keywords/system_keywords.py,sha256=n_jRrMvSv2v6Pm_amokfyLNVOLYP7CFWbBE3_dlO7h4,11299
63
+ pytest_dsl-0.4.0.dist-info/licenses/LICENSE,sha256=Rguy8cb9sYhK6cmrBdXvwh94rKVDh2tVZEWptsHIsVM,1071
64
+ pytest_dsl-0.4.0.dist-info/METADATA,sha256=uiectpQ02KNfzI7Hw1wYXjTzIkS-Wv5I7blPoGCY_Uc,12691
65
+ pytest_dsl-0.4.0.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
66
+ pytest_dsl-0.4.0.dist-info/entry_points.txt,sha256=BWrYOFO9UgaDUTdS_W0G6Gl9YAj490cetXJsXrcjruA,94
67
+ pytest_dsl-0.4.0.dist-info/top_level.txt,sha256=4CrSx4uNqxj7NvK6k1y2JZrSrJSzi-UvPZdqpUhumWM,11
68
+ pytest_dsl-0.4.0.dist-info/RECORD,,