测试步骤间传递数据
1.
2.
读取前置步骤的运行结果
把登录接口 (测试步骤 1) 添加到测试场景中。
把查询接口 (测试步骤 2) 添加到测试场景中。
测试步骤 2 的 “Query 参数” 需要包含测试步骤 1 返回的 token。点击测试步骤 2 “Query 参数” 中 token 字段的魔法棒图标,选择 “读取前置步骤的运行结果”。
点击插入,可以看到查询参数中插入了 
{{$.1.response.body.token}}
。点击测试场景中的 “运行” 按钮,就能成功地把数据从测试步骤 1 传递到测试步骤 2。你可以在测试报告的 “实际请求页” 中查看
通过变量语法引用前置步骤数据
{{$.1.response.body.token}}
为例:1
表示步骤 ID,可以在每个测试步骤中找到。response.body
表示前置步骤数据的位置。可以包括请求的 header、body,或者响应的 header、body 等数据。具体见下文。token
表示 body 中下一层级的 token
字段数据。你可以使用 JSONPath 语法提取想要的数据。{{variable}}
语法引用变量,而是要使用 pm.variables.get
引用前置步骤数据。比如:
语法参考
类别 | 功能 | 语法示例 |
---|---|---|
请求 | URL | {{$.<step id>.request.url}} |
路径参数 | {{$.<step id>.request.pathParam.<field name>}} | |
查询参数 | {{$.<step id>.request.query.<field name>}} | |
请求头 | {{$.<step id>.request.header.<field name>}} | |
请求体(form) | {{$.<step id>.request.body.<field name>}} | |
请求体(json) | {{$.<step id>.request.body.<field path>}} | |
响应 | 响应体 | {{$.<step id>.response.body.<field path>}} |
响应头 | {{$.<step id>.response.header.<field name>}} | |
Cookie | {{$.<step id>.response.cookie.<field name>}} | |
循环 | 元素(ForEach 循环中的数组元素) | {{$.<loop step id>.element.<field path>}} |
索引 | {{$.<loop step id>.index}} |
使用变量传递数据
把登录接口 (测试步骤 1) 添加到测试场景中。
把查询接口 (测试步骤 2) 添加到测试场景中。
在 测试步骤 2 的 Query 参数中引用变量 
{{token}}
。点击测试场景中的 “运行”按钮,就能成功地把数据 (提取出来的 token) 从测试步骤 1 传递到测试步骤 2。
常见问题
修改于 2025-02-06 03:33:25