In mabl, you can set up tests to validate that your API is working as expected. Besides verifying that your API returns responses in the correct format with the expected data, API testing can play a critical role in your team’s overall testing workflow, including:
- Detecting bugs in your underlying API before they cause issues in your application
- Setting up and tearing down data for your UI tests
- Validating the performance and scalability of your application’s backend under load
If you’re just getting started, use the steps outlined in this article to create your first API test and select an appropriate next step based on your goals.
Plan out your test
To create a new API test, click New test and choose the API test type. Name your test, and describe what you want it to validate in the prompt, such as CRUD operations or error handling. For example:
“Validate that the get orders endpoint returns the correct error response for unauthorized, forbidden, and not found requests: 401, 403, and 404 respectively.”
Add an API specification and/or API description for your test:
- API spec: upload a file in JSON, YAML, or plain text format that describes how your API works, such as a GraphQL or OpenAPI schema, Postman collection, or code file.
- API description: add any information that isn’t captured in uploaded files, such as authentication instructions or a sample cURL request and response. The more information the better.
Before creating the test, make sure to add the base API URL in the Core settings section. The base API URL is the root URL of the API you want to send requests to. For example, if you’re testing the mabl API, the base API URL is https://api.mabl.com.
Click Create test to launch the API Test Editor.
When the API Test Editor launches, mabl’s AI uses the test prompt and API spec to generate your test. Requests use the variable {{@api.url}}, which is the base URL that you added when you configured test settings.
If you’re looking for something different, modify the test prompt and click on Generate new outline. When you’re satisfied with the outline, click on Create test to load the generated steps and finish building out the test.
Finish building out the test
The API Test Editor shows generated requests on the right. Click a request to see its method, URL, headers, body, and assertions on the left. Update any settings as needed, such as authentication or assertions.
Check auth settings
If your API requires authentication, make sure the test provides the correct auth. To apply the same auth to all requests, click on the pencil in the top left corner to edit test properties.
To apply auth to a specific request, open the Auth tab in the Request details section of the editor.
To learn more, check out the article on adding auth settings to API tests.
Review assertions
Assertions are how mabl checks for the expected results, such as checking that a request returns a 201 response. To review assertions for a specific step, click on the request in the editor and scroll to the Variables & validation section.
To make sure your test works as expected, click the play button at the top of the editor:
Run the test
When you’re finished, save the test and close the API Test Editor.
To run your test, click on the Run test button and choose from one of the following options:
- Cloud run (recommended): execute the test from the mabl cloud and review detailed test output in the app.
- Local run: execute the test from your local machine and review results in an output window. Use this option if your API is not accessible from the public Internet and you haven’t set up mabl Link.
Read the test execution overview to learn more about different test execution environments.
Next steps
Depending on your current setup and what you want to accomplish, next steps could include learning more about the following:
- Getting started
- Advanced API testing
- Integrating with UI tests