You can use variables in API tests to chain requests, assert on dynamic values, generate random values, do data-driven testing, compare values from one response to another, and handle any scenario that require working with dynamic data.
There are two primary ways to create a variable within an API test:
- Using the variables panel in the left sidebar
- As part of the response validation for a given API request
You will typically use the variables panel to create variables with a default value which you can later override with different values that are passed to the API test from a datatable, or from previous test run within the same plan.
If you need to run an API test across multiple environments, you can use environment variables to pass environment-specific variables to an API test. Here is what you need to do:
- In the API editor, use the Add variable button in the left side panel to add your variables with a default value that will then be overridden based on the environment
- In the mabl app, go to Configuration -> Applications and click on the pencil icon next to an environment name to edit its environment varaibles. Make sure to use the same variable names as the ones you already defined in your API test.
Now, you can run the API test in the mabl cloud by choosing the desired environment and mabl will use the variable values coming from the environment.
You can perform data-driven API testing in similar way to data-driven browser testing, using variables and a datatable to manage the test data outside of the test. There are three steps to it.
- In the API editor, use the Add variable button in the left side panel to add your data-driven variables with a given default value.
- In the mabl app, go to Configuration -> Datatables to create a datatable with the same variable names that you used in your API test and assign a value for each variable based on the number of scenarios you want to test.
- Go back to your API test details page, click on the pencil icon in the top left to update the test and associate the newly created datatable with it. When you run the test in the mabl cloud, mabl will override the default variable values based on the values coming for the datatable scenario as long as the variable names match.
After you have associated the datatable with your API test, you can use the Run test button from the test details page to do an ad-hoc API test run in the mabl cloud and select to use the datatable to verify that everything works as expected.
Updated 5 months ago