我正在尝试在POSTMAN中进行一个测试,其中大小必须大于0,但我无法正确地进行测试。
我所做的是让它在大小小于0的时候失败。
postman中有检查大小是否大于x数字的函数吗?
pm.test("Step 7/ Getting the resources and availabilites list " , function(){
pm.expect(pm.response.code).to.be.oneOf([200]);
if(pm.response.code === 200){
var jsonData = JSON.parse(responseBody);
var sizeOK= 1;
if(jsonData.resources.length>0){
}else{
//I will make the test fail if there is not data available on the response.
pm.test("Response body is empty ", function () {
pm.expect(pm.response.json().resources.length).to.equal(1);
});
}
console.log(Boolean(jsonData.resources.length>1))
}
});发布于 2018-04-10 20:53:58
pm.expect(pm.response.json().resources.length).to.be.above(0);请参阅http://www.chaijs.com/api/bdd/
发布于 2018-04-11 01:57:20
Postman使用chai库的扩展实现。你可以在这里查看源代码:https://github.com/postmanlabs/chai-postman
因此,从逻辑上讲,只有当抛出错误并被测试捕获时,测试才会失败。否则它就会通过。所以expect调用实际上抛出了一个错误,导致测试失败。如果您只是返回任何内容,或者可能什么也不返回,那么测试也会通过。
考虑一个简单的try和catch块。因此,要立即解决您的问题,只需抛出一个错误,您的测试就会失败。
你可以这样修改你的代码:
pm.test("Step 7/ Getting the resources and availabilites list " , function(){
pm.expect(pm.response.code).to.be.oneOf([200]);
if(pm.response.code === 200){
var jsonData = JSON.parse(responseBody);
var sizeOK= 1;
if(jsonData.resources.length>0){
} else {
pm.test("Response body is empty ", function () {
throw new Error("Empty response body"); // Will make the test fail.
});
}
console.log(Boolean(jsonData.resources.length>1))
}
});此外,您还可以使用简单的javascript轻松地测试长度/大小,如下所示(例如:)
pm.test("Step 7/ Getting the resources and availabilites list " , function(){
pm.expect(pm.response.code).to.be.oneOf([200]);
if(pm.response.code === 200){
var jsonData = JSON.parse(responseBody);
var sizeOK= 1;
if(jsonData.resources.length>0){
} else {
pm.test("Response body is empty ", function () {
if(jsonData.length < 3) {
throw new Error("Expected length to be greater than 3");
}
});
}
console.log(Boolean(jsonData.resources.length>1))
}
});发布于 2017-11-21 23:36:20
虽然我不确定您需要的精确度,但您可以在Postman中获得响应大小。它由Body size和Headers组成(只需在应用程序中指向size的值)。在您的测试区域中,您可以执行以下操作来恢复身体大小:
var size=0;
for (var count in responseBody) {
if(responseBody.hasOwnProperty(count))
size += 1;
}
console.log("BODY SIZE = " + size); // you'll see the correct value in the console for the body part然后根据这个值进行测试。
https://stackoverflow.com/questions/47385993
复制相似问题