An API, or Application Programming Interface, is a set of methods, procedures, and functions that allow for an application to communicate and interact with other applications. After an API is built, it’s necessary to test it to make sure it’s working properly.
This is where API testing comes along.
API testing is different from UI testing in that it concentrates on the use of software to send API “calls”, receive outputs, and log the system responses. And behind every successful test is, of course, a tester. For most testers, an API testing tool is the most efficient way to check the application for any bugs and errors.
How to Automate API Testing in 3 Easy Steps
1. Get an API testing tool.
As we’ve mentioned above, API testing is best done with certain tools to make things go easier and faster. There are several API testing tools that are available online:
- JMeter
- Spotlight
- Test Manager
- Postman
- Newman
Before we start with using the tools, don’t be surprised if the names of the buttons differ slightly. This is because different tools can have different layouts and names for buttons, but how they function remains the same. Let’s get started.
Once you have the tool, setup the environment configuration. You should have the option to create a new environment, which you can label as “Dev Test” (assuming you’d like to start testing on Dev environment). Next, add key “host” and value as the test environment URL.
2. Create the first test and run it.
Before you create the first test, you should get familiar with the different API call types used in the testing procedure:
- PUT: for endpoints that update the existing data.
- GET: for endpoints that fetch data.
- DELETE: for endpoints that remove data.
- POST: for endpoints that add data.
Let’s start by running an API request. With the environment you’ve already created, you can press “Send” to forward the request to API, or you can press “Add API Test” to save the request.
Take note that API requires more input to perform the request, such as Headers, parameters, Body (JSON), and more. To add parameters, look for the “Parameters” section and add the required information.
Sending API Requests with Authentication
Your hosted API may need authentication. If that’s the case, you can search for the Authorization tab, select “BasicAuth” (this may have a different name on your tool), and then provide the Username and Password. You can now send authenticated requests.
Adding Assertions
When automating API tests, it’s crucial that you verify the output using assertions. To add an assertion, you’ll need to choose the response type, the assertion’s conditions, and input the value you want to have checked.
Variables
This is useful for keeping values that are received as a response from API requests. Simply add a variable, give it a name so the rest of the team can understand it, and place the JSON path.
Once you’ve successfully created the first test, save it and run it.
3. Run the tests from the command line.
You’ve just finished running the first test at this point. The next and final step is to run the test from the command line. For this, a lot of people use Newman, which is a program that runs collections written in JavaScript. Using Newman, export the test collections and environment files and save them on your local drive. Next, run the test collection from the command line.
There you go. You’ve just automated your API testing.
Wrapping it up
After reading the above guide and creating your first API test, you’ve now expanded your knowledge on API test automation. There’s still plenty more to learn however, especially because API testing can cover many different aspects of testing. In our opinion, the next step is to understand how test scripts go with the development code, and how to automate tests using Agile & Scrum to achieve what developers call “continuous delivery”. But now that you’ve taken the first step, the other parts will also come easier for you.