0

响应体

{
    "message": "Hi I am 'lakshmi' from 'India'"
}

该文本lakshmi在预请求脚本中提供,我需要在响应中验证相同的内容。我不想像下面这样验证

Var message = "Hi I am 'lakshmi' from 'India'"

既然我提到lakshmi了全局变量,我该如何在测试中进行验证

Hi I am "{{name}}" from 'India'
4

2 回答 2

1

你可以使用:

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'`)
})
于 2020-06-15T16:36:05.190 回答
0

您可以使用Postman 的测试脚本。还要检查那些例子

这段代码应该可以工作

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);
});
于 2020-06-15T16:34:01.027 回答