[英]Postman: How to check whether the field is returning null in the Postman automation
我试过!== null
,但即使字段返回0
或空字符串,它也会返回 PASS 。
这适用于 2019 年 3 月:
pm.test("To Check if Value is Null", function() {
var jsonData = pm.response.json();
pm.expect(jsonData.<yourfield>).not.eq(undefined);
)};
你试过了吗
pm.expect(response.your_field).to.eql(null);
?
如果您正在检查列表中返回的第一项的 Id,则可以使用not.equal(null)
:
pm.expect(pm.response.json().value[0].Id).not.equal(null);
请注意,“equal”这个词是完全拼写出来的,尽管缩短的“eql”确实有效。
试试这个:
pm.test("your-value is not null", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.your_value).not.eql(null);
});
Postman 不会将不存在的路径引用为 null,而是未定义。
pm.expect(JsonResponse.FAKE.PATH).not.eql(undefined);
这个测试应该失败,因为这个假的 json 路径实际上是未定义的。
我遇到了类似的问题。 但是以以下方式检查它对我有用
tests["Item is not null"] =
jsonData.item !== undefined;
您可以访问响应 json,如:
var json = JSON.parse(responseBody);
var yourVAr = json.yourVar
if(yourVar == null){
//your var is null here
}
我做过类似的,这是代码的一部分,它运行良好检查此代码
var jsonData = JSON.parse(responseBody);
for(i=0; i<jsonData.data.length; i++){
tests["due date is between given date range"] = jsonData.data[i].duedate < environment.Cenddate && jsonData.data[i].duedate > environment.Cstartdate;
tests["response body has department name"] = jsonData.data[i].department.name !== null;
}
怎么样:
var jsonData = JSON.parse(responseBody);
tests["Item is not null"] =
jsonData.item !== null &&
jsonData.item !== ' ' &&
jsonData.item !== 0;
您必须将 + [i] 放在要在 tests[] 上验证的函数之后,这样只有它才会在每个数组上返回有效语句。 例如,
function checkIsNull() {
var items = json.data.pois.items
var PlaceIdIsNull = true;
var subTypeExtraIsNull = true;
var websiteIsNull = true;
for (var i = 0; i < items.length; i++) {
if (items[i].placeId !== null) {
PlaceIdIsNull = false;
}
if (items[i].subTypeExtra !== null) {
subTypeExtraIsNull = false;
}
if (items[i].website === null) {
websiteIsNull = false;
tests['website is null only happened on these arrays' + [i]] = true;
}
tests['Place ID is null '] = PlaceIdIsNull
tests['subTypeExtra is null '] = subTypeExtraIsNull
}
}
checkIsNull();
尝试这个:
let jsonData = pm.response.json();
pm.expect(jsonData["yourfield"]).to.eq(null);
这适用于 2022 年 9 月 19 日
pm.test("Check that response body is not null", ()=>{
pm.response.to.be.have.body
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.