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

I want to integrate Postman Newman API tests into CICD, so the test results should always be passed (o

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"));
    });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744753810a4591758.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信