When creating your API tests, it is important that you 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 200 and that the response body (JSON) contains a user with a specific email address.
By default, when you create a new API step in your test, mabl will automatically create an assertion that expects the status code to be equal to 200. You can change this assertion from Validation and variable assignment section for that API step.
You can add new assertions from the Assertions tab of the Validation and variable assignment section. For example, let's say you want to assert that the response body for an API request contains a user with a specific email address. To create this assertion, click on the Add button. You will see an assertion card with all available options. Select JSON body as a target for your assertion and type the JSON path to the target response property that should contain the specific email address. In our case this is
some vision path. It is best to send the API request to see what the path to the target response property should be. Once you specify the JSON path, you can select the assertion type (e.g. equals, contains) and specify the expected value.
You can also choose to assign target property value to a variable which can be used in consecutive test steps or shared with other tests within the same test plan. Simply, go to the Variables tab, repeat the steps above to get the target value that you want assigned to a variable and give the variable a name. When you send the request again, you will see the variable appear in the variables section of the left-side panel.
Response body validation
Following the above approach, you can create as many assertions and variables as needed for each step of your API test. Thus, when you run the API test, if one of the assertion fails, then the test run will be marked as failed.
You can easily create assertions and variables based on the following response data:
- Status code
Supported assertion types are:
- Does not equal
- Does not contain
- Is present
- Not present
Updated 9 months ago