Employing GitHub Actions and RapidAPI for API Testing

Wed Aug 17 2022

16 min read

APIs are widely used and their testing is crucial when having the shift-left approach. There are countless products and tools available on the market.

However, we'll look at how to start using RapidAPI in this guide. Why is RapidAPI unique from other tools? In particular, we'll look at RapidAPI Testing's GitHub Actions for API Testing.

What is RapidAPI?

A hackathon served as the inspiration for RapidAPI's founding in 2015. It was created by developers for developers and serves as a one-stop shop for APIs. It is the biggest API hub in the world, where you can look for APIs, analyze them, incorporate them, and even sell your services to customers.

Mobile Applications Example

Let's say you are creating a website or a mobile app and you want to display certain data. For instance, it might be information about the cost of flights, the weather, movies, or anything else. All you have to do is go to the RapidAPI Hub, search for the API using the keyword, choose it based on its popularity, latency, and service level, subscribe to it, copy the snippet, and paste the code into your app.

Moreover, you can use a variety of features like RapidAPI Enterprise, RapidAPI Design, etc. from RapidAPI. It is also simple to integrate the requirements into your website or mobile application.

RapidAPI Services

RapidAPI features a variety of products. The RapidAPI Hub, where you can browse, explore, and examine the APIs from hundreds of providers, would be a good place to start. RapidAPI for Teams provides team functionality, such as sharing and collaboration. You can also choose Enterprise Hub for enterprise services like a private workspace, access control, CI/CD features, etc.

RapidAPI Testing makes it possible to develop, execute, schedule, monitor, report on, and test APIs. RapidAPI allows you to streamline cooperation and speed up API delivery. You can see the summarized services provided by RapidAPI as follows:

  • RapidAPI Marketplace
  • Search API
  • Integrate
  • RapidAPI for Teams
  • Share
  • Collaborate
  • RapidAPI Enterprise Hub
  • white-labeled marketplace
  • developers, customers, and partners
  • RapidAPI Testing
  • testing
  • performance monitoring
  • RapidAPI Provider
  • API design

Getting Started with RapidAPI Testing

There are numerous ways to produce your tests, but this guide is solely concerned with RapidAPI Testing.

Request Generator

You can start by using the request generator, which supports a variety of HTTP methods. You can then quickly add your tests or use the visual editor to add steps by clicking the add step button followed by building your requests and validating them. You can also use faker, labor, etc., or write the JavaScript code directly.

Execution Report

When you create and run your tests, an Execution Report with a permalink and other details will be displayed. It will also exhibit the assertion and get values as illustrated in the figure below. Clicking on "Show details" will expand and reveal more information about your request and response.

Monitor Performance

RapidAPI Testing will display a dashboard plotting the response size, the number of requests, and latency if you want to monitor your performance. The X axis on the dashboard represents the size of the response, while the Y axis represents the number of requests sent. Furthermore, the larger the bubble, the slower the request. This will allow you to monitor the performance of your APIs and optimize their performance.

RapidAPI GitHub Action

RapidAPI comes with the GitHub Action, where you can integrate your RapidAPI tests in your API GitHub repo if you're leveraging the CI/CD platform within GitHub. So this particular GitHub action will trigger the RapidAPI tests in GitHub itself.

yaml
name: Execute Tests
id: tstExec
uses: RapidAPI/gh-api-testing-trigger@master
with:
test: 'YOUR TEST ID'
location: 'AWS-US-WEST-2'
environment: 'ENV ID (OPTIONAL) '

It requires three inputs:

  • First is the ID of the test you want to execute.
  • The second is the test location where you want to execute your test.
  • Lastly, you have to specify the environment where you want to execute the test.

As a result, this GitHub action will run your test within the GitHub action server. The report and other details will then be displayed. Let's dive into the demo section right away so you can see how to use the GitHub action and generate tests using RapidAPI.

Create an application in RapidAPI

Let's start creating an application on the RapidAPI platform. You can simply go to RapidAPI Hub and Sign up.

You may create a free account by clicking on the login button. After that, you can register with GitHub, Facebook, or Google. For this guide, I will use my GitHub login information to access my RapidAPI account.

You will see RapidAPI Hub here after logging in. The dashboard displays the public and private APIs. If you attempt to develop your own APIs, they will be listed in the private API section. Additionally, you can turn on and off your dark and light modes in the top left corner. Likewise, you can search for the APIs in the search box.

Numbers API - Example

Let's create an app called Numbers and click on save. So this will create the new application and also it'll create the application key.

Furthermore, you could also see the analytics of this newly made API.

You can find this app Numbers using the search bar. When you open it, you'll find documentation with interesting facts about this API. The endpoints available for this API will be displayed on the left side.

