3

I am attempting to write tests in Postman for the first time. I am using the pm.test method containing pm.expect.

Here is my test:

//contract details tests
pm.test("Contract data is correct", function() {
        pm.expect(pm.response.json().results.contractNb).to.equal("00002");
        pm.expect(pm.response.json().results.progSrvcNm).to.equal("009");
});

My response appears like so:

{
    "contractNb": "00002",
    "progSrvcNm": "009",
    "contractPartyNm": "testContract",
    "terms": 30,
    "startDt": "2018-01-01"
}
n-verbitsky
  • 552
  • 2
  • 9
  • 20
jwatkins
  • 61
  • 2
  • 8
  • What is the full response body? Is `results` an array of objects? – Danny Dainton Nov 27 '18 at 17:36
  • I cannot include the full response data as it contains private customer data, but yes it is an array in the format { "contractNb": "00002", "progSrvcNm": "009", "parameter": "value" } – jwatkins Nov 27 '18 at 18:46
  • I found an answer from you Danny on this question. https://stackoverflow.com/questions/49586400/postman-test-scripts-inspecting-contents-of-response-json?rq=1 Thanks! – jwatkins Nov 27 '18 at 19:26
  • OK, you could of just masked all the sensitive pieces of information. It wasn't the data I was asking about, it was the structure of it. That answer is a bit old now and I bit there is a cleaner way of doing it. Can still help if you update the question with the Data. – Danny Dainton Nov 27 '18 at 20:15
  • Ok, I have edited the response body to reflect test data. – jwatkins Nov 27 '18 at 20:43
  • If that's the whole response body and nothing else, I would just remove the `results` part. – Danny Dainton Nov 27 '18 at 21:02

2 Answers2

3

Given your response body data - If you just remove the .results part of the expect statement, the check will pass.

pm.test("Contract data is correct", () => {
    pm.expect(pm.response.json().contractNb).to.equal("00002")
    pm.expect(pm.response.json().progSrvcNm).to.equal("009")
})

Postman

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • Thank you very much! I got the example using the results from the Postman example page. Going forward, should I ever use that? – jwatkins Nov 28 '18 at 13:39
-1

The correct code was as Danny Dainton posted.

pm.test('Contract details are correct for the passed in contract ID', () => {
    pm.expect(pm.response.json().contractNb).to.equal("00002");
    pm.expect(pm.response.json().progSrvcNm).to.equal("009");
});
n-verbitsky
  • 552
  • 2
  • 9
  • 20
jwatkins
  • 61
  • 2
  • 8