javascript 如何使用邮递员测试某物是否大于某个数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47385993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test if something is greater than certain number with POSTMAN
提问by mavi
I'm trying to make a test in POSTMAN where the size has to be greater than 0 but I haven't been able to make the test correctly.
我正在尝试在 POSTMAN 中进行测试,其中大小必须大于 0,但我无法正确进行测试。
What I did was to make it fail when the size is smaller than 0.
我所做的是在大小小于 0 时让它失败。
Is there a function in postman to check if the size is greater than x number?
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))
}
});
回答by anti_gone
pm.expect(pm.response.json().resources.length).to.be.above(0);
回答by Sivcan Singh
Postman uses an extended implementation of the chai library. You can check out the source code here: https://github.com/postmanlabs/chai-postman
Postman 使用 chai 库的扩展实现。你可以在这里查看源代码:https: //github.com/postmanlabs/chai-postman
So logically the test only fails when you throw an error and the test catches it. Else it simply passes. So the expect calls actually throw an error which makes the test fail. If you just return anything or maybe return nothing, even then the test would pass.
所以从逻辑上讲,只有当您抛出错误并且测试捕获它时,测试才会失败。否则它只是通过。所以期望调用实际上抛出一个错误,使测试失败。如果你只是返回任何东西或者可能什么都不返回,即使这样测试也会通过。
Think in terms of a simple try and catch block. So, to solve your problem instantly you can just throw an error and your test would fail.
考虑一个简单的 try 和 catch 块。因此,要立即解决您的问题,您只需抛出一个错误,您的测试就会失败。
You can modify your code like so:
您可以像这样修改代码:
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))
}
});
Also, you can additionally use simple javascript to test the length / size easily like so (just an eg.):
此外,您还可以使用简单的 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))
}
});
回答by A.Joly
Though I'm not sure the degree of precision you need, you get a response size in Postman. It is made of Body size and Headers size (just point on Size's value in the app). In your test area, you can recover Body size doing:
虽然我不确定您需要的精确度,但您可以在 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
and then test against this value ...
然后针对此值进行测试...

