Assertions | Zoho QEngine Help

Assertions

Overview 

Assertion validates whether the response from the API matches an expected one. The response can be compared using various operators to verify if they satisfy expected outcomes. 
 
Assertions can be applied using:
  • Source: Specifies which aspects of the response or what type of response are being compared or evaluated. You can select the source as Header, Status Code, XML Body, or JSON Body.
  1. Assertions using Header as the source can be performed with the header keys as property against the expected value.
  1. Assertions using Status Code as the source can be performed by specifying the expected response code as the value.
  1. 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.
  • PropertyThis captures the data retrieved from the API response that will be compared. 
  • Value: Expected value of the property.
  • Comparison: Operators to compare actual and expected values.

Note: Ideally, when performing assertions with comparisons, the data type of the actual value derived from the given property should match the data type of the expected value. If there is a mismatch, the assertion will fail.

In the example below, the required operators are applied to values, comparing them to determine whether the response aligns with the expected outcome.



The following table lists the respective operators for each data type: 

Data Types
Applicable Operators
Boolean
  • Equals - Verifies if a boolean value returned by the API matches an expected value.

  • Not Equals - Verifies that a boolean value returned by the API does not match a specific value.

Number
  • Equals - Verify if a numerical value returned by the API matches an expected value.

  • Not Equals - Verifies that a numerical value returned by the API does not match a specific value.

  • Lesser Than - Verifies if a numerical value returned by the API is less than a specified threshold.

  • Lesser Than Or Equal To - Checks if a numerical value returned by the API is less than or equal to a specified threshold.

  • Greater Than - Verifies if a numerical value returned by the API exceeds a specified threshold.

  • Greater Than Or Equal to - Checks if a numerical value returned by the API is greater than or equal to a specified threshold. 

String
  • Equals - Verifies if a string returned by the API matches an expected string exactly.

  • Not Equals - Checks if a string returned by the API does not match a specified string.

  • Contains - Verifies if the string returned by the API contains the expected string.

  • Not Contains - Checks if the string returned by the API does not contain the expected string.

  • Is Empty - Validates if a string returned by the API is empty (has zero characters).

  • Is Not Empty - Checks if the string returned by the API is not empty (contains one or more characters).

  • Starts With - Verifies if the string returned by the API starts with the specified prefix.

  • Ends With - Validates if a string returned by the API ends with the specified suffix.

  • Length - Validates the length of a string returned by the API against the expected length.

Note: Operators used to perform comparison for the string data type are case-sensitive.
JSON
  • Array Count - Verifies the count of elements in the expected JSON array returned by the API.

  • Has Key - Verifies whether a specific key from the API response exists in the expected JSON response.

    Note: Comparison using the "Has Key" operator is case-sensitive.

  • Is Null - Verifies if a specific value from the API response is null in the expected JSON response.

  • Is Not Null - Validates that a specific value from the API response is not null in the JSON response.

  • Schema Validation - Ensures that the data from the API response conforms to the structure and data types defined in the expected JSON.

  • Object Equals - Checks that the received JSON and the expected JSON are identical in structure, values, data types, and keys.

Note: Comparison using the "Object Equals" operator is case-sensitive.

Regex
Matches - This is used for Regex validation. A Regex pattern is given in the value field, and the value associated to the specified key returned from the JSON response is validated with this pattern.


Examples of data extraction from a JSON key-value response

The following is a sample JSON key-value response

  1. {
  2.    "id":1,
  3.    "name":"Leanne Graham",
  4.    "username":"Bret",
  5.    "address":{
  6.       "city":"Gwenborough",
  7.       "geo":{
  8.          "lat":"-37.3159",
  9.          "lng":"81.1496"
  10.       }
  11.    },
  12.    "phoneNumbers":[
  13.       {
  14.          "type":"home",
  15.          "number":"000-111-2222"
  16.       },
  17.       {
  18.          "type":"mobile",
  19.          "number":"111-222-3333"
  20.       },
  21.       {
  22.          "type":"office",
  23.          "number":"333-444-5555"
  24.       }
  25.    ]
  26. }

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. [
  2.       {
  3.          "type":"home",
  4.          "number":"000-111-2222"
  5.       },
  6.       {
  7.          "type":"mobile",
  8.          "number":"111-222-3333"
  9.       },
  10.       {
  11.          "type":"office",
  12.          "number":"333-444-5555"
  13.       }
  14.    ]

Assertion Property
Actual Value
[1].number
111-222-3333
[-1].type
office

XPATH Expressions

The following is a sample for XPath expressions:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <root>
  3.    <address>
  4.       <city>Gwenborough</city>
  5.       <geo>
  6.          <lat>-37.3159</lat>
  7.          <lng>81.1496</lng>
  8.       </geo>
  9.    </address>
  10.    <id>1</id>
  11.    <name>Leanne Graham</name>
  12.    <phoneNumbers>
  13.       <element>
  14.          <number>000-111-2222</number>
  15.          <type>home</type>
  16.       </element>
  17.       <element>
  18.          <number>111-222-3333</number>
  19.          <type>mobile</type>
  20.       </element>
  21.       <element>
  22.          <number>333-444-5555</number>
  23.          <type>office</type>
  24.       </element>
  25.    </phoneNumbers>
  26.    <username>Bret</username>
  27. </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

Related Links