簡體   English   中英

Postman:如何在 Postman 自動化中檢查字段是否返回 null

[英]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();

結果:


PASS 地點 ID 為空

PASS subTypeExtra 為空

嘗試這個:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM