Postman断言

1、断言:让程序判断预期结果和实际结果是否一致。

(1)特点

  • Postman的断言是使用JavaScript语言编写的,写在Tests标签页里
  • Tests中的脚本在发送请求之后执行,会把断言的结果(PASS/FAIL)最终在Test Results标签页中展示

(2)常用断言

  • Status code:Code is 200      //判断响应状态码是否等于200
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
  • Response body:Contains string    //判断响应体中是否包含指定的字符串
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
  • Response body:Is equal to a string    //判断响应体是否完成等于某个字符串
pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});
  • Response body:JSON value check    //校验响应的json数据
pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});
  • Response headers:Content-Type header check  //断言响应头信息
pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});

 

原文地址:https://www.cnblogs.com/wangzicong/p/15654964.html