pytest-dsl 0.1.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.
- pytest_dsl/__init__.py +10 -0
- pytest_dsl/cli.py +44 -0
- pytest_dsl/conftest_adapter.py +4 -0
- pytest_dsl/core/__init__.py +0 -0
- pytest_dsl/core/auth_provider.py +409 -0
- pytest_dsl/core/auto_decorator.py +181 -0
- pytest_dsl/core/auto_directory.py +81 -0
- pytest_dsl/core/context.py +23 -0
- pytest_dsl/core/custom_auth_example.py +425 -0
- pytest_dsl/core/dsl_executor.py +329 -0
- pytest_dsl/core/dsl_executor_utils.py +84 -0
- pytest_dsl/core/global_context.py +103 -0
- pytest_dsl/core/http_client.py +411 -0
- pytest_dsl/core/http_request.py +810 -0
- pytest_dsl/core/keyword_manager.py +109 -0
- pytest_dsl/core/lexer.py +139 -0
- pytest_dsl/core/parser.py +197 -0
- pytest_dsl/core/parsetab.py +76 -0
- pytest_dsl/core/plugin_discovery.py +187 -0
- pytest_dsl/core/utils.py +146 -0
- pytest_dsl/core/variable_utils.py +267 -0
- pytest_dsl/core/yaml_loader.py +62 -0
- pytest_dsl/core/yaml_vars.py +75 -0
- pytest_dsl/docs/custom_keywords.md +140 -0
- pytest_dsl/examples/__init__.py +5 -0
- pytest_dsl/examples/assert/assertion_example.auto +44 -0
- pytest_dsl/examples/assert/boolean_test.auto +34 -0
- pytest_dsl/examples/assert/expression_test.auto +49 -0
- pytest_dsl/examples/http/__init__.py +3 -0
- pytest_dsl/examples/http/builtin_auth_test.auto +79 -0
- pytest_dsl/examples/http/csrf_auth_test.auto +64 -0
- pytest_dsl/examples/http/custom_auth_test.auto +76 -0
- pytest_dsl/examples/http/file_reference_test.auto +111 -0
- pytest_dsl/examples/http/http_advanced.auto +91 -0
- pytest_dsl/examples/http/http_example.auto +147 -0
- pytest_dsl/examples/http/http_length_test.auto +55 -0
- pytest_dsl/examples/http/http_retry_assertions.auto +91 -0
- pytest_dsl/examples/http/http_retry_assertions_enhanced.auto +94 -0
- pytest_dsl/examples/http/http_with_yaml.auto +58 -0
- pytest_dsl/examples/http/new_retry_test.auto +22 -0
- pytest_dsl/examples/http/retry_assertions_only.auto +52 -0
- pytest_dsl/examples/http/retry_config_only.auto +49 -0
- pytest_dsl/examples/http/retry_debug.auto +22 -0
- pytest_dsl/examples/http/retry_with_fix.auto +21 -0
- pytest_dsl/examples/http/simple_retry.auto +20 -0
- pytest_dsl/examples/http/vars.yaml +55 -0
- pytest_dsl/examples/http_clients.yaml +48 -0
- pytest_dsl/examples/keyword_example.py +70 -0
- pytest_dsl/examples/test_assert.py +16 -0
- pytest_dsl/examples/test_http.py +168 -0
- pytest_dsl/keywords/__init__.py +10 -0
- pytest_dsl/keywords/assertion_keywords.py +610 -0
- pytest_dsl/keywords/global_keywords.py +51 -0
- pytest_dsl/keywords/http_keywords.py +430 -0
- pytest_dsl/keywords/system_keywords.py +17 -0
- pytest_dsl/main_adapter.py +7 -0
- pytest_dsl/plugin.py +44 -0
- pytest_dsl-0.1.0.dist-info/METADATA +537 -0
- pytest_dsl-0.1.0.dist-info/RECORD +63 -0
- pytest_dsl-0.1.0.dist-info/WHEEL +5 -0
- pytest_dsl-0.1.0.dist-info/entry_points.txt +5 -0
- pytest_dsl-0.1.0.dist-info/licenses/LICENSE +21 -0
- pytest_dsl-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,111 @@
|
|
1
|
+
@name: 外部文件引用测试
|
2
|
+
@description: 演示如何在HTTP请求中使用外部文件引用
|
3
|
+
@tags: [HTTP, 文件引用, 示例]
|
4
|
+
@author: Felix
|
5
|
+
@date: 2024-04-08
|
6
|
+
|
7
|
+
# 设置测试变量
|
8
|
+
username = "张三"
|
9
|
+
email = "zhangsan@example.com"
|
10
|
+
password = "test123456"
|
11
|
+
age = 28
|
12
|
+
city = "上海"
|
13
|
+
theme = "light"
|
14
|
+
skills = "Java,Go,React"
|
15
|
+
role = "开发工程师"
|
16
|
+
current_date = "2024-04-08T12:00:00Z"
|
17
|
+
|
18
|
+
# 示例1: 使用简单文件引用语法加载静态JSON
|
19
|
+
[打印],内容:'1. 测试简单文件引用语法 - 静态JSON'
|
20
|
+
|
21
|
+
[HTTP请求],客户端:'default',配置:'''
|
22
|
+
method: POST
|
23
|
+
url: https://httpbin.org/post
|
24
|
+
request:
|
25
|
+
headers:
|
26
|
+
Content-Type: application/json
|
27
|
+
json: "@file:pytest_dsl/examples/http/data/user_data.json"
|
28
|
+
captures:
|
29
|
+
user_name: ["jsonpath", "$.json.user.name"]
|
30
|
+
user_email: ["jsonpath", "$.json.user.email"]
|
31
|
+
asserts:
|
32
|
+
- ["status", "eq", 200]
|
33
|
+
- ["jsonpath", "$.json.user.name", "eq", "测试用户"]
|
34
|
+
''',步骤名称: "使用静态JSON文件发送请求"
|
35
|
+
|
36
|
+
[打印],内容:'捕获的用户名: ${user_name}'
|
37
|
+
[打印],内容:'捕获的邮箱: ${user_email}'
|
38
|
+
|
39
|
+
# 示例2: 使用简单文件模板语法加载带变量的JSON
|
40
|
+
[打印],内容:'2. 测试简单文件引用语法 - 带变量的JSON模板'
|
41
|
+
|
42
|
+
[HTTP请求],客户端:'default',配置:'''
|
43
|
+
method: POST
|
44
|
+
url: https://httpbin.org/post
|
45
|
+
request:
|
46
|
+
headers:
|
47
|
+
Content-Type: application/json
|
48
|
+
json: "@file_template:pytest_dsl/examples/http/data/user_template.json"
|
49
|
+
captures:
|
50
|
+
user_name: ["jsonpath", "$.json.user.name"]
|
51
|
+
user_email: ["jsonpath", "$.json.user.email"]
|
52
|
+
metadata_date: ["jsonpath", "$.json.metadata.created_at"]
|
53
|
+
asserts:
|
54
|
+
- ["status", "eq", 200]
|
55
|
+
- ["jsonpath", "$.json.user.name", "eq", "${username}"]
|
56
|
+
- ["jsonpath", "$.json.metadata.created_at", "eq", "${current_date}"]
|
57
|
+
''',步骤名称: "使用带变量的JSON模板文件发送请求"
|
58
|
+
|
59
|
+
[打印],内容:'用户名被替换为: ${user_name}'
|
60
|
+
[打印],内容:'创建日期被替换为: ${metadata_date}'
|
61
|
+
|
62
|
+
# 示例3: 使用详细文件引用语法
|
63
|
+
[打印],内容:'3. 测试详细文件引用语法'
|
64
|
+
|
65
|
+
[HTTP请求],客户端:'default',配置:'''
|
66
|
+
method: POST
|
67
|
+
url: https://httpbin.org/post
|
68
|
+
request:
|
69
|
+
headers:
|
70
|
+
Content-Type: application/json
|
71
|
+
json:
|
72
|
+
file_ref:
|
73
|
+
path: "pytest_dsl/examples/http/data/user_data.json"
|
74
|
+
type: "json"
|
75
|
+
template: false
|
76
|
+
encoding: "utf-8"
|
77
|
+
captures:
|
78
|
+
skills: ["jsonpath", "$.json.user.skills"]
|
79
|
+
asserts:
|
80
|
+
- ["status", "eq", 200]
|
81
|
+
- ["jsonpath", "$.json.user.skills", "length", 4]
|
82
|
+
''',步骤名称: "使用详细文件引用语法发送请求"
|
83
|
+
|
84
|
+
[打印],内容:'捕获的技能列表: ${skills}'
|
85
|
+
|
86
|
+
# 示例4: 将外部文件引用与请求JSON部分合并
|
87
|
+
[打印],内容:'4. 测试将外部文件与请求JSON合并'
|
88
|
+
|
89
|
+
[HTTP请求],客户端:'default',配置:'''
|
90
|
+
method: POST
|
91
|
+
url: https://httpbin.org/post
|
92
|
+
request:
|
93
|
+
headers:
|
94
|
+
Content-Type: application/json
|
95
|
+
json:
|
96
|
+
file_ref:
|
97
|
+
path: "pytest_dsl/examples/http/data/user_template.json"
|
98
|
+
template: true
|
99
|
+
extra_field: "附加字段"
|
100
|
+
timestamp: ${current_date}
|
101
|
+
captures:
|
102
|
+
request_json: ["jsonpath", "$.json"]
|
103
|
+
asserts:
|
104
|
+
- ["status", "eq", 200]
|
105
|
+
''',步骤名称: "合并外部文件与请求JSON"
|
106
|
+
|
107
|
+
[打印],内容:'请求JSON已合并: ${request_json}'
|
108
|
+
|
109
|
+
@teardown do
|
110
|
+
[打印],内容:'外部文件引用测试完成!'
|
111
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
@name: HTTP请求关键字高级示例
|
2
|
+
@description: "展示HTTP请求关键字的高级用法包括会话管理,和客户端配置"
|
3
|
+
@tags: [HTTP, API, 高级, 示例]
|
4
|
+
@author: Felix
|
5
|
+
@date: 2024-04-01
|
6
|
+
|
7
|
+
# 本例使用httpbin.org作为测试API
|
8
|
+
|
9
|
+
# 创建并使用会话示例
|
10
|
+
[HTTP请求],会话:'test_session',配置:'''
|
11
|
+
method: GET
|
12
|
+
url: https://httpbin.org/cookies/set
|
13
|
+
request:
|
14
|
+
params:
|
15
|
+
session_id: abcd1234
|
16
|
+
user: testuser
|
17
|
+
asserts:
|
18
|
+
- ["status", "eq", 200]
|
19
|
+
'''
|
20
|
+
|
21
|
+
# 验证会话中的Cookie已保存
|
22
|
+
[HTTP请求],会话:'test_session',配置:'''
|
23
|
+
method: GET
|
24
|
+
url: https://httpbin.org/cookies
|
25
|
+
captures:
|
26
|
+
session_id: ["jsonpath", "$.cookies.session_id"]
|
27
|
+
user: ["jsonpath", "$.cookies.user"]
|
28
|
+
asserts:
|
29
|
+
- ["jsonpath", "$.cookies.session_id", "eq", "abcd1234"]
|
30
|
+
- ["jsonpath", "$.cookies.user", "eq", "testuser"]
|
31
|
+
'''
|
32
|
+
|
33
|
+
[打印],内容:'会话Cookie - session_id: ${session_id}, user: ${user}'
|
34
|
+
|
35
|
+
# 使用请求头
|
36
|
+
[HTTP请求],配置:'''
|
37
|
+
method: GET
|
38
|
+
url: https://httpbin.org/headers
|
39
|
+
request:
|
40
|
+
headers:
|
41
|
+
Custom-Header: test-value
|
42
|
+
User-Agent: pytest-dsl-client
|
43
|
+
captures:
|
44
|
+
custom_header: ["jsonpath", "$.headers.Custom-Header"]
|
45
|
+
user_agent: ["jsonpath", "$.headers.User-Agent"]
|
46
|
+
asserts:
|
47
|
+
- ["jsonpath", "$.headers.Custom-Header", "eq", "test-value"]
|
48
|
+
'''
|
49
|
+
|
50
|
+
[打印],内容:'请求头 - Custom-Header: ${custom_header}, User-Agent: ${user_agent}'
|
51
|
+
|
52
|
+
# 使用响应对象
|
53
|
+
[HTTP请求],配置:'''
|
54
|
+
method: POST
|
55
|
+
url: https://httpbin.org/anything
|
56
|
+
request:
|
57
|
+
json:
|
58
|
+
message: 测试消息
|
59
|
+
timestamp: 1234567890
|
60
|
+
captures:
|
61
|
+
json_data: ["jsonpath", "$.json"]
|
62
|
+
save_response: full_response
|
63
|
+
'''
|
64
|
+
|
65
|
+
# 正则表达式提取示例
|
66
|
+
[HTTP请求],配置:'''
|
67
|
+
method: GET
|
68
|
+
url: https://httpbin.org/html
|
69
|
+
captures:
|
70
|
+
title: ["regex", "<h1>(.*?)</h1>"]
|
71
|
+
asserts:
|
72
|
+
- ["regex", "<h1>(.*?)</h1>", "matches", ".*Melville.*"]
|
73
|
+
'''
|
74
|
+
|
75
|
+
[打印],内容:'HTML标题: ${title}'
|
76
|
+
|
77
|
+
# XPath提取示例
|
78
|
+
[HTTP请求],配置:'''
|
79
|
+
method: GET
|
80
|
+
url: https://httpbin.org/html
|
81
|
+
captures:
|
82
|
+
p_text: ["xpath", "//p/text()"]
|
83
|
+
asserts:
|
84
|
+
- ["status", "eq", 200]
|
85
|
+
'''
|
86
|
+
|
87
|
+
[打印],内容:'段落文本: ${p_text}'
|
88
|
+
|
89
|
+
@teardown do
|
90
|
+
[打印],内容:'HTTP高级测试完成!'
|
91
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
@name: HTTP请求关键字示例
|
2
|
+
@description: 演示HTTP请求关键字的基本用法
|
3
|
+
@tags: [HTTP, API, 示例]
|
4
|
+
@author: Felix
|
5
|
+
@date: 2024-04-01
|
6
|
+
|
7
|
+
# 基本GET请求示例
|
8
|
+
[HTTP请求],客户端:'default',配置:'''
|
9
|
+
method: GET
|
10
|
+
url: https://jsonplaceholder.typicode.com/posts/1
|
11
|
+
asserts:
|
12
|
+
- ["status", "eq", 200]
|
13
|
+
- ["jsonpath", "$.id", "eq", 1]
|
14
|
+
- ["body", "matches", "^.*"]
|
15
|
+
- ["jsonpath", "$.title", "exists"]
|
16
|
+
''',步骤名称: "使用默认客户端获取博客信息"
|
17
|
+
|
18
|
+
# 带参数的GET请求
|
19
|
+
[HTTP请求],客户端:'default',配置:'''
|
20
|
+
method: GET
|
21
|
+
url: https://jsonplaceholder.typicode.com/posts
|
22
|
+
request:
|
23
|
+
params:
|
24
|
+
userId: 1
|
25
|
+
captures:
|
26
|
+
first_post_id: ["jsonpath", "$[0].id"]
|
27
|
+
data_length: ["jsonpath", "$", "length"]
|
28
|
+
asserts:
|
29
|
+
- ["status", "eq", 200]
|
30
|
+
- ["jsonpath", "$", "type", "array"]
|
31
|
+
- ["jsonpath", "$", "length", "gte", 8]
|
32
|
+
'''
|
33
|
+
|
34
|
+
# 显示捕获的变量
|
35
|
+
[打印],内容:'第一篇文章ID: ${first_post_id}, 文章总数: ${data_length}'
|
36
|
+
|
37
|
+
# POST请求示例
|
38
|
+
[HTTP请求],客户端:'default',配置:'''
|
39
|
+
method: POST
|
40
|
+
url: https://jsonplaceholder.typicode.com/posts
|
41
|
+
request:
|
42
|
+
headers:
|
43
|
+
Content-Type: application/json
|
44
|
+
json:
|
45
|
+
title: 测试标题
|
46
|
+
body: 测试内容
|
47
|
+
userId: 1
|
48
|
+
captures:
|
49
|
+
new_post_id: ["jsonpath", "$.id"]
|
50
|
+
asserts:
|
51
|
+
- ["status", "eq", 201]
|
52
|
+
- ["jsonpath", "$.title", "eq", "测试标题"]
|
53
|
+
- ["jsonpath", "$.id", "exists"]
|
54
|
+
'''
|
55
|
+
|
56
|
+
[打印],内容:'新创建的文章ID: ${new_post_id}'
|
57
|
+
|
58
|
+
# 使用响应时间断言
|
59
|
+
[HTTP请求],客户端:'default',配置:'''
|
60
|
+
method: GET
|
61
|
+
url: https://jsonplaceholder.typicode.com/posts
|
62
|
+
captures:
|
63
|
+
response_time: ["response_time"]
|
64
|
+
asserts:
|
65
|
+
- ["response_time", "lt", 3000] # 响应时间小于3000ms
|
66
|
+
'''
|
67
|
+
|
68
|
+
[打印],内容:'请求响应时间: ${response_time}ms'
|
69
|
+
|
70
|
+
# 断言重试功能演示 - 模拟数据处理API
|
71
|
+
[打印],内容:'开始测试断言重试功能 - 模拟数据处理场景'
|
72
|
+
|
73
|
+
# 步骤1: 创建一个任务(模拟启动数据处理)
|
74
|
+
[HTTP请求],客户端:'default',配置:'''
|
75
|
+
method: POST
|
76
|
+
url: https://httpbin.org/post
|
77
|
+
request:
|
78
|
+
headers:
|
79
|
+
Content-Type: application/json
|
80
|
+
json:
|
81
|
+
task_type: "data_analysis"
|
82
|
+
dataset_id: "sample123"
|
83
|
+
parameters: {
|
84
|
+
"analysis_depth": "detailed",
|
85
|
+
"include_graphs": true
|
86
|
+
}
|
87
|
+
captures:
|
88
|
+
task_id: ["jsonpath", "$.json.dataset_id"]
|
89
|
+
timestamp: ["jsonpath", "$.headers.Host"]
|
90
|
+
asserts:
|
91
|
+
- ["status", "eq", 200]
|
92
|
+
'''
|
93
|
+
|
94
|
+
[打印],内容:'已创建分析任务,任务ID: ${task_id}'
|
95
|
+
|
96
|
+
# 步骤2: 查询任务状态 - 使用断言重试功能等待"完成"状态
|
97
|
+
# 由于这是模拟场景,我们使用 httpbin.org/delay/2 来模拟处理延迟
|
98
|
+
# 在实际场景中,API可能需要一段时间才能返回预期结果
|
99
|
+
|
100
|
+
[HTTP请求],客户端:'default',配置:'''
|
101
|
+
method: GET
|
102
|
+
url: https://httpbin.org/delay/2
|
103
|
+
request:
|
104
|
+
params:
|
105
|
+
task_id: ${task_id}
|
106
|
+
_: ${timestamp} # 添加时间戳防止缓存
|
107
|
+
captures:
|
108
|
+
url: ["jsonpath", "$.url"]
|
109
|
+
task_details: ["jsonpath", "$.args"]
|
110
|
+
asserts:
|
111
|
+
- ["status", "eq", 200]
|
112
|
+
- ["jsonpath", "$.args.task_id", "eq", "${task_id}"] # 这个断言一定会通过
|
113
|
+
- ["response_time", "lt", 1000] # 这个断言可能失败,因为延迟是2秒
|
114
|
+
''',断言重试次数: 3,断言重试间隔: 1
|
115
|
+
|
116
|
+
[打印],内容:'任务查询完成,URL: ${url}'
|
117
|
+
[打印],内容:'任务详情: ${task_details}'
|
118
|
+
|
119
|
+
# 步骤3: 演示更复杂的断言重试场景 - 使用随机数模拟不确定结果
|
120
|
+
[打印],内容:'开始测试断言重试功能 - 随机结果场景'
|
121
|
+
|
122
|
+
[HTTP请求],客户端:'default',配置:'''
|
123
|
+
method: GET
|
124
|
+
url: https://www.random.org/integers/
|
125
|
+
request:
|
126
|
+
params:
|
127
|
+
num: 1
|
128
|
+
min: 1
|
129
|
+
max: 10
|
130
|
+
col: 1
|
131
|
+
base: 10
|
132
|
+
format: plain
|
133
|
+
captures:
|
134
|
+
random_number: ["body"]
|
135
|
+
asserts:
|
136
|
+
- ["status", "eq", 200]
|
137
|
+
- ["body", "matches", "^[0-9]+$"] # 确认结果是数字
|
138
|
+
# 下面的断言大概有50%的几率成功,50%的几率失败,需要重试
|
139
|
+
- ["body", "lt", 6] # 断言随机数小于6
|
140
|
+
''',断言重试次数: 5,断言重试间隔: 2
|
141
|
+
|
142
|
+
[打印],内容:'获取的随机数: ${random_number}'
|
143
|
+
[打印],内容:'断言重试测试完成!'
|
144
|
+
|
145
|
+
@teardown do
|
146
|
+
[打印],内容:'HTTP请求测试完成!'
|
147
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
@name: HTTP请求长度捕获测试
|
2
|
+
@description: 测试HTTP请求关键字中的长度捕获功能
|
3
|
+
@tags: [HTTP, API, 长度捕获, 示例]
|
4
|
+
@author: Felix
|
5
|
+
@date: 2024-04-01
|
6
|
+
|
7
|
+
# 测试捕获数组长度
|
8
|
+
[HTTP请求],客户端:'default',配置:'''
|
9
|
+
method: GET
|
10
|
+
url: https://jsonplaceholder.typicode.com/users
|
11
|
+
captures:
|
12
|
+
# 测试直接捕获数组长度
|
13
|
+
users_count: ["jsonpath", "$", "length"]
|
14
|
+
# 测试嵌套数组长度
|
15
|
+
first_user_company: ["jsonpath", "$[0].company"]
|
16
|
+
# 测试字符串长度
|
17
|
+
first_user_name: ["jsonpath", "$[0].name"]
|
18
|
+
first_name_length: ["jsonpath", "$[0].name", "length"]
|
19
|
+
asserts:
|
20
|
+
- ["status", "eq", 200]
|
21
|
+
- ["jsonpath", "$", "type", "array"]
|
22
|
+
'''
|
23
|
+
|
24
|
+
# 显示捕获结果
|
25
|
+
[打印],内容:'用户总数: ${users_count}'
|
26
|
+
[打印],内容:'第一个用户公司信息: ${first_user_company}'
|
27
|
+
[打印],内容:'第一个用户名: ${first_user_name}'
|
28
|
+
[打印],内容:'用户名长度: ${first_name_length}'
|
29
|
+
|
30
|
+
# 测试捕获嵌套对象的字段数量
|
31
|
+
[HTTP请求],客户端:'default',配置:'''
|
32
|
+
method: GET
|
33
|
+
url: https://jsonplaceholder.typicode.com/posts/1
|
34
|
+
captures:
|
35
|
+
# 测试对象字段数量
|
36
|
+
post_fields_count: ["jsonpath", "$", "length"]
|
37
|
+
# 测试对象中特定字段的长度
|
38
|
+
title: ["jsonpath", "$.title"]
|
39
|
+
title_length: ["jsonpath", "$.title", "length"]
|
40
|
+
body: ["jsonpath", "$.body"]
|
41
|
+
body_length: ["jsonpath", "$.body", "length"]
|
42
|
+
asserts:
|
43
|
+
- ["status", "eq", 200]
|
44
|
+
'''
|
45
|
+
|
46
|
+
# 显示捕获结果
|
47
|
+
[打印],内容:'帖子字段数量: ${post_fields_count}'
|
48
|
+
[打印],内容:'标题: ${title}'
|
49
|
+
[打印],内容:'标题长度: ${title_length}'
|
50
|
+
[打印],内容:'正文: ${body}'
|
51
|
+
[打印],内容:'正文长度: ${body_length}'
|
52
|
+
|
53
|
+
@teardown do
|
54
|
+
[打印],内容:'HTTP请求长度捕获测试完成!'
|
55
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
@name: HTTP请求断言重试配置示例
|
2
|
+
@description: 演示新的retry_assertions配置式断言重试功能
|
3
|
+
@tags: [HTTP, API, 断言重试, 示例]
|
4
|
+
@author: Felix
|
5
|
+
@date: 2024-06-15
|
6
|
+
|
7
|
+
# 示例1: 使用旧版全局配置重试所有断言
|
8
|
+
[打印],内容:'示例1: 使用旧版全局配置重试所有断言'
|
9
|
+
|
10
|
+
[HTTP请求],客户端:'default',配置:'''
|
11
|
+
method: GET
|
12
|
+
url: https://httpbin.org/delay/2
|
13
|
+
retry:
|
14
|
+
count: 3
|
15
|
+
interval: 1
|
16
|
+
asserts:
|
17
|
+
- ["status", "eq", 200]
|
18
|
+
- ["jsonpath", "$.url", "contains", "httpbin.org"]
|
19
|
+
- ["response_time", "lt", 1000] # 这个断言可能会失败,因为请求延迟约2秒
|
20
|
+
''',步骤名称: "旧版全局断言重试示例"
|
21
|
+
|
22
|
+
# 示例2: 使用indices指定重试特定断言
|
23
|
+
[打印],内容:'示例2: 使用indices指定重试特定断言'
|
24
|
+
|
25
|
+
[HTTP请求],客户端:'default',配置:'''
|
26
|
+
method: GET
|
27
|
+
url: https://www.random.org/integers/
|
28
|
+
request:
|
29
|
+
params:
|
30
|
+
num: 1
|
31
|
+
min: 1
|
32
|
+
max: 10
|
33
|
+
col: 1
|
34
|
+
base: 10
|
35
|
+
format: plain
|
36
|
+
captures:
|
37
|
+
random_number: ["body"]
|
38
|
+
asserts:
|
39
|
+
- ["status", "eq", 200] # 索引0
|
40
|
+
- ["body", "matches", "^[0-9]+$"] # 索引1
|
41
|
+
- ["body", "lt", 6] # 索引2 - 需要重试
|
42
|
+
- ["response_time", "lt", 10000] # 索引3
|
43
|
+
retry_assertions:
|
44
|
+
count: 5
|
45
|
+
interval: 2
|
46
|
+
indices: [2] # 只重试索引为2的断言(即body < 6)
|
47
|
+
''',步骤名称:"特定断言重试示例"
|
48
|
+
|
49
|
+
[打印],内容:'获取的随机数: ${random_number}'
|
50
|
+
|
51
|
+
# 示例3: 使用specific为不同断言设置不同的重试参数
|
52
|
+
[打印],内容:'示例3: 使用specific为不同断言设置不同的重试参数'
|
53
|
+
|
54
|
+
[HTTP请求],客户端:'default',配置:'''
|
55
|
+
method: GET
|
56
|
+
url: https://httpbin.org/delay/1
|
57
|
+
asserts:
|
58
|
+
- ["status", "eq", 200] # 索引0
|
59
|
+
- ["response_time", "lt", 800] # 索引1
|
60
|
+
- ["jsonpath", "$.headers.Host", "eq", "httpbin.org"] # 索引2
|
61
|
+
- ["jsonpath", "$.url", "contains", "httpbin.org"] # 索引3
|
62
|
+
retry_assertions:
|
63
|
+
count: 2 # 默认重试次数
|
64
|
+
interval: 1 # 默认重试间隔
|
65
|
+
indices: [1, 2] # 只重试索引1和2的断言
|
66
|
+
specific:
|
67
|
+
"2": { # 为索引2的断言设置特定参数
|
68
|
+
count: 4,
|
69
|
+
interval: 0.5
|
70
|
+
}
|
71
|
+
# 注意:索引3不会重试,因为没有包含在indices中
|
72
|
+
''',步骤名称:"混合断言重试策略示例"
|
73
|
+
|
74
|
+
# 示例4: 使用all=true重试所有断言
|
75
|
+
[打印],内容:'示例4: 使用all=true重试所有断言'
|
76
|
+
|
77
|
+
[HTTP请求],客户端:'default',配置:'''
|
78
|
+
method: GET
|
79
|
+
url: https://httpbin.org/status/200
|
80
|
+
asserts:
|
81
|
+
- ["status", "eq", 200]
|
82
|
+
- ["response_time", "lt", 100] # 可能会失败
|
83
|
+
retry_assertions:
|
84
|
+
count: 3
|
85
|
+
interval: 1.5
|
86
|
+
all: true # 重试所有断言
|
87
|
+
''',步骤名称:"全局断言重试示例"
|
88
|
+
|
89
|
+
@teardown do
|
90
|
+
[打印],内容:'断言重试测试完成!'
|
91
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
@name: HTTP请求增强断言重试示例
|
2
|
+
@description: 演示新的独立断言重试配置结构
|
3
|
+
@tags: [HTTP, API, 断言重试, 增强配置]
|
4
|
+
@author: Felix
|
5
|
+
@date: 2024-06-01
|
6
|
+
|
7
|
+
# 示例1: 使用独立的retry_assertions配置
|
8
|
+
[打印],内容:'示例1: 使用独立的retry_assertions配置'
|
9
|
+
|
10
|
+
[HTTP请求],客户端:'default',配置:'''
|
11
|
+
method: GET
|
12
|
+
url: https://httpbin.org/delay/2
|
13
|
+
asserts:
|
14
|
+
- ["status", "eq", 200]
|
15
|
+
- ["jsonpath", "$.url", "contains", "httpbin.org"]
|
16
|
+
- ["response_time", "lt", 1000] # 这个断言可能会失败,因为请求延迟约2秒
|
17
|
+
retry_assertions:
|
18
|
+
count: 3
|
19
|
+
interval: 1
|
20
|
+
all: true
|
21
|
+
''',步骤名称: "独立断言重试配置示例"
|
22
|
+
|
23
|
+
# 示例2: 对指定索引的断言进行重试
|
24
|
+
[打印],内容:'示例2: 对指定索引的断言进行重试'
|
25
|
+
|
26
|
+
[HTTP请求],客户端:'default',配置:'''
|
27
|
+
method: GET
|
28
|
+
url: https://www.random.org/integers/
|
29
|
+
request:
|
30
|
+
params:
|
31
|
+
num: 1
|
32
|
+
min: 1
|
33
|
+
max: 10
|
34
|
+
col: 1
|
35
|
+
base: 10
|
36
|
+
format: plain
|
37
|
+
captures:
|
38
|
+
random_number: ["body"]
|
39
|
+
asserts:
|
40
|
+
- ["status", "eq", 200] # 索引0
|
41
|
+
- ["body", "matches", "^[0-9]+$"] # 索引1
|
42
|
+
- ["body", "lt", 6] # 索引2 - 需要重试
|
43
|
+
- ["response_time", "lt", 10000] # 索引3
|
44
|
+
retry_assertions:
|
45
|
+
count: 5
|
46
|
+
interval: 2
|
47
|
+
indices: [2] # 只重试索引为2的断言(即body < 6)
|
48
|
+
''',步骤名称:"指定索引断言重试示例"
|
49
|
+
|
50
|
+
[打印],内容:'获取的随机数: ${random_number}'
|
51
|
+
|
52
|
+
# 示例3: 为特定断言指定不同的重试策略
|
53
|
+
[打印],内容:'示例3: 为特定断言指定不同的重试策略'
|
54
|
+
|
55
|
+
[HTTP请求],客户端:'default',配置:'''
|
56
|
+
method: GET
|
57
|
+
url: https://httpbin.org/delay/1
|
58
|
+
asserts:
|
59
|
+
- ["status", "eq", 200] # 索引0
|
60
|
+
- ["response_time", "lt", 800] # 索引1
|
61
|
+
- ["jsonpath", "$.headers.Host", "eq", "httpbin.org"] # 索引2
|
62
|
+
retry_assertions:
|
63
|
+
# 全局设置
|
64
|
+
count: 2
|
65
|
+
interval: 1
|
66
|
+
# 针对特定断言的设置
|
67
|
+
specific:
|
68
|
+
"1": { # 对索引1的断言使用特定配置
|
69
|
+
count: 4,
|
70
|
+
interval: 0.5
|
71
|
+
}
|
72
|
+
# 不重试索引0的断言
|
73
|
+
indices: [1, 2]
|
74
|
+
''',步骤名称:"混合断言重试策略示例"
|
75
|
+
|
76
|
+
# 示例4: 只使用新式配置
|
77
|
+
[打印],内容:'示例4: 只使用新式配置'
|
78
|
+
|
79
|
+
[HTTP请求],客户端:'default',配置:'''
|
80
|
+
method: GET
|
81
|
+
url: https://httpbin.org/status/200
|
82
|
+
asserts:
|
83
|
+
- ["status", "eq", 200]
|
84
|
+
- ["response_time", "lt", 5000]
|
85
|
+
# 使用新的独立重试配置
|
86
|
+
retry_assertions:
|
87
|
+
count: 3
|
88
|
+
interval: 1.5
|
89
|
+
all: true
|
90
|
+
''',步骤名称:"纯配置式重试示例"
|
91
|
+
|
92
|
+
@teardown do
|
93
|
+
[打印],内容:'增强断言重试测试完成!'
|
94
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
@name: 使用YAML变量的HTTP请求示例
|
2
|
+
@description: 演示如何使用YAML变量文件中的客户端配置和模板
|
3
|
+
@tags: [HTTP, API, YAML, 模板]
|
4
|
+
@author: Felix
|
5
|
+
@date: 2024-04-01
|
6
|
+
|
7
|
+
# 注意:运行此测试用例需要使用 --yaml-vars 命令行参数指定vars.yaml文件
|
8
|
+
# 例如:pytest-dsl --yaml-vars pytest_dsl/examples/http/vars.yaml pytest_dsl/examples/http/http_with_yaml.auto
|
9
|
+
|
10
|
+
# 使用命名客户端配置
|
11
|
+
[HTTP请求],客户端:'jsonplaceholder',配置:'''
|
12
|
+
method: GET
|
13
|
+
url: /users/${api_test_data.user_id}
|
14
|
+
captures:
|
15
|
+
username: ["jsonpath", "$.username"]
|
16
|
+
email: ["jsonpath", "$.email"]
|
17
|
+
company_name: ["jsonpath", "$.company.name"]
|
18
|
+
asserts:
|
19
|
+
- ["status", "eq", 200]
|
20
|
+
- ["jsonpath", "$.id", "eq", ${api_test_data.user_id}]
|
21
|
+
'''
|
22
|
+
|
23
|
+
[打印],内容:'用户信息 - 用户名: ${username}, 邮箱: ${email}, 公司: ${company_name}'
|
24
|
+
|
25
|
+
# 使用模板
|
26
|
+
[HTTP请求],客户端:'jsonplaceholder',模板:'json_post',配置:'''
|
27
|
+
url: /posts
|
28
|
+
request:
|
29
|
+
json:
|
30
|
+
title: 使用模板创建的文章
|
31
|
+
body: 这是使用模板创建的测试文章内容
|
32
|
+
userId: ${api_test_data.user_id}
|
33
|
+
captures:
|
34
|
+
new_post_id: ["jsonpath", "$.id"]
|
35
|
+
'''
|
36
|
+
|
37
|
+
[打印],内容:'使用模板创建的文章ID: ${new_post_id}'
|
38
|
+
|
39
|
+
# 使用默认客户端(httpbin.org)
|
40
|
+
[HTTP请求],配置:'''
|
41
|
+
method: GET
|
42
|
+
url: /get
|
43
|
+
request:
|
44
|
+
params:
|
45
|
+
user_id: ${api_test_data.user_id}
|
46
|
+
username: ${api_test_data.username}
|
47
|
+
captures:
|
48
|
+
args: ["jsonpath", "$.args"]
|
49
|
+
asserts:
|
50
|
+
- ["status", "eq", 200]
|
51
|
+
- ["jsonpath", "$.args.user_id", "eq", "${api_test_data.user_id}"]
|
52
|
+
'''
|
53
|
+
|
54
|
+
[打印],内容:'请求参数: ${args}'
|
55
|
+
|
56
|
+
@teardown do
|
57
|
+
[打印],内容:'YAML变量和模板测试完成!'
|
58
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
@name: 新的断言重试示例
|
2
|
+
@description: 测试修改后的断言重试机制
|
3
|
+
@tags: [HTTP, API, 断言重试]
|
4
|
+
@author: Felix
|
5
|
+
@date: 2024-06-15
|
6
|
+
|
7
|
+
[打印],内容:'开始测试修改后的断言重试机制'
|
8
|
+
|
9
|
+
# 使用全局retry_assertions配置
|
10
|
+
[HTTP请求],客户端:'default',配置:'''
|
11
|
+
method: GET
|
12
|
+
url: https://httpbin.org/delay/1
|
13
|
+
asserts:
|
14
|
+
- ["status", "eq", 200]
|
15
|
+
- ["response_time", "lt", 100] # 强制失败,设置为很小的值
|
16
|
+
retry_assertions:
|
17
|
+
count: 2
|
18
|
+
interval: 1
|
19
|
+
all: true
|
20
|
+
''',步骤名称:"全局断言重试示例"
|
21
|
+
|
22
|
+
[打印],内容:'测试完成!'
|