When creating an API test, it is important to add assertions on the response of individual API requests to validate that the API returns the expected result. For example, you may want to assert that the status code of the request equals 201 or that the response body (JSON) contains a user with a specific email address. Read on to learn more about assertion options in the API Test Editor.
Create assertions in the Validation and variable assignment section
Create assertions without code
To create an assertion, click on the Assertions tab in the Validations and variable assignment section. If you're new to validating API responses, check out our best practices article on API assertions for guidance.
Default status assertion
By default, when you create a new API step in your test, mabl will automatically create an assertion that expects the status code equal 200. You can edit or remove this assertion as needed.
Assertion targets
The API Test Editor supports the following assertion targets:
Assertion target | Description |
Headers | Value of the header key |
Status | HTTP status code of the API response |
Size | Size of the API response in bytes |
JSON Body | Use JSON path language to target a specific response property |
Assertion types
The API Test Editor supports the following types of assertions:
- Equals*
- Does not equal*
- Contains*
- Is present
- Not present
- Greater than
- Greater than or equal to
- Less than
- Less than or equal to
- Starts with*
- Ends with*
- Matches regular expression
*Includes the ability to disable case sensitivity
Validating responses with scripts
If you need to handle more complex validation scenarios, or you're validating a SOAP-based API that returns an XML response body, you can use JavaScript code as described in the API script examples guide.