On the right side, you can also see your Code Snippets and Example Responses. Simply scrolling down will reveal additional details such as required parameters, optional parameters, and so on. Clicking on the Test Endpoint button will reveal the results and it will be shown on the right side of the screen.

Now let me choose a different year, let's say 2000, and then I'll click the Test Endpoint button. The new year's fact will now be displayed in the Results. Additionally, the Test Endpoint will show different outputs if you keep clicking it.

Similar to this, you can choose the endpoints according to your requirements to obtain a random fact or a mathematical fact. To incorporate this specific call into your application, simply choose the language you want to use. I'll use the (Python) Requests library as an illustration, and this will show the code.

Simply click the copy code button, and the necessary code will be copied to the clipboard for quick integration. Using the marketplace in this way allows you to connect your application or mobile apps quite rapidly. Let's now look at how to test your RapidAPI.

Add new API

Let's now examine how you can create an app on the RapidAPI dashboard and distribute them to customers and suppliers. The first step is to click on My APIs on the top right. This particular dashboard will therefore assist you in configuring the alerts, branding your service, etc.

The New API can now be added, we will give it the name Numbers in this guide. After giving it a name, you must write a short description and include information about the category — for example, tool.

Additionally, you can also choose how the API should be created. It supports multiple import methods such as postman collection, open API, GraphQL Schema, and Kafka. You can choose any option that best suits your design and press the Add API button.

Add a base URL

The next step is to Add a base URL. Here you need to configure the endpoints, methods, and other details.

Testing

After you add the base URL, you can see the option of Testing at the left side of the dashboard. So if you click on Testing, it will open the RapidAPI Testing editor. Here you can create your tests and monitor the performance of your APIs.

Sample Test

Let’s take an example to understand this. To make it simple, I have created one sample test using the HTTP Bin service for elaboration.

Let's launch the HTTP Bin app. It will show a list of the tests that I have created. When you open the test, it will display the number of APIs as well as the details of the steps.

Creating a Test suite

Let's return to RapidAPI Testing. By clicking the Create Test button, we will begin a new test. So, here, enter a new name, say HTTP Bin Tests, and then click OK. This will create a HTTP Bin Tests.

Step 1

You can now generate your requests in a number of ways. The first approach involves using a Visual Editor, the second method involves using the Request Generator, and the third option entails entering code directly into a Code Editor.

Let's create an API request right away by selecting Add Step.

Step 2

When you click on the Add Step button, a list of options will appear. Details about your Data Fetch, Asserts, Flow Control, and Dynamic Values must be provided here.

Choose your HTTP method in the Data Fetch option from the methods listed in this column. You can use logical conditions or loop control to control your flow or loop for an array, respectively. Furthermore, you can use any option in the Dynamic Values to decode or encode your JSON web token.

Step 3

Let me now select the HTTP GET data fetch option. Some mandatory information must be entered here. It will display the URL to be tested, the variable where you want your data to be stored, URL Params, Headers, and the Body.

Step 4

Let's open httpbin.org, which is a simple HTTP request and response service that we will use inside RapidAPI Testing. If you scroll down, you'll see a lot of of testing methods available, such as PATCH, POST, PUT, and so on.

Choose the GET method, then click Try it out and Execute. You can see the request URL as well as the response body.

Step 5

We'll copy the Request URL and return to HTTP Bin Test and paste it over here. After adding the URL, we will click the Save and Run buttons to test the API. You can also use the shortcut keys for this purpose, such as ctrl + s to save and ctrl + r to run. When you run your HTTP test, the results will be displayed on the left side.

The method is changed to Success in the Recent Results section, indicating that the response was successfully received. It will also display the location of your request generation, which is N. Virginia by default in this case.

Step 6

When you click on a result, you'll see information like CREATED, ID, TEST, ENVIRONMENT, LOCATION, and EXECUTION TIME. You can also see the Response Body, Response Headers, and Request details by clicking on the Show Details button.

Thus, this is a very basic HTTP get request. We can also use the other methods, such as assertions.

Step 7

Let's make a simple HTTP request with the Request Generator. You can enter the HTTP URL in the Request Generator, for example, https://httpbin.org/anything/testing*. Then, click the Send button to see the output in the Response.

You can also use the POST method by selecting it from the drop down menu. You must also select the BODY type, in this case JSON, and write the following code:

json
{
"message": "hello"
}

When you click Send, the corresponding response will appear. Additionally, if you wish to create the Step for this specific request, you can do so by selecting the Add to Test button in the right bottom corner.

Step 8

The variable name for your response will be displayed once you click Add to Test; by default, it will say apiResponse. You can also view a lot of information, including the assertions you want to insert.

