What is RapidAPI Testing?
RapidAPI Testing is a cloud-based API testing tool that allows you to create comprehensive API tests (as well as monitor APIs). With RapidAPI testing, you can test all of your REST APIs. There is also support for SOAP and GraphQL APIs. This API testing tool integrates with a user’s internal or external APIs that may already be on RapidAPI.
The RapidAPI testing dashboard lets users see all the APIs connected to their account. Then, they can jump into the test suite for each API.
The API testing tool wants to make test creation easier. That’s why the service offers automatic test generation by integrating with a user or organization’s existing APIs on RapidAPI.
Also, you can build steps manually, or move steps around using the graphical user interface. In the testing tutorial, we’ll show you how to:
- use test variables
- define environments
- schedule tests
- set alerts
- monitor test analytics
I think you’ll be surprised by how intuitive it is to create your tests from new or existing APIs.
How to Test API Endpoints
Next, I am going to show you how to get started using RapidAPI Testing. If you already use RapidAPI for your APIs feel free to use existing endpoints during the tutorial. However, if you don’t have any APIs to test (or programming experience) no problem, you can still follow along to create a mock API utilizing the friendly user-interface.
If you already have a RapidAPI account and don’t need to create a mock API, jump down to Step 2 “Manually Create Your First Test”.
1. Sign Up for an Account on RapidAPI
If you don’t have an account on RapidAPI you’ll need to sign up—but don’t worry—it’s free to create an account!
Once you have created an account on RapidAPI, navigate to your provider dashboard. Once you are on the dashboard, select the Add New API option on the left sidebar navigation.
We are now ready to create an API!
(Optional) Create Mock API
The API is going to have a couple of endpoints and simulate an API that allows users to create a user, delete a user, and list a user’s information. Name the API whatever you deem appropriate, or whatever is available. I am going to choose Favorite Python Libraries. For the description add, “This is a mock API for favorite programming languages.”
Select UI for the Specify using option, and then click Add API.
After adding the API, we are taken to the definition and overview section for our new API. Here, we can add a longer description, upload an image, add base URL, and upload an Open API spec, among other options.
I am going to upload a Python logo for the API that I obtained from python.org.
Next, we need to create endpoints.
Add Your First Endpoint
First, select Add some endpoints in the tutorial steps or the Endpoints tab.
Then, select Create REST Endpoint above the empty endpoints table.
For the endpoint-name enter Add User, and for the description, enter Adds a user to the database.
In the dropdown select POST and specify that the URL for this endpoint is /add
.
Select the Body tab for this endpoint and enter the following information to specify the data payload that this endpoint should receive:
- Select the Payload option
- Name the payload data
- Enter User information for the description
- In the Example dropdown, click Add New Example
- Paste the below object into the Body example input
{ "language": "Python", "name": "Alex" }
Scroll down the bottom of the page. In the Response section:
- Select Mock Response
- Enter 200 for the Code
- Select JSON in the Body dropdown
- Add the below JSON object in the input section
{ "userId": "1", "username": "Alex", "languages": "Python", "favorites": [] }
This is the hardcoded response that we will get back when we call the endpoint.
Toggle the Enable Mock Response switch to enable this API to return the response object when called.
Finally, click Save.
The API now has an endpoint that returns a mock response. Let’s create two more endpoints to use for our tests.
Create the Delete User and View User List Endpoints
On the Endpoints tab, click Create Endpoint.
Provide the following information:
- Name the endpoint Delete User
- For the description enter Deletes User
- Change the HTTP method (using the dropdown) to DELETE
- Provide an endpoint path of
/users/{id}
- Curly braces indicate path parameters
Scroll down the bottom of the page. In the Response section:
- Select Mock Response
- Enter 200 for the Code
- Select JSON in the Body dropdown
- Add the below JSON object in the input section
{ "message": "User deleted." }
Toggle Enable Mock Response to on.
Save the endpoint.
Finally, let’s add the View User List endpoint.
On the Endpoints tab, click Create Endpoint.
Provide the following information:
- Name the endpoint View User List
- For the description, enter Gets a user’s list of favorite libraries.
- Make sure the HTTP method (using the dropdown) is set to GET
- Provide an endpoint path of
/users/{userId}/list
- Curly braces indicate path parameters
- On the Path Parameters tab, enter 1 as the example for the userId parameter
Scroll down to the bottom of the page. In the Response section:
- Select Mock Response
- Enter 200 for the Code
- Select JSON in the Body dropdown
- Add the below JSON object in the input section
{ "userId": 1, "username": "Alex", "language": "Python", "favorites": [ "requests", "selenium", "scrapy" ] }
Toggle Enable Mock Response to on.
Save the endpoint.
Well done! We are now ready to jump into the testing dashboard and start creating tests!
2. Manually Create Your First Test
Navigate to your testing dashboard.
Related: How to Manually Test APIs
Here you can view (or add) APIs connected to your personal and organizational accounts.
Select the API that you want to create tests for.
On the next page, click Create Test.
Provide a name for the test. If you are using the mock API that we created in the previous section, name this test Adding a User.
After you name the test, you’ll be taken to the user interface for building tests. There are many ways that we can configure parts of the test, however, for this test, we are going to focus on adding steps, saving tests, running tests, and viewing results.
Click Add Step and select HTTP POST from the list of available options.
We can edit the information for this step by clicking the angle down icon on the right side of the step.
For this step, we want to call the /add
endpoint for our mock API.
The URL is typically https://the-name-of-your-api.p.rapidapi.com
. However, it can also be found by viewing your API in the marketplace.
Locate API Url
Back on your provider dashboard, you can see what your API is going to look like on the marketplace by clicking View in Marketplace in the top right of the screen.
To get the URL for different endpoints, select the endpoint on the left side of the API dashboard. Then, the endpoint can be found in the sample code in the right section.
Define Test Steps
Insert the following values for the HTTP POST test step:
- URL: the URL for your API with the
/add
path attached - Variable: response
- Click the Body tab and paste in the object below.
{ "language": "Python", "name": "Alex" }
This step in the test is going to send a POST request to the /add
endpoint with the provided JSON object. The endpoint will send back a response (containing the mock response object that we defined earlier). This could be data, file, an error, etc.
Next, the returned response object will be saved in the context variable response. We are given the option to name the context variable whatever we like, and for this tutorial, I have chosen response.
Before running the test, we need to add the headers that allow us to access the API. These headers are:
- X-RapidAPI-Key
- X-RapidAPI-Host
The X-RapidAPI-key header is a secret value. Both of the values for the headers can be obtained from the API dashboard.
On the Headers tab for the POST action, select the two necessary headers by clicking the + icon, and enter the value for your API.
We are now ready to run the test and see if we can successfully reach the endpoint.
Running a Test and Viewing Results
In the top right of the page, click the Save and Run button. The test will run and should succeed.
Down in the bottom left, you can view test results by clicking on the individual test result.
In the pop-up, you are able to view test details like the amount of time the test took, the response body, response headers, and request.
This test was successful, but it only had one step. Let’s add an assertion before moving on to more complex testing scenarios.
Making Assertions
Click Add Step, and select the Assert Equals option.
We can build assertions from context variables. Recall that the response object was saved in the variable response. Therefore, we can assert that username
on the returned response object is "Alex"
by accessing the username property with the expression response.data.username
.
For the Value, enter Alex.
For the test to be successful, both of the steps need to pass. Save and run the test.
The execution report details the steps and values that were assessed.
Adding steps to the test execution is intuitive and straightforward. But, it can be tedious to add headers, URLs, and assert against many different aspects of the response. That’s why in the next section I am going to introduce how to automatically generate multi-step tests!
3. Automatically Generating Tests
Head back to the testing dashboard and create a new test named Getting User Favorites.
This time, we are going to use the Request Generator to automatically create test steps.
Click on the bar at the bottom of the screen labeled Request Generator.
The bottom drawer will appear and you’ll see the APIs endpoints on the left side of the pop-up.
- Click on the View User List endpoint
- Hit the blue Send button
You should get back a 200 status code with the mock response that we defined. Additionally, a blue Add to Test button will appear. Click the button!
In the pop-up, you can now select different types of assertions based on the actual response object that was returned. For every toggled property assertion a new step is added to the test. You can select different assertion options from the dropdown list:
- Assert property exists
- Asserting the value is of type ‘number’
- Assert it equals [value]
Click OK in the bottom right of the pop-up to add all the assertions to our test. Back on the test execution list, all of the tests have been added!
Also, the URL has automatically been added along with the necessary headers!
We can automatically generate tests for any of our endpoints and add asserts or send varying types of requests by modifying the URL Params, Headers, or Body using the Request Generator.
4. Using Test Variables
In the last section, we hardcoded the userId
parameter into multiple steps. However, this can be inconvenient if we decide to change the testing parameter or move steps around later.
To add a test context variable, click on the Settings tab in the secondary navigation menu.
On this page, there is a Test Variables section that allows us to add variables into the context of our tests.
Click the + icon and add a variable with key userId
and value 1. Click the checkmark on the right side of the input to save the variable.
Back in the editor, click the down angle on the right side of the first step to edit the initial GET request.
Instead of hardcoding the user’s ID into the URL, we can add it as a value from our test variables.
Change the value 1 in the URL to {{userId}}
.
The URL should now look something like, https://favorite-python-libraries.p.rapidapi.com/users/{{userId}}/list
.
Next, at the bottom of the test steps, add another assertion step that checks if the a.data.userId
value equals the test variable {{userId}}
.
Save and run the test.
The test should run successfully. If you check the execution report, you will see the test variable value.
5. Chaining Requests Together
REST APIs often have many endpoints that interact with the same data. Some endpoints might perform actions that have consequences for other endpoints. For this next example, we are going to test that a user is deleted. However, we are going to do this by:
- “Creating” a user using the Add User endpoint
- “Deleting” a user using the Delete User endpoint
- Finally, trying to fetch the user’s favorites
Add Call to Add User
First, create a new test and name it Deleting a User.
In the request generator:
- Select the Add User endpoint
- Click the Send button
- After a successful response, click the Add to Test button
The response to this step isn’t very important. The only assertion we want to make is that the HTTP response equals 200.
Click OK to add it to the test.
Set Test Variable Dynamically
Next, let’s add a step that takes the returned userId
parameter and saves it to a test context variable.
Select Add Step, and choose the Set Variable action.
Enter userId for the Key and a.data.userId
for the Value.
Delete the User
Click on the Request Generator again.
Select the Delete User endpoint. Provide any numerical value on the end of the URL as an example ID and then click Send.
Click Add to Test, and again, only select the assertion that checks for a value of 200 for the status code.
Modify the HTTP DELETE step so that the endpoint uses the test context variable userId
. This value is going to be passed along in the requests.
Test If User Is Still in Database
In this article, we set up a mock API. Therefore, this test is going to fail because there is no database set up yet to add or a delete a user from.
In the Request Generator, click on the View User List endpoint. This time, in the URL, insert the context variable {{userId}}. This will save us from having to change it after we add the step.
Send the response and click Add to Test. In the pop-up, toggle the Unselect All slider so that none of the assertions are added. Click OK.
In a real application, this test would return a 404 status code for Not Found. Add a final step that asserts that the status code for this response is 404.
The steps in your test should now be similar to the image below.
Notice that we have chained three API calls together and utilized dynamic test context variables.
View Test Analytics
Save and Run the test. It should fail for our mock API. The status code for the final request returns a 200 because the mock API isn’t connected to a database. Therefore, the View User Favorites returns a response object when it should return an error. Now that we have a failed test, let’s look at analytics.
To view test analytics for individual tests, click on the Analytics tab in the secondary navigation. Here, you can filter tests by the environment, location, and date.
To view test analytics for your API, click on the graph icon located in the left navigation. Here you can sort requests, and see how different tests perform given their response size.
6. Using Environments
Environments are important for any serious application or API. That’s why it’s easy to create different environments and set variables in RapidAPI Testing.
To create environments, click on the Settings icon in the left navigation.
Then click on the + icon to name your new environment. Environments show up as tabs along the top of the Test Environments section.
You define environment variables the same way you define test variables. For this example, you can change the URL for your tests by specifying the protocol and domain.
I specified two variables for each environment.
Back in the individual tests, I can modify the URLs for each request and select the environment I want to use before I execute a test.
With the environment variables, we can share variables across different tests. With test variables, we can scope values that are only important to a specific test.
7. Scheduling Tests and Setting Alerts
A side effect of good tests is peace of mind for you and your team. With RapidAPI Testing, you can schedule tests to run from different locations around the world and with different environments.
To schedule a test, select the test that you want to schedule from the dashboard. Click on the Schedule option in the side navigation. Then, select the:
- Frequency
- Environment
- Locations
Now that your test is scheduled, go to the main API test settings. This is the same location that we defined test environments and environment variables.
You can select to either receive Email or SMS alerts when a test for this API fails.
To learn more about API Testing click here.
Leave a Reply