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:
Know your API
This article outlines a workflow for testing your company’s API with mabl. If you don’t have an API to test, try using a public API that you understand well.
Configure test settings
To create a new API test, click New test and choose API test. For your first test, it is sufficient to give the test a name and enter 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.
Add requests
Before adding your first request, expand the variables panel at the bottom of the Editor. This panel shows the variables in your test.
The variable api.url
is the base API URL that you added when you configured test settings. When you add requests, you will use the api.url
variable to write out the request endpoint.
The variables panel
To add your first request, click on the + Add step button. The API Test Editor loads a form for configuring the request, testing it out, and validating the response.
Add a simple GET request. For example, if the endpoint is https://api.example.com/users
, use mabl variable syntax for the base API URL: {{@api.url}}/users
. Don’t forget to include any required auth or parameters!
Configure your first request
Validate responses
As you test out requests, add assertions to validate that the API returns the expected data in the proper format. By default, the API Test Editor adds a 200 status code assertion to all responses, but you can update as needed in the Validation and variable assignment section of the Editor.
If you aren’t sure what to test, try implementing one of the following examples to validate core functionality of your API:
- Verify that an endpoint returns the correct HTTP status in different scenarios. Create a request with valid data to ensure the API returns a 200 response. Add additional requests with incorrect auth or missing data to verify that the API returns the appropriate error message and status codes.
- Test CRUD operations to ensure data is handled correctly. Add a request to create a resource. Store the resource ID from the response in a variable, and use it in subsequent requests to test GET, PUT/PATCH, and DELETE methods for the same resource.
While most use cases are supported by low-code assertions, you can also use scripts for more complex validations, such as validating XML payloads.
To make sure your test works as expected, click the play button in the top left corner of the Editor:
Running your steps in the Editor
Run your 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.
Learn more about different test execution environments here.
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