We'll unselect everything first, then toggle the status to on. We will change the default type in Assertion type to "Assert it equals 200." Likewise, we will validate the data by selecting data.data and changing the Assertion type to Assert it equals {"message": "hello"}.

After you've chosen the appropriate assertions, click OK to see the assertions and request step that have been added to your HTTP Bin tests. Everything is now working properly if you click on Save and Run.

This powerful editor now allows you to save the output of one response and send another request. You must chain your requests to accomplish this, but first you must save your response so that you can read it in subsequent requests.

Return to the previous tests and choose the HTTP Bin. To illustrate, I created the request chaining and run this specific test. After you run it, you will see the output in the form of a Execution Report, which contains all of the values you entered earlier.

Step 9

The next step is to copy the uuid value from the Assert.exists heading. Then, under the Http.post heading, click Show Details to display the code snippet in the response body. As shown below, we will paste the value into the 'data' field. The entire procedure is known as chaining.

Let me expand the chaining to illustrate this further. We will go to HTTP POST request and the apiResponse in the variable heading of HTTP GET will be sent in the body of this particular POST request.

This will be fetched from this GET call and then it'll be passed along with the POST request. So this is how you can create a chaining flow for your APIs.

Step 10

You can also store specific static values as variables. If you click on settings, you can upgrade the test variables for this specific test, such as baseURL:httpbin.org and protocol:http.

Furthermore, you can map this variable in your tests by using double curly braces in your editor. When you enter a variable name, it will refer to the variables in your test variables.

If you click on analytics, it will show you how many successful runs, how many failed runs, and so on.

When you click on the Results button, the detailed results will be displayed.

You can also check the performance of your API by going back and clicking on the performance. The X axis represents response size, and the Y axis represents the number of requests. You can also see the circles; the large circle indicates that this particular request was poorly performed. As a result, the larger the bubble, the smaller the request.

Integrating with GitHub Actions

It is now time to integrate this test in our GitHub actions. RapidAPI has a free GitHub Actions which will help you to run your tests in GitHub servers. To add the RapidAPI GitHub Action, go to its page on GitHub Marketplace. .

From the code snippet on the RapidAPI GitHub Action page, you can see that only three things need to be passed. You have to pass ID of your test, location and the environment. You can use variables to display these details in your show results step.

Step 1

To leverage this particular testing in GitHub action, the first step is to click on the Use latest version and copy the snippet in your GitHub repository.

Step 2

Let's click on the plus (+) icon in the top right corner to create a new GitHub repository. We'll choose a name for the repository, such as RapidAPI-Testing, check the box next to Add a README file, and then click Create repository.

Step 3

To use the GitHub action, click on the Actions inside the repository. Now click on Configure button.

Step 4

The next step is to launch the Marketplace and do a RapidAPI search. Type “RapidAPI/gh-api-testing-trigger” into the search box. You can copy the example that is given in the repository and paste it inside the yml file.

Enter your test ID and rename the file to, say, api-testing.yml. Remove the existing template, then put the following code in its place.

yaml
on: [push]
jobs:
run_api_test:
runs-on: ubuntu-latest
name: Execute RapidAPI API Tests
steps:
- name: Execute Tests
id: tstExec
uses: RapidAPI/gh-api-testing-trigger@v0.0.2
with:
test: 'YOUR_TEST_ID'
location: 'AWS-US-WEST-2'
environment: 'ENV_ID(OPTIONAL)'
- name: Show Results
run: echo "The test took ${{ steps.tstExec.outputs.time }}ms to run"; echo "The test result was ${{ steps.tstExec.outputs.computedStatus }}"; echo "View Report - ${{ steps.tstExec.outputs.reportUrl }}"

Step 5

You must include your test Id in the above yml. Go to your test suite in RapidAPI Testing and copy the test Id from the query parameter test=.

Now paste this test ID inside your yml file. You can optionally configure your environment and location. This can be done as shown in the figure below:

Step 6

After configuring the yml, click on Start Commit and then Commit new file. This initiates your GitHub action workflow. Click the Actions tab, then the workflow run button.

We can view the runtime log in the console by clicking on the Execute RapidAPI Tests button. Wait a few seconds for the test to finish and you will see that it was successful. If you expand each step, you can see the log information.

Step 7

When you expand the Execute Tests button, it will show a URL. The report will open if you click the URL in the Execution Test option. As a result, this report will show information such as the status of your result, among other things.

You can see the Response Body, Response Headers, and other details by simply clicking on show details.

Conclusion

RapidAPI Testing allows you to seamlessly integrate your test suites with the developer environment. Multiple integrations, alerts, environments, and test variables are also supported. API monitoring aids in the delivery of APIs by leveraging the performance and analytics dashboard.