pytest-dsl 0.2.0__tar.gz → 0.3.1__tar.gz
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.
- pytest_dsl-0.3.1/PKG-INFO +448 -0
- pytest_dsl-0.3.1/README.md +418 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pyproject.toml +1 -1
- pytest_dsl-0.3.1/pytest_dsl/cli.py +143 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/dsl_executor.py +133 -2
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/lexer.py +23 -1
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/parser.py +111 -12
- pytest_dsl-0.3.1/pytest_dsl/core/parsetab.py +109 -0
- pytest_dsl-0.3.1/pytest_dsl.egg-info/PKG-INFO +448 -0
- pytest_dsl-0.2.0/PKG-INFO +0 -504
- pytest_dsl-0.2.0/README.md +0 -474
- pytest_dsl-0.2.0/pytest_dsl/cli.py +0 -44
- pytest_dsl-0.2.0/pytest_dsl/core/parsetab.py +0 -88
- pytest_dsl-0.2.0/pytest_dsl.egg-info/PKG-INFO +0 -504
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/LICENSE +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/MANIFEST.in +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/__init__.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/conftest_adapter.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/__init__.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/auth_provider.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/auto_decorator.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/auto_directory.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/context.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/custom_keyword_manager.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/dsl_executor_utils.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/global_context.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/http_client.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/http_request.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/keyword_manager.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/plugin_discovery.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/utils.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/variable_utils.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/yaml_loader.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/core/yaml_vars.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/docs/custom_keywords.md +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/__init__.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/assert/assertion_example.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/assert/boolean_test.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/assert/expression_test.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/custom/test_advanced_keywords.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/custom/test_custom_keywords.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/custom/test_default_values.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/__init__.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/builtin_auth_test.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/file_reference_test.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/http_advanced.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/http_example.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/http_length_test.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/http_retry_assertions.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/http_retry_assertions_enhanced.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/http_with_yaml.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/new_retry_test.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/retry_assertions_only.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/retry_config_only.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/retry_debug.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/retry_with_fix.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/simple_retry.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/http/vars.yaml +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/quickstart/api_basics.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/quickstart/assertions.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/quickstart/loops.auto +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/test_assert.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/test_custom_keyword.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/test_http.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/examples/test_quickstart.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/keywords/__init__.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/keywords/assertion_keywords.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/keywords/global_keywords.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/keywords/http_keywords.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/keywords/system_keywords.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/main_adapter.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/parsetab.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl/plugin.py +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl.egg-info/SOURCES.txt +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl.egg-info/dependency_links.txt +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl.egg-info/entry_points.txt +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl.egg-info/requires.txt +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/pytest_dsl.egg-info/top_level.txt +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/setup.cfg +0 -0
- {pytest_dsl-0.2.0 → pytest_dsl-0.3.1}/setup.py +0 -0
@@ -0,0 +1,448 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: pytest-dsl
|
3
|
+
Version: 0.3.1
|
4
|
+
Summary: A DSL testing framework based on pytest
|
5
|
+
Author: Chen Shuanglin
|
6
|
+
License: MIT
|
7
|
+
Project-URL: Homepage, https://github.com/felix-1991/pytest-dsl
|
8
|
+
Project-URL: Bug Tracker, https://github.com/felix-1991/pytest-dsl/issues
|
9
|
+
Classifier: Framework :: Pytest
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
11
|
+
Classifier: Programming Language :: Python :: 3.8
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
16
|
+
Classifier: Operating System :: OS Independent
|
17
|
+
Requires-Python: >=3.8
|
18
|
+
Description-Content-Type: text/markdown
|
19
|
+
License-File: LICENSE
|
20
|
+
Requires-Dist: pytest>=7.0.0
|
21
|
+
Requires-Dist: allure-pytest>=2.9.0
|
22
|
+
Requires-Dist: ply>=3.11
|
23
|
+
Requires-Dist: filelock>=3.17.0
|
24
|
+
Requires-Dist: PyYAML==6.0.2
|
25
|
+
Requires-Dist: jsonpath-ng>=1.5.0
|
26
|
+
Requires-Dist: requests>=2.28.0
|
27
|
+
Requires-Dist: lxml>=4.9.0
|
28
|
+
Requires-Dist: jsonschema>=4.17.0
|
29
|
+
Dynamic: license-file
|
30
|
+
|
31
|
+
# pytest-dsl: 强大的关键字驱动测试自动化框架
|
32
|
+
|
33
|
+
pytest-dsl是一个基于pytest的关键字驱动测试框架,使用自定义的领域特定语言(DSL)来编写测试用例,使测试更加直观、易读和易维护。它不仅限于API测试,更是一个可以应对各种测试场景的通用自动化框架。
|
34
|
+
|
35
|
+
## 快速入门
|
36
|
+
|
37
|
+
### 安装
|
38
|
+
|
39
|
+
```bash
|
40
|
+
# 使用 pip 安装
|
41
|
+
pip install pytest-dsl
|
42
|
+
|
43
|
+
# 或使用 uv 安装(推荐)
|
44
|
+
uv pip install pytest-dsl
|
45
|
+
```
|
46
|
+
|
47
|
+
### 第一个DSL测试
|
48
|
+
|
49
|
+
创建第一个DSL文件,命名为`hello.dsl`:
|
50
|
+
|
51
|
+
```python
|
52
|
+
message = "Hello, pytest-dsl!"
|
53
|
+
|
54
|
+
# 使用[打印]关键字输出消息
|
55
|
+
[打印],内容: ${message}
|
56
|
+
|
57
|
+
# 使用简单循环结构
|
58
|
+
for i in range(1, 3) do
|
59
|
+
[打印],内容: "循环次数: ${i}"
|
60
|
+
end
|
61
|
+
|
62
|
+
@teardown do
|
63
|
+
[打印],内容: "测试完成!"
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
### 直接运行DSL文件
|
68
|
+
|
69
|
+
安装pytest-dsl后,无需其他配置即可通过命令行直接执行DSL文件:
|
70
|
+
|
71
|
+
```bash
|
72
|
+
# 运行单个DSL文件
|
73
|
+
pytest-dsl hello.dsl
|
74
|
+
|
75
|
+
# 也可以执行目录下的所有DSL文件
|
76
|
+
pytest-dsl tests/
|
77
|
+
```
|
78
|
+
|
79
|
+
### 简单算术示例
|
80
|
+
|
81
|
+
创建`arithmetic.dsl`文件,测试基本运算:
|
82
|
+
|
83
|
+
```python
|
84
|
+
@name: "算术运算示例"
|
85
|
+
|
86
|
+
# 基本运算
|
87
|
+
a = 10
|
88
|
+
b = 5
|
89
|
+
sum = a + b
|
90
|
+
product = a * b
|
91
|
+
|
92
|
+
# 输出结果
|
93
|
+
[打印],内容: "a + b = ${sum}"
|
94
|
+
[打印],内容: "a * b = ${product}"
|
95
|
+
|
96
|
+
# 条件判断
|
97
|
+
if a > b do
|
98
|
+
[打印],内容: "a 大于 b"
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
## 基础语法
|
103
|
+
|
104
|
+
### 变量与流程控制
|
105
|
+
|
106
|
+
pytest-dsl支持变量定义、条件判断和循环结构:
|
107
|
+
|
108
|
+
```python
|
109
|
+
@name: "测试变量定义、条件判断和循环结构"
|
110
|
+
# 变量定义
|
111
|
+
name = "pytest-dsl"
|
112
|
+
version = "1.0.0"
|
113
|
+
|
114
|
+
# 条件判断
|
115
|
+
if version == "1.0.0" do
|
116
|
+
[打印],内容: "当前是正式版"
|
117
|
+
else
|
118
|
+
[打印],内容: "当前是开发版"
|
119
|
+
end
|
120
|
+
|
121
|
+
# 循环结构
|
122
|
+
count = 3
|
123
|
+
for i in range(1, ${count}) do
|
124
|
+
[打印],内容: "循环次数: ${i}"
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
### 内置关键字
|
129
|
+
|
130
|
+
DSL提供多种内置关键字满足基本测试需求:
|
131
|
+
|
132
|
+
```python
|
133
|
+
@name: 使用内置关键字
|
134
|
+
# 打印输出
|
135
|
+
[打印],内容: "测试开始执行"
|
136
|
+
|
137
|
+
# 断言测试
|
138
|
+
[断言],条件: "1 + 1 == 2",消息: "基本算术断言失败"
|
139
|
+
```
|
140
|
+
|
141
|
+
### 自定义关键字(函数)
|
142
|
+
|
143
|
+
pytest-dsl允许在DSL文件中直接定义自定义关键字,类似于编程语言中的函数:
|
144
|
+
|
145
|
+
```python
|
146
|
+
@name: "自定义关键字示例"
|
147
|
+
|
148
|
+
# 定义一个简单的关键字(函数)
|
149
|
+
@keyword 拼接字符串 (前缀, 后缀="默认后缀") do
|
150
|
+
# 直接使用关键字参数
|
151
|
+
[打印],内容: "拼接前缀: ${前缀} 和后缀: ${后缀}"
|
152
|
+
|
153
|
+
# 保存到变量中
|
154
|
+
结果变量 = "${前缀}${后缀}"
|
155
|
+
[打印],内容: "拼接结果: ${结果变量}"
|
156
|
+
|
157
|
+
# 返回结果
|
158
|
+
return ${结果变量}
|
159
|
+
end
|
160
|
+
|
161
|
+
# 使用自定义关键字
|
162
|
+
问候语 = [拼接字符串],前缀: "你好, ",后缀: "世界"
|
163
|
+
[打印],内容: ${问候语} # 输出: 你好, 世界
|
164
|
+
|
165
|
+
# 只传递必要参数,使用默认值
|
166
|
+
简单问候 = [拼接字符串],前缀: "你好"
|
167
|
+
[打印],内容: ${简单问候} # 输出: 你好默认后缀
|
168
|
+
```
|
169
|
+
|
170
|
+
自定义关键字可以保存在独立的资源文件中(`.resource`),通过`@import`导入使用:
|
171
|
+
|
172
|
+
```python
|
173
|
+
# 导入资源文件
|
174
|
+
@import: "path/to/common_utils.resource"
|
175
|
+
|
176
|
+
# 使用导入的关键字
|
177
|
+
结果 = [拼接字符串],前缀: "开始",后缀: "结束"
|
178
|
+
```
|
179
|
+
|
180
|
+
资源文件的定义示例(`common_utils.resource`):
|
181
|
+
|
182
|
+
```python
|
183
|
+
@name: 通用工具关键字
|
184
|
+
@description: 包含一些常用的工具关键字
|
185
|
+
@author: Felix
|
186
|
+
@date: 2024-06-11
|
187
|
+
|
188
|
+
@keyword 拼接字符串 (前缀, 后缀="我是默认值哦") do
|
189
|
+
# 直接使用关键字参数
|
190
|
+
[打印],内容:'拼接前缀: ${前缀} 和后缀: ${后缀}'
|
191
|
+
|
192
|
+
# 保存到变量中
|
193
|
+
结果变量 = "${前缀}${后缀}"
|
194
|
+
[打印],内容:'拼接结果: ${结果变量}'
|
195
|
+
|
196
|
+
# 返回结果
|
197
|
+
return ${结果变量}
|
198
|
+
end
|
199
|
+
|
200
|
+
@keyword 计算长度 (文本) do
|
201
|
+
# 在实际场景中,可能会使用更复杂的逻辑
|
202
|
+
[打印],内容:'计算文本: ${文本} 的长度'
|
203
|
+
长度 = 10 # 为简化示例,这里使用固定值
|
204
|
+
[打印],内容:'计算得到长度: ${长度}'
|
205
|
+
return ${长度}
|
206
|
+
end
|
207
|
+
```
|
208
|
+
|
209
|
+
## API测试示例
|
210
|
+
|
211
|
+
创建`api_test.dsl`文件进行简单的API测试:
|
212
|
+
|
213
|
+
```python
|
214
|
+
@name: "API测试示例"
|
215
|
+
@description: "演示基本的API接口测试"
|
216
|
+
@tags: ["API", "HTTP"]
|
217
|
+
|
218
|
+
# 执行GET请求
|
219
|
+
[HTTP请求],客户端: "default",配置: '''
|
220
|
+
method: GET
|
221
|
+
url: https://jsonplaceholder.typicode.com/posts/1
|
222
|
+
asserts:
|
223
|
+
- ["status", "eq", 200]
|
224
|
+
- ["jsonpath", "$.id", "eq", 1]
|
225
|
+
''',步骤名称: "获取文章详情"
|
226
|
+
|
227
|
+
# 捕获响应数据
|
228
|
+
[HTTP请求],客户端: "default",配置: '''
|
229
|
+
method: GET
|
230
|
+
url: https://jsonplaceholder.typicode.com/posts
|
231
|
+
request:
|
232
|
+
params:
|
233
|
+
userId: 1
|
234
|
+
captures:
|
235
|
+
post_count: ["jsonpath", "$", "length"]
|
236
|
+
asserts:
|
237
|
+
- ["status", "eq", 200]
|
238
|
+
''',步骤名称: "获取用户文章列表"
|
239
|
+
|
240
|
+
# 输出捕获的变量
|
241
|
+
[打印],内容: "用户文章数量: ${post_count}"
|
242
|
+
```
|
243
|
+
|
244
|
+
在实际测试文件中使用导入的关键字示例(`custom_test.dsl`):
|
245
|
+
|
246
|
+
```python
|
247
|
+
@name: 自定义关键字测试
|
248
|
+
@description: 测试自定义关键字功能
|
249
|
+
@tags: [测试, 自定义关键字]
|
250
|
+
@author: Felix
|
251
|
+
@date: 2024-06-11
|
252
|
+
|
253
|
+
# 导入资源文件
|
254
|
+
@import: "resources/common_utils.resource"
|
255
|
+
|
256
|
+
# 定义测试输入参数
|
257
|
+
前缀值 = "你好, "
|
258
|
+
后缀值 = "世界"
|
259
|
+
|
260
|
+
# 测试拼接字符串关键字
|
261
|
+
[打印],内容:'测试拼接字符串关键字'
|
262
|
+
拼接结果 = [拼接字符串],前缀:${前缀值},后缀:${后缀值}
|
263
|
+
[打印],内容:'获取到拼接结果: ${拼接结果}'
|
264
|
+
|
265
|
+
# 使用默认参数
|
266
|
+
拼接结果2 = [拼接字符串],前缀:"hello"
|
267
|
+
[打印],内容:'获取到拼接结果2: ${拼接结果2}'
|
268
|
+
|
269
|
+
# 测试第二个关键字
|
270
|
+
[打印],内容:'测试计算长度关键字'
|
271
|
+
测试文本 = "这是测试文本"
|
272
|
+
文本长度 = [计算长度],文本:${测试文本}
|
273
|
+
[打印],内容:'获取到文本长度: ${文本长度}'
|
274
|
+
|
275
|
+
# 测试断言
|
276
|
+
[断言],条件:'${拼接结果} == "你好, 世界"',消息:'字符串拼接不匹配'
|
277
|
+
[断言],条件:'${文本长度} == 10',消息:'长度不匹配'
|
278
|
+
|
279
|
+
@teardown do
|
280
|
+
[打印],内容:'自定义关键字测试完成'
|
281
|
+
end
|
282
|
+
```
|
283
|
+
|
284
|
+
## 使用YAML变量文件
|
285
|
+
|
286
|
+
创建`variables.yaml`文件管理测试配置:
|
287
|
+
|
288
|
+
```yaml
|
289
|
+
# variables.yaml
|
290
|
+
api:
|
291
|
+
base_url: "https://jsonplaceholder.typicode.com"
|
292
|
+
timeout: 30
|
293
|
+
```
|
294
|
+
|
295
|
+
执行测试时加载变量文件:
|
296
|
+
|
297
|
+
```bash
|
298
|
+
pytest-dsl api_test.dsl --yaml-vars variables.yaml
|
299
|
+
```
|
300
|
+
|
301
|
+
## 与pytest集成
|
302
|
+
|
303
|
+
除了直接使用命令行工具外,pytest-dsl还可以与pytest无缝集成,扩展测试能力。
|
304
|
+
|
305
|
+
### 创建pytest测试类
|
306
|
+
|
307
|
+
```python
|
308
|
+
# test_api.py
|
309
|
+
from pytest_dsl.core.auto_decorator import auto_dsl
|
310
|
+
|
311
|
+
@auto_dsl("./api_tests") # 加载指定目录下所有的.auto或.dsl文件
|
312
|
+
class TestAPI:
|
313
|
+
"""API测试类
|
314
|
+
|
315
|
+
该类将自动加载api_tests目录下的所有DSL文件作为测试方法
|
316
|
+
"""
|
317
|
+
pass
|
318
|
+
```
|
319
|
+
|
320
|
+
### 使用pytest运行测试
|
321
|
+
|
322
|
+
```bash
|
323
|
+
# 运行所有测试
|
324
|
+
pytest
|
325
|
+
|
326
|
+
# 运行特定测试文件
|
327
|
+
pytest test_api.py
|
328
|
+
|
329
|
+
# 使用pytest参数和插件
|
330
|
+
pytest -v --alluredir=./reports
|
331
|
+
```
|
332
|
+
|
333
|
+
## 更多功能
|
334
|
+
|
335
|
+
### 断言重试功能
|
336
|
+
|
337
|
+
对于异步API或需要一定处理时间的请求,可以使用断言重试功能:
|
338
|
+
|
339
|
+
```python
|
340
|
+
[HTTP请求],客户端: "default",配置: '''
|
341
|
+
method: GET
|
342
|
+
url: https://httpbin.org/delay/2
|
343
|
+
asserts:
|
344
|
+
- ["status", "eq", 200]
|
345
|
+
- ["response_time", "lt", 1000] # 这个断言可能失败
|
346
|
+
''',断言重试次数: 3,断言重试间隔: 1
|
347
|
+
```
|
348
|
+
|
349
|
+
### 数据驱动测试
|
350
|
+
|
351
|
+
使用CSV文件测试多组数据:
|
352
|
+
|
353
|
+
```python
|
354
|
+
@name: "批量测试"
|
355
|
+
@data: "test_data.csv" using csv
|
356
|
+
|
357
|
+
# 使用CSV数据中的"username"和"password"列
|
358
|
+
[HTTP请求],客户端: "default",配置: '''
|
359
|
+
method: POST
|
360
|
+
url: https://example.com/api/login
|
361
|
+
request:
|
362
|
+
json:
|
363
|
+
username: "${username}"
|
364
|
+
password: "${password}"
|
365
|
+
asserts:
|
366
|
+
- ["status", "eq", ${expected_status}]
|
367
|
+
'''
|
368
|
+
```
|
369
|
+
|
370
|
+
## 自定义关键字
|
371
|
+
|
372
|
+
pytest-dsl的真正强大之处在于能够轻松创建自定义关键字,扩展测试能力到任何领域:
|
373
|
+
|
374
|
+
```python
|
375
|
+
# keywords/my_keywords.py
|
376
|
+
from pytest_dsl.core.keyword_manager import keyword_manager
|
377
|
+
|
378
|
+
@keyword_manager.register('调用微服务', [
|
379
|
+
{'name': '服务名', 'mapping': 'service_name', 'description': '微服务名称'},
|
380
|
+
{'name': '方法名', 'mapping': 'method_name', 'description': '要调用的方法'},
|
381
|
+
{'name': '参数', 'mapping': 'params', 'description': '调用参数'}
|
382
|
+
])
|
383
|
+
def call_microservice(**kwargs):
|
384
|
+
"""调用内部微服务接口"""
|
385
|
+
service = kwargs.get('service_name')
|
386
|
+
method = kwargs.get('method_name')
|
387
|
+
params = kwargs.get('params', {})
|
388
|
+
context = kwargs.get('context')
|
389
|
+
|
390
|
+
# 实现微服务调用逻辑
|
391
|
+
result = your_microservice_client.call(service, method, params)
|
392
|
+
return result
|
393
|
+
```
|
394
|
+
|
395
|
+
## 完整项目结构
|
396
|
+
|
397
|
+
```
|
398
|
+
测试项目/
|
399
|
+
├── keywords/ # 自定义关键字
|
400
|
+
│ └── api_keywords.py
|
401
|
+
├── tests/ # 测试用例
|
402
|
+
│ ├── test_api.py # 使用@auto_dsl装饰器的测试类
|
403
|
+
│ └── api_tests/ # DSL测试文件目录
|
404
|
+
│ ├── login.dsl
|
405
|
+
│ └── users.dsl
|
406
|
+
├── vars/ # 变量文件
|
407
|
+
│ ├── dev.yaml # 开发环境配置
|
408
|
+
│ └── prod.yaml # 生产环境配置
|
409
|
+
└── pytest.ini # pytest配置
|
410
|
+
```
|
411
|
+
|
412
|
+
## 为什么选择pytest-dsl?
|
413
|
+
|
414
|
+
- **降低自动化门槛**:不需要专业编程技能也能编写自动化测试
|
415
|
+
- **关注测试逻辑**:不必纠结于编程细节,专注业务测试逻辑
|
416
|
+
- **统一测试框架**:通过扩展关键字包覆盖多种测试类型
|
417
|
+
- **无缝集成pytest**:兼容pytest的所有插件和功能
|
418
|
+
- **可定制性强**:通过自定义关键字实现任何特定领域的测试需求
|
419
|
+
- **旁路模式扩展**:不干扰现有测试代码,可平滑演进
|
420
|
+
|
421
|
+
## 核心优势
|
422
|
+
|
423
|
+
- **关键字驱动架构**:使用高级抽象关键字描述测试步骤,无需编写复杂代码
|
424
|
+
- **易读的DSL语法**:自然语言风格的测试描述,降低学习门槛
|
425
|
+
- **高度可扩展**:轻松创建自定义关键字满足特定领域需求
|
426
|
+
- **统一测试框架**:通过扩展关键字包支持多种测试类型
|
427
|
+
- **完整测试生命周期**:内置teardown、变量管理和断言机制
|
428
|
+
- **非侵入式设计**:以"旁路模式"扩展现有pytest项目,不影响原有测试代码
|
429
|
+
|
430
|
+
## 进阶文档
|
431
|
+
|
432
|
+
- [完整DSL语法指南](./docs/自动化关键字DSL语法设计.md)
|
433
|
+
- [创建自定义关键字](./pytest_dsl/docs/custom_keywords.md)
|
434
|
+
- [HTTP测试关键字](./docs/api.md)
|
435
|
+
- [断言关键字详解](./docs/assertion_keywords.md)
|
436
|
+
- [HTTP断言重试机制](./docs/http_assertion_retry.md)
|
437
|
+
|
438
|
+
## 贡献与支持
|
439
|
+
|
440
|
+
我们欢迎您的贡献和反馈!如有问题,请提交issue或PR。
|
441
|
+
|
442
|
+
## 许可证
|
443
|
+
|
444
|
+
MIT License
|
445
|
+
|
446
|
+
---
|
447
|
+
|
448
|
+
开始使用pytest-dsl,释放测试自动化的无限可能!
|