Javascript 如何用邮递员测试某个数字是否大于某个数字
我正在尝试在POSTMAN中进行测试,其中大小必须大于0,但我无法正确进行测试 我所做的是使它在大小小于0时失败 邮递员是否有功能检查尺寸是否大于x号Javascript 如何用邮递员测试某个数字是否大于某个数字,javascript,postman,Javascript,Postman,我正在尝试在POSTMAN中进行测试,其中大小必须大于0,但我无法正确进行测试 我所做的是使它在大小小于0时失败 邮递员是否有功能检查尺寸是否大于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){
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))
}
});
虽然我不确定你需要的精确程度,但你可以在Postman中得到一个响应大小。它由正文大小和页眉大小组成(只需在应用程序中指出大小的值)。在测试区域,您可以通过以下操作恢复身体大小:
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
然后测试这个值
pm.expect(pm.response.json().resources.length).to.be.above(0);
请参见Postman使用柴图书馆的扩展实现。 您可以在此处查看源代码: 因此,从逻辑上讲,只有当您抛出错误并被测试捕获时,测试才会失败。 否则它就会过去。 因此,expect调用实际上抛出了一个错误,使测试失败。 如果您只返回任何内容或可能不返回任何内容,那么测试也会通过 从一个简单的尝试和捕捉块的角度考虑。 因此,要立即解决您的问题,您只需抛出一个错误,您的测试就会失败 您可以这样修改代码:
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(“步骤7/获取资源和可用性列表”,函数(){
pm.expect(pm.response.code).to.be.of([200]);
如果(pm.response.code==200){
var jsonData=JSON.parse(responseBody);
var sizeOK=1;
if(jsonData.resources.length>0){
}否则{
pm.test(“响应体为空”,函数(){
if(jsonData.length<3){
抛出新错误(“预期长度大于3”);
}
});
}
log(布尔值(jsonData.resources.length>1))
}
});
要恢复的尺寸是多少?从响应主体中的元素开始?我只希望它大于0,然后我将遍历列表。嗨,A.Joly,谢谢你的回答,但你可能不明白我的意思。在测试中,我要做的是检查resources.length是否大于2,并执行如下操作:pm.expect(pm.response.json().resources.length).to.greaterThan(2);或者类似于:pm.expect(pm.response.json().resource.length>2)。但即使长度结果小于2,测试也成功啊,好吧,对不起。。。我对pm.xx方法不够熟悉。。。你试过不带柴吗?用老式的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))
}
});