day:28 postman——断言

一.postman 断言

1.断言再test中

状态码是否等于200:

tests["Status code is 200"] = responseCode.code === 200;

断言响应时间小于200ms:

tests["Response time is less than 200ms"] = responseTime < 200;

断言响应体包含内容:

tests["Body matches string"] = responseBody.has("string_you_want_to_search");

查看断言结果

如果不断言,只能说明接口是通的,不能说明响应体是正确的

认识下test中的功能

environment.get("variable_key");
globals.get("variable_key");
environment.set("variable_key", "variable_value");
globals.set("variable_key", "variable_value");
environment.unset("variable_key");
globals.unset("variable_key");
sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
});
test("Status code is 200", function () {
response.to.have.status(200);
});
test("Body matches string", function () {
expect(response.text()).to.include("string_you_want_to_search");
});
test("Your test name", function () {
var jsonData = response.json();
expect(jsonData.value).to.eql(100);
});
test("Body is correct", function () {
response.to.have.body("response_body_string");
});
test("Content-Type is present", function () {
response.to.have.header("Content-Type");
});
test("Response time is less than 200ms", function () {
expect(response.responseTime).to.be.below(200);
});
test("Successful POST request", function () {
expect(response.code).to.be.oneOf([201, 202]);
});
test("Status code name has string", function () {
response.to.have.status("Created");
});
var jsonObject = xml2Json(responseBody);
var schema = {
"items": {
"type": "boolean"
}
};

var data1 = [true, false];
var data2 = [true, 123];

test('Schema is valid', function () {
expect(tv4.validate(data1, schema)).to.be.true;
expect(tv4.validate(data2, schema)).to.be.true;
});

posted @ 2025-03-24 19:29  君庭  阅读(23)  评论(0)    收藏  举报