接口测试笔记-postman( 四 )


用例集导出、导入
导出
导入
断言断言简介常用断言 1. 断言响应状态码
code: Code is 200
在 Tests 标签中,选中Code:code is 200,生成对应代码适当调整 test() 方法参数1,和 匿名函数中的 预期结果 。点击 send 按钮,发送请求,执行断言代码 。查看断言结果 。
// 断言响应状态码 是否为 200pm.test("Status code is 200", function () {pm.response.to.have.status(200);});pm:代表 postman 的一个实例test():是 pm实例的一个方法 。有两个参数参数1:在断言成功后,给出的文字提示 。可以修改 。"Status code is 200"参数2:匿名函数 。pm.response.to.have.status(200); // 意思:postman 的响应结果中应该包含状态码 200200 ——> 预期结果!
2. 断言响应体是否包含某个字符串
body:
// 断言响应体包含指定字符串pm.test("Body matches string", function () {pm.expect(pm.response.text()).to.include("string_you_want_to_search");});pm:postman的一个实例test(): postman实例的方法,有两个参数参1:断言后显示的文字提示信息,可改 。参2:匿名函数pm.expect(pm.response.text()).to.include("string_you_want_to_search"); // 意思:pm 期望 响应文本 中,包含 xxxx 字符串 。"string_you_want_to_search" ——> 预期结果 。可以修改
3. 断言响应体是否等于某个字符串(对象)
body: Is equal to a
// 断言 响应体 等于某个字符串(对象)pm.test("Body is correct", function () {pm.response.to.have.body("response_body_string");});pm.response.to.have.body("response_body_string");// 意思是,pm 的 响应中应该有 响应体 xxx"response_body_string" ——> 预期结果 。可以修改
4. 断言JSON数据
body: JSON value check
// 断言json的响应结果pm.test("Your test name", function () {var jsonData = http://www.kingceram.com/post/pm.response.json();pm.expect(jsonData.value).to.eql(100);});var jsonData = pm.response.json();// var jsonData: 用js语法定义一个变量 。jsonData 就是变量名// pm.response.json();代表响应的json结果/* 举例:response.json();{"success": true,"code": 10000,"message": "操作成功!","data": "95c78d75-721c-40fb-b2d5-742fea42cbd5"}*/pm.expect(jsonData.value).to.eql(100);// 意思:pm 预期 json结果 key对应的值 等于 xxx// to.eql(100); 中的 100 代表预期结果 。可以修改的 。/* 举例:jsonData.value 的value:取 :success、code、message、data*/
示例:
// 断言json的响应结果-success的值为truepm.test("断言响应结果success的值为true", function () {var jsonData = http://www.kingceram.com/post/pm.response.json();pm.expect(jsonData.success).to.eql(true);});pm.test("断言响应结果中code的值为10000", function () {var jsonData = http://www.kingceram.com/post/pm.response.json();pm.expect(jsonData.code).to.eql(10000);});pm.test("断言响应结果中message的值为 操作成功", function () {var jsonData = http://www.kingceram.com/post/pm.response.json();pm.expect(jsonData.message).to.eql("操作成功!");});
5. 断言响应头
: -Typecheck
// 断言响应头pm.test("Content-Type is present", function () {pm.response.to.have.header("Content-Type");});pm.response.to.have.header("Content-Type");// pm 的响应 头中包含 Content-Type// 示例:可以在 header 中,添加 响应头中的 key 对应的 value 判定 。用,隔分 。// 断言响应头pm.test("Content-Type is present", function () {pm.response.to.have.header("Content-Type", "application/json;charset=UTF-8");});