最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Postman conditional tests if json body array is empty = skip tests - Stack Overflow

programmeradmin1浏览0评论

I want to integrate Postman/ Newman API tests into CICD, so the test results should always be passed (or skipped). Therefor I want to use conditional tests, dependent on the data of the response.

I tried the method described on GitHub, but the condition in my case is very different.

So if the json body of the response contains an empty array, tests should be skipped. If not, perform tests...

Empty data

{
    "data": []
}

Testable data

{
    "data": [
        {
            "key1": "value1",
            "key2": {
                "amount": 1357,
                "unit": "units"
            },
            "from": "2019-08-01",
            "to": "2019-08-31",
        }
    ]
}

Test script

let response = JSON.parse(responseBody);

pm.test("Status code is 200", function() {
  pm.expect(pm.response.code).to.equal(200);
});

(pm.expect(pm.response.json().data).to.be.empty === true ? pm.test.skip : pm.test)('Body is empty', function () {
    pm.environment.set("key2Amount", response.data[0].key2.amount);
    var key2Amount = pm.environment.get("key2Amount");

    pm.test("Response includes corresponding amount", function () {
       pm.expect(pm.response.json().data[0].key2.amount).to.eql(key2Amount);
    });
});

Empty data: TypeError: Cannot read property 'key2' of undefined.

Testable data: AssertionError: expected [ Array(1) ] to be empty.

I've also tried it with

(pm.expect([]).to.be.an('array').that.is.empty ? pm.test : pm.test.skip)

Testable data: Tests performed positive.

Empty data: TypeError: Cannot read property 'key2' of undefined. Why not skipped?

Further

(pm.expect([]).to.be.empty ? pm.test.skip : pm.test)

Empty data: skipped tests

Testable data: skipped tests

What would be the correct condition on the array to make the tests run or skipped?

I want to integrate Postman/ Newman API tests into CICD, so the test results should always be passed (or skipped). Therefor I want to use conditional tests, dependent on the data of the response.

I tried the method described on GitHub, but the condition in my case is very different.

So if the json body of the response contains an empty array, tests should be skipped. If not, perform tests...

Empty data

{
    "data": []
}

Testable data

{
    "data": [
        {
            "key1": "value1",
            "key2": {
                "amount": 1357,
                "unit": "units"
            },
            "from": "2019-08-01",
            "to": "2019-08-31",
        }
    ]
}

Test script

let response = JSON.parse(responseBody);

pm.test("Status code is 200", function() {
  pm.expect(pm.response.code).to.equal(200);
});

(pm.expect(pm.response.json().data).to.be.empty === true ? pm.test.skip : pm.test)('Body is empty', function () {
    pm.environment.set("key2Amount", response.data[0].key2.amount);
    var key2Amount = pm.environment.get("key2Amount");

    pm.test("Response includes corresponding amount", function () {
       pm.expect(pm.response.json().data[0].key2.amount).to.eql(key2Amount);
    });
});

Empty data: TypeError: Cannot read property 'key2' of undefined.

Testable data: AssertionError: expected [ Array(1) ] to be empty.

I've also tried it with

(pm.expect([]).to.be.an('array').that.is.empty ? pm.test : pm.test.skip)

Testable data: Tests performed positive.

Empty data: TypeError: Cannot read property 'key2' of undefined. Why not skipped?

Further

(pm.expect([]).to.be.empty ? pm.test.skip : pm.test)

Empty data: skipped tests

Testable data: skipped tests

What would be the correct condition on the array to make the tests run or skipped?

Share Improve this question asked Sep 25, 2019 at 9:29 ZenoZeno 531 gold badge1 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

Could you use something like this:

let response = pm.response.json();

pm.test("Status code is 200", function() {
  pm.expect(pm.response.code).to.equal(200);
});

let skipTest = (response.data === undefined || response.data.length === 0);

(skipTest ? pm.test.skip : pm.test)('Body is empty', function () {
    pm.environment.set("key2Amount", response.data[0].key2.amount);

    pm.test("Response includes corresponding amount", function () {
      pm.expect(response.data[0].key2.amount).to.eql(pm.environment.get("key2Amount"));
    });
});
发布评论

评论列表(0)

  1. 暂无评论