响应体
{
"message": "Hi I am 'lakshmi' from 'India'"
}
该文本lakshmi
在预请求脚本中提供,我需要在响应中验证相同的内容。我不想像下面这样验证
Var message = "Hi I am 'lakshmi' from 'India'"
既然我提到lakshmi
了全局变量,我该如何在测试中进行验证
Hi I am "{{name}}" from 'India'
响应体
{
"message": "Hi I am 'lakshmi' from 'India'"
}
该文本lakshmi
在预请求脚本中提供,我需要在响应中验证相同的内容。我不想像下面这样验证
Var message = "Hi I am 'lakshmi' from 'India'"
既然我提到lakshmi
了全局变量,我该如何在测试中进行验证
Hi I am "{{name}}" from 'India'
你可以使用:
let name = pm.globals.get("name"),
jsonData = pm.response.json();
pm.test("Name is correct in the response", () => {
pm.expect(jsonData.message).to.equal(`Hi I am ${name} from 'India'`)
})
或者
let jsonData = pm.response.json()
pm.test("Name is correct in the response", () => {
pm.expect(jsonData.message).to.equal(`Hi I am ${pm.variables.replaceIn('{{name}}')} from 'India'`)
})
这段代码应该可以工作
pm.test("Status test", function () {
pm.response.to.have.status(200);
});
var expectedValue = pm.environment.get("lakshmi");
pm.test("Body contains variable", function () {
pm.expect(pm.response.text()).to.include(expectedValue);
});
// IF YOU WANT TO CHECK THE WHOLE SENTENCE
var expectedValue = "Hi I am '" + pm.environment.get("lakshmi") + "' from 'India";
pm.test("Body contains variable", function () {
pm.response.to.have.body(expectedValue);
});