接口测试——断言的使用
(一)postman中断言的使用
tests框就是用来输入断言命令的。
1、最常用的就是断言响应码
在tests框内输入如下命令,验证响应码是否为200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
2、检查响应码是否与预期集合中的某个值一致
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
3、断言响应是否包含某个字符串
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
4、断言响应为json体时某个key对应的值
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql("yh");
});
5、断言响应时间,验证响应时间是否小于500ms
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});
6、将json返回值添加到环境变量中,供其他用例使用
var jsonData = pm.response.json();
pm.environment.set("variable_key", "variable_value");
7、检查实际获取的响应体与预期结果的响应体是否一致
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
8、检查响应头中的Headers是否与预期一致
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
9、将XML格式响应转化成json对象
var jsonObject = xml2Json(responseBody);
(二)Robot Framework中断言的使用
${list_a} create list 1 a 3 21 12
${list_b} create list 2 a 4 21 21
${list_c} create list
1、Should Contain(包含)、Should Not Contain(不包含)、Should Contain x Times(包含多少次)
列表b包含2 不包含1 包含21 2次
should contain ${list_b} 2
should not contain ${list_b} 1
should contain x times ${list_b} 21 2
2、Should Be Empty(为空)、Should Not Be Empty(不为空)
列表c为空,列表a不为空
should be empty ${list_c}
should not be empty ${list_a}
3、Should Be Equal(相等)、Should Not Be Equal(不相等)
should be equal ${list_a[1]} ${list_b[1]}
should not be equal ${list_a[0]} ${list_b[0]}
4、Should Be Equal As Numbers(作为数字相等)、Should not Be Equal As Numbers(作为数字不相等)
Should Be Equal As Numbers ${list_b[0]} 2.0000
Should not Be Equal As Numbers ${list_b[0]} 2.1
5、Should Be Equal As Integers(作为整数相等) 、Should Not Be Equal As Integers(作为整数不相等)
Should Be Equal As Integers ${list_a[3]} ${list_b[3]}
Should not Be Equal As Integers ${list_a[4]} ${list_b[4]}
6、Should Be Equal As Strings(作为字符串相等)、Should Not Be Equal As Strings(作为字符串不相等)
Should Be Equal As Strings ${list_b[0]} 2
Should Be Equal As Strings ${list_b[0]} 2.0