Assertions play a crucial role in API testing. When you need to verify if the response of an request meets your expectations, you have to write an assertion. In tools like Postman, assertions require writing scripts manually. Nevertheless, in Apidog, you can visually create assertions, which is extremely convenient.
Adding an assertion to post processor
In the Run tab (DESIGN Mode) or Request tab (DEBUG Mode), navigate to Post Processors.
Hover over "Add Post Processor" and select "Assertion".
![assersions.png](https://api.apidog.com/api/v1/projects/544525/resources/342852/image-preview)
Select the target for your assertion, which can be various elements in the API response such as headers, body content, or status codes. You can choose the specific element that you want to verify against your expected outcome. (For example:`$.data.status`) Please note that the root object is represented by`$`, and it works for both objects and arrays.
![image.png](https://api.apidog.com/api/v1/projects/544525/resources/348239/image-preview)
</Step>
If the target is in the JSON body, use JSONPath to extract the desired value.
Set the assertion conditions based on your testing requirements. You can define criteria such as equal to, not equal to, contains, does not contain, greater than, less than, etc.
In the assertion value field, you can input a static value or use dynamic variables in the format of `{{variable}}`.
Click on the send button to execute the assertions. You can view the results of the assertions in the right-hand side panel.
![image.png](https://api.apidog.com/api/v1/projects/544525/resources/342854/image-preview)
</Step>
:::tip[]
When performing assertions in Apidog, all data is automatically converted to STRING for comparison. While this simplifies the process in most cases, you may encounter bad cases such as 4
not being equal to 4.0
. For such specific assertions where data comparison needs to consider more intricate details, you can utilize custom scripts to write assertions manually.
:::
Setting assersions on actual response
To quickly create an assertion based on the actual response, hover over the response field(e.g., on endpoint response or test reports), and click onassertion
.
![assertion-on-actual-response.png](https://api.apidog.com/api/v1/projects/544525/resources/348240/image-preview)
Using scripts for assertions
In Apidog, you can employ the pm.test
syntax to create assertions using scripts. Apidog is compatible with Postman scripts, allowing users to leverage existing scripts seamlessly.
:::highlight purple
Learn more about scripts.
:::