Need help creating assertions for test

I’m new to Postman and do not have any coding experience. How would I go about creating assertions to verify values for “name”, “latitude”, “upperLimit” and “lowerLimit” in the code below?

{
@type”: “AntennaConfig”,
“id”: “9e22f537-0f68-4caf-9d98-445555578d27”,
“name”: “RPM_Antenna_1”,
“inService”: true,
“changeTime”: 0,
“rfPathDelayNS”: 0,
“latitude”: 42.321,
“altitude”: 62.125,
“longitude”: 105.781,
“type”: “RPM”,
“feeds”: ,
“pedestalConfig”: {
“id”: “4b25d95d-600e-466f-890f-50e84e292e44”,
“icdImpl”: “DE-3002”,
“rotorConfigs”: {
“EL”: {
“id”: “804e33c4-d959-425d-becd-fd9c52ded61e”,
“upperLimit”: 250.0,
“lowerLimit”: -90.0,
“installationDate”: null,
“offset”: 5.0,
“stowPosition”: 25.0,
“rotorProperties”: {}
},

What kind of assertions do you need? To verify that the values are exactly that? Or do you need to verify just that the fields exist and are of a specific data type?

The simplest form is matching the exact values you have, but it won’t work with other responses (since the values will likely change)

const jsonData = pm.response.json();
pm.test('Has correct values', function(){
  pm.expect(jsonData).has.property('name', 'RPM_Antenna_1');
  pm.expect(jsonData).has.property('latitude', 42.321);
  pm.expect(jsonData).has.property('upperLimit', 250.0);
  pm.expect(jsonData).has.property('lowerLimit', -90.0);
});

Using pm.expect this way will verify that your response has the specific properties with the passed in values.

There are ways you can make this dynamic and richer as time goes on, like parameterizing the variable values, or validating the response against a schema. But just give it time for you to understand the pieces and how everything fits together.

Thank you, Allen for the response. However, I am encountering the error shown below when running the assertion. Any further help you can provide would be appreciated.

FAIL
Has correct values | AssertionError: expected { Object (@type, id, …) } to have property ‘upperLimit’

Does the response have the upperLimit property?

Yes, the response is what I have in my first question above.

@Straub2148 is this a public facing API that we can use or an internal project? if it’s public facing can we get the URL and the relevant GET/POST information please?

{
   "@type":"AntennaConfig",
   "id":“9e22f537-0f68-4caf-9d98-445555578d27”,
   "name":“RPM_Antenna_1”,
   "inService":true,
   "changeTime":0,
   "rfPathDelayNS":0,
   "latitude":42.321,
   "altitude":62.125,
   "longitude":105.781,
   "type":"RPM",
   "feeds":,
   "pedestalConfig":{
      "id":“4b25d95d-600e-466f-890f-50e84e292e44”,
      "icdImpl":“DE-3002”,
      "rotorConfigs":{
         "EL":{
            "id":“804e33c4-d959-425d-becd-fd9c52ded61e”,
            "upperLimit":250.0,
            "lowerLimit":-90.0,
            "installationDate":null,
            "offset":5.0,
            "stowPosition":25.0,
            "rotorProperties":{
               
            }
         },

@allen.helton I could be wrong, but doesn’t the expect have to follow the JSON path down?

e.g.

pm.expect(jsonData).has.property('pedestalConfig.rotorConfigs.EL.upperLimit', 250.0);

or

pm.expect(jsonData. pedestalConfig.rotorConfigs.EL.upperLimit).to.equal(250.0);

@Straub2148

pm.expect(jsonData).has.property('pedestalConfig.rotorConfigs.EL.upperLimit', 250.0);

or

pm.expect(jsonData. pedestalConfig.rotorConfigs.EL.upperLimit).to.equal(250.0);

The error for the second case is less clear than in the first, if the property is missing.
In the first case, it will tell you the property can’t be found.
In the latter case, it will tell you that undef does not equal 250.0

But basically, yah, you need to add the complete json node path.

const jsonData = pm.response.json();
pm.test('Has correct values', function(){
  pm.expect(jsonData).has.property('name', 'RPM_Antenna_1');
  pm.expect(jsonData).has.property('latitude', 42.321);
  pm.expect(jsonData).has.property('pedestalConfig.EL.upperLimit', 250.0);
  pm.expect(jsonData).has.property('pedestalConfig.EL.lowerLimit', -90.0);
});

@Straub2148 try the above and let us know :slight_smile:

Yup, thanks guys, using dot notation to trace down the nested objects is absolutely the way to go.

I couldn’t tell the objects were nested the way it was pasted in the OP. :man_shrugging:

This is an internal project.

Encountered the error shown below when trying the new assertion.

FAIL
Has correct values | AssertionError: expected { Object (@type, id, …) } to have property ‘pedestalConfig.EL.upperLimit’

@Straub2148 please try what I have put in my post:

pm.expect(jsonData).has.property('pedestalConfig.rotorConfigs.EL.upperLimit', 250.0);

That error clearly shows you are missing .rotorConfigs

Wendy was confirming my post and is missing this path

Let me know

Hey @Straub2148 ,

@JBird20 is right on this.

To help you in the future for working with json, if you’re trying to get to a specific value, you need to add a dot every time you see a curly brace “{”


So for your specific example, the way to get to upperLimit is:

pedestalConfig.rotorConfigs.EL.upperLimit

I’m still encountering the error when using this:

const jsonData = pm.response.json();

pm.test(‘Has correct values’, function(){

pm.expect(jsonData).has.property(‘pedestalConfig.rotorConfigs.EL.upperLimit’, 250.0);

});

Error: Has correct values | AssertionError: expected { Object (@type, id, …) } to have property ‘pedestalConfig.rotorConfigs.EL.upperLimit’