postman的tests-snippets环境变量
时间: 2023-09-05 10:14:17 浏览: 129
Postman中的测试脚本和环境变量可以一起使用,以便在测试脚本中使用环境变量的值。以下是一些有用的测试片段,涉及到环境变量。
- 检查环境变量是否被设置:
pm.test("Check if environment variable is set", function () {
pm.expect(pm.environment.get("variableName")).to.not.eql(null);
});
- 将环境变量值赋给变量:
var variableValue = pm.environment.get("variableName");
- 使用环境变量中的值作为请求参数:
// GET request example
pm.sendRequest({
url: "https://api.example.com/users",
method: "GET",
headers: {
"Authorization": "Bearer " + pm.environment.get("accessToken")
}
}, function (err, res) {
// Handle response
});
- 检查响应中是否包含环境变量的值:
pm.test("Check if response contains environment variable value", function () {
pm.expect(pm.response.json().id).to.eql(pm.environment.get("userId"));
});
- 将环境变量值保存到全局变量:
pm.globals.set("globalVariableName", pm.environment.get("environmentVariableName"));
相关推荐













