5

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?

Zeno
  • 53
  • 1
  • 1
  • 3

1 Answers1

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"));
    });
});
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • 1
    Thanks Danny for the quick reply! It seems to be working perfectly at first sight! – Zeno Sep 25 '19 at 11:11
  • No worries, I was a bit unsure a out what you're testing. Is that just getting the value from the response and then checking it against the value of the response - That would always pass right? – Danny Dainton Sep 25 '19 at 12:23
  • That's literally it. I'm testing with live data, so the response values differ each time I send the request. I don't have a better idea to test it, do you? We have separate unit tests with mocked data, so code is mostly covered as well. – Zeno Sep 26 '19 at 07:23
  • It's not really a test as it's checking against its own value, whatever is returned is going to pass so it would never fail. Its never going to tell you anything new. Its like saying that you `pm.expect(1).to.equal(1)` - that will always be true. Is the value important? Or is the fact that its not null, undefined, a number etc. – Danny Dainton Sep 26 '19 at 07:42
  • True story. But it only applies to these kind of tests, I have others that make explicit values necessary in the response. But yeah, I better should test the type of values (string, number, date, ...). – Zeno Sep 26 '19 at 07:58
  • I'm only concerned about that particular test as I think it's not doing anything being in there, if all it's doing is checking itself. You will find greater value in checking that its not a string or maybe within a certain range if that's a potential risk factor. – Danny Dainton Sep 26 '19 at 08:12
  • instead of assigning JSON, you can directly use: pm.response.length === undefined; – torina Mar 23 '20 at 14:08