Assertions | Zoho QEngine Help

Assertions

In a nutshell
Assertions are the validation layer in API testing, determining whether the data returned in a response meets the expected outcome. You specify which part of the response to evaluate by selecting a source such as Header, Status Code, JSON Body, or XML Body, then define the expected value and an operator to perform the comparison. Multiple assertions can be added to a single test case to validate different aspects of the same API response.

Overview   

Assertions are used to validate a response. If the assertions are not satisfied, the test fails.
 
Assertions can be applied using:
  • Source: Location from where the values will be compared. You can select Header, Status code, XML body, or JSON body as the source.
  • Assertions using Header as the source can be performed with the header keys as the property against the expected value.
  • Assertions using Status code as the source can be performed by specifying the expected response code as the value.
  • Assertions using JSON body and XML body as the source can be performed by extracting data from the response using its path and specifying it as the key along with its expected value.
  • Property: The data (from the source) value that will be compared.
  • Comparison: Operators that compare actual and expected values.
  • Value: Expected value of the property.

Examples of data extraction from a JSON key-value response  

The following is a sample JSON key-value response:
 
  1. {
  1.    "id":1,
  1.    "name":"Leanne Graham",
  1.    "username":"Bret",
  1.    "address":{
  1.       "city":"Gwenborough",
  1.       "geo":{
  1.          "lat":"-37.3159",
  1.          "lng":"81.1496"
  1.       }
  1.    },
  1.    "phoneNumbers":[
  1.       {
  1.          "type":"home",
  1.          "number":"000-111-2222"
  1.       },
  1.       {
  1.          "type":"mobile",
  1.          "number":"111-222-3333"
  1.       },
  1.       {
  1.          "type":"office",
  1.          "number":"333-444-5555"
  1.       }
  1.    ]
  1. }
 
Assertion Property
Actual Value
username
Bret
address.city
Gwenborough
phoneNumbers[0].number
000-111-2222
phoneNumbers[-1].number
333-444-5555
phoneNumbers[*].number
000-111-2222 (or) 111-222-3333 (or) 333-444-5555
 
Examples of data extraction from a JSON index-value response
  
The following is a sample JSON index-value response:
 
  1. [
  1.       {
  1.          "type":"home",
  1.          "number":"000-111-2222"
  1.       },
  1.       {
  1.          "type":"mobile",
  1.          "number":"111-222-3333"
  1.       },
  1.       {
  1.          "type":"office",
  1.          "number":"333-444-5555"
  1.       }
  1.    ]
 
Assertion Property
Actual Value
[1].number
111-222-3333
[-1].type
office
 
XPATH Expressions
  
The following is a sample of XPath expressions:
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  1. <root>
  1.    <address>
  1.       <city>Gwenborough</city>
  1.       <geo>
  1.          <lat>-37.3159</lat>
  1.          <lng>81.1496</lng>
  1.       </geo>
  1.    </address>
  1.    <id>1</id>
  1.    <name>Leanne Graham</name>
  1.    <phoneNumbers>
  1.       <element>
  1.          <number>000-111-2222</number>
  1.          <type>home</type>
  1.       </element>
  1.       <element>
  1.          <number>111-222-3333</number>
  1.          <type>mobile</type>
  1.       </element>
  1.       <element>
  1.          <number>333-444-5555</number>
  1.          <type>office</type>
  1.       </element>
  1.    </phoneNumbers>
  1.    <username>Bret</username>
  1. </root>
 
Assertion Property
Actual Value
/root/username/text()
Bret
/root/address/city/text()
Gwenborough
/root/phoneNumbers/element[1]/number/text()
000-111-2222
/root/phoneNumbers/element[3]/number/text()
333-444-5555
/root/phoneNumbers/element/number/text()
000-111-2222 (or) 111-222-3333 (or) 333-444-5555
 
Comparisons
  
The following operators let you compare actual and expected values. The comparison field is a dropdown menu from which you can select the required operator.
 
  1. Equals
  1. Not Equals
  1. Contains
  1. Not Contains
  1. Is Empty
  1. Is Not Empty
  1. Lesser Than
  1. Greater Than
  1. Lesser Than or Equal To
  1. Greater Than or Equal To