With variables, you can assert on dynamic values, compare values from one response to another, generate random values, do data-driven testing, and handle other scenarios that require working with dynamic data.
This article explains how to work with variables in the API Test Editor, including how to...
- Create variables from a response
- Add placeholders for shared variables
- Create variables with randomly generated values
- Use variables in API tests
How to create variables from a response
Follow these steps to create a variable from the response of an API request:
- Expand the Validation and variable assignment section in the step builder.
- Select the Variables tab.
- Click on the Add button.
- Enter a description for your variable.
- Select a variable target. If you select "Header", enter the header key. For a "JSON body", type the JSON path to the target response property.
- Give the variable a name in the Assign to variable input field.
Creating a variable from the response body
When you send the request again, the variable appears in the Variables panel on the left. You may use variables in consecutive test steps or share them with subsequent tests in the same test plan.
How to add placeholders for shared variables
If the API test is inheriting shared variable values from an earlier test in a plan, create data-driven variables with placeholder values:
- In the variables panel, click on the Add row button.
- Enter a variable name.
- Give the variable a default value.
During local and ad hoc runs, the variable uses the default value. During plan runs, the default value is overridden by the shared variable value as long as the conditions for shared variables are met.
How to create variables with randomly generated values
While API tests can use mabl variable syntax ({{@variable_name}}
), they do not support the other valid expressions: macros, faker values, and math expressions. However, you can use Postman dynamic variables in API tests thanks to the mabl integration with Postman.
Using Postman dynamic variables in a request
How to use variables
Use mabl variable syntax to reference a variable in a request or in a response validation.
For example, the variable existing_user_id
becomes {{@existing_user_id}}
in the following request:
The following assertion verifies that the response value matches a value from a DataTable scenario. The DataTable variable first_name
becomes {{@first_name}}
.
Using variables to assert an expected value