As more companies adopt Microservices and Agile, they need API Testing to ensure the integration and connection of the digital product with other systems. In this article, you will get to explore the What, Why, and How of API Testing.
Table of Contents
What is API Testing?
To understand what API Testing is and how to do it, let's first talk about API.
API stands for Application Programming Interface. It is an intermediary between applications, allowing them to communicate with each other. API also determines how an application request services back and forth with the operating system or other apps.
As a type of software testing, API Testing ensures that a given API works well in terms of functionality, security, performance, and reliability. Typically, people run API directly on the API or during integration testing.
Where does API Testing happen?
There are three layers to an application:
- Data layer
- API layer (service layer)
- User interface (UI, or presentation layer)
The API layer contains the application's business logic (algorithms that handle the data exchange between the database and the user interface). It is also the area of focus for API Testing. In other words, API Testing deals with the business logic and the security of the application and its data exchange.
Usually, people do API Testing by sending requests to API endpoints. From that, they compare the response with expected results.
How API Testing is done?
To perform API Testing, first of all, the scope must be defined, in that the APIs of the application under test (AUT) are thoroughly learned. Several critical factors needed consideration:
- All API endpoints available for API Testing
- Response codes expected in case of successful requests
- Response codes expected in case of failed requests
- Error message expected in the failed request’s body
After testers have considered these factors, they can begin API Testing by applying various techniques.
Of course, test cases must be designed specifically for APIs. The objective of these test cases is to specify conditions and variables where testers can evaluate the performance and response of the application. Once the test cases have been executed, the actual results will be compared to the expected ones. Responses that API tests attempt to analyze include:
- Reply time,
- Data quality
- Confirmation of authorization
- HTTP status code
- error codes.
Various API endpoints, including web services, databases, or user interfaces can be analyzed by API Testing. However, failures or unexpected inputs during API Testing are not uncommon. Testers should prepare for them. For example, the team should set an acceptable limit for response time. They should also have plans and actions to prevent the API from potential attacks.
API tests need to be built in ways that prevent users from affecting the application, unintentionally or not. Specifically, API Testing must ensure that APIs could deal with large user loads. Moreover, it should be able to operate on multiple browsers and devices.
Lastly, API Testing needs to evaluate the results of non-functional tests, such as performance tests and security tests.
Why it’s important to perform API Testing
API testing can benefit both testers and developers across the development cycle and in modern development methods such as Agile and microservices. In turn, these benefits will result in improved user experience and product quality.
UI Testing is not enough
When it comes to verifying API, User Interface (UI) Testing alone is ineffective. Moreover, UI tests often fail to cover back-end’s components. Without API testing, there would be bugs left in the server or unit levels, which require code to be rewritten. The consequence is that product release is delayed and cost overrun is incurred.
Help to find and resolve bugs earlier
Thanks to API, developers can test early in the development cycle, even before UI is ready. The UI will not display requests that don’t produce appropriate value at the server layer, which offers two benefits, First, developers are able to resolve at least half of existing bugs before they become more serious. Second, testers are able to make requests that are hard to make via UI, which helps to explore security flaws.
API Testing is a must-have to companies applying microservices because it allows products to be developed and deployed more efficiently. Any areas of the app that are being updated won’t interrupt others. There is a distinct data store for each application section, and there are also different commands to interact with the data store. As more adopt microservices and API, the use of API Testing will be more widespread.
Enable Agile Software Development
Agile also requires API Testing, where instant feedback is a must. Since unit tests and API tests are easier to maintain, Businesses adopting Agile prefer them more than GUI tests, which require vigorous reworking to keep with the changing requirements of Agile.
What are the types of API Testing?
Various testing types can be used to ensure APIs work as they should be. From the most general to the very specific tests, these types of API testing are:
Validation testing for API concerns three aspects: the product, the API’s behaviors, and the API’s efficiency.
In terms of product, validation testing checks if:
- The correct product was built
- The designed API is the correct product for the issue it tries to resolve
- There was any code bloat, which is the production of unnecessarily long, slow, and wasteful code, during development that could put the API in an unsustainable state
In terms of API’s behaviors, validation testing checks if:
- Data is accessed as per the predefined requirements
- Too much data is being accessed
- The API is storing data correctly given the integrity and confidentiality requirements of data
In terms of API’s efficiency, validation checks if:
- The API being used is the most efficient and accurate method
- Any codebase can be altered or removed to improve overall service
Functional testing helps to verify that the API operates as expected. By analyzing functions in the codebase, it ensures API works within its parameters. In case the results are outside the parameters, functional tests make sure that API can deal with the errors.
Testers use load testing to check the number of calls the API can handle. Usually, they perform load testing after completing each unit or the entire codebase. This is to validate if the hypothetical solution can work well in real life.
Reliability testing ensures the consistency of API’s results and the integration between platforms
Together with penetration testing and fuzz testing, security testing is often a part of the security auditing process. Not only does security testing have characteristics of penetration testing and fuzz testing, it also can help to verify the API’s encryption method and the access control design.
Penetration testing tries to attack the API with a person who had limited knowledge of the API. This helps to analyze the attacks from the perspective of an outsider. The team can limit the attacks to either certain components of API or the whole API.
With fuzz testing, the testing team intentionally loads huge amounts of data (called noise or fuzz) to the APIs to see if they behave negatively, such as forced crash or overflow.
Fuzz testing forcibly inputs huge amounts of random data -- also called noise or fuzz -- into the system, attempting to create negative behavior, such as a forced crash or overflow.
Benefits of API Testing
While it can vary based on your project timeline, functionalities, and integration requirements, API Testing can benefit your product development efforts in various ways, from product release and customer experience to security.
Access without UI
Thanks to API testing, the application can be accessed in the absence of a user interface or real users. This means API tests can be run without the need to experience the product/application. In other words, testers can early explore defects and errors so that developers can resolve them before they have negative impacts on the GUI.
Safeguard against malicious code and breakage
Because API Testing requires extreme conditions and inputs when analyzing applications, it can help keep the application away from malicious code and breakage. In essence, API testing challenges the application’s ability to connect and integrate so testers can find and resolve its vulnerabilities.
Faster in time and lower in cost
Once API Testing is automated, there is less code to write than automated GUI testing. API test automation results in a faster testing lifecycle, lower costs, and better ROI.
Independence of technology
Another benefit of API Testing is that technology and language-dependent. Within API tests, data is exchanged using XML or JSON. Moreover, the data also contains HTTP requests and responses.
Easy integration with GUI
API tests are highly integrable with GUI Tests. This is particularly advantageous when GUI tests are performed following API testing. Furthermore, integration can help create new users for the app even before any GUI test is executed.
Challenges of API Testing
Aside from those benefits, testers can face certain challenges when implementing API Testing. Most often, they are parameter selection, parameter combination, and call sequencing.
Parameter selection, in which parameters that are sent through API requests must be verified. This verification can be very hard. Testers are required to ensure all parameters satisfy verification criteria: the use of an appropriate string of numerical data, the assigned value range, and conformance with length restrictions.
Parameter combination, where it’s a must to test every combination to check if there is any problem related to configurations
Call sequencing, because for the application to work as expected, every API call must be in a certain order. Call sequencing can be very difficult if the team is dealing with multithreaded applications.
Tools for API Testing
There are typically two approaches to designing API tests: writing your own framework or choose a ready-made one from an API testing tool. The DIY approach allows greater customization as the API tests would not be limited to the capabilities of the tool and its plugins. You can choose from a wide variety of libraries for coding, build reporting systems as you see fit, and create whichever business logic you want. Nevertheless, this approach requires testers to have sophisticated coding skills to build their own framework
On the contrary, testing tools have everything ready for use: user-friendly interfaces, self-service features which means minimal coding requirements, and more.
There are a wide variety of testing tools available, ranging from open sources to paid licenses. For API Testing specifically, some ideal candidates are
SoapUI: testing API in SOAP, REST APIs, and web services.
Apache JMeter: An open-source tool for load and functional Testing in API.
Apigee: A cloud-based API testing tool built by Google that supports API performance testing.
REST Assured: An open-source, Java tool that facilitates the testing of REST APIs.
Swagger UI: An open-source tool that creates a webpage documenting APIs used.
Postman: A Google chrome app for verifying and automating API testing.
Katalon: An open-source application that aids in UI automated testing.
Best Practices for API Testing
API testing best practices include:
- Group test cases by category when building them.
- Insert the parameters directly in the test case.
- Ensure test coverage by creating test cases for each API input combination.
- Reuse and repeat test cases to monitor the API during production.
- Apply both manual and automated tests for more reliable results.
- Perform load testing on the API to see its limitations.
- Test for failure, where tests are repeated until there is failed output.
- Have plans for call sequencing.
- Focus on API function call to ease the process.
- Design documentation that is easy to understand and automate the creation of documentation.
- Try to keep each test case self-contained and separate from dependencies
If API functionality is not validated thoroughly with API Testing, problems would not only inevitably happens to the API, but also the application that is called. That’s why API Testing has gradually become an integral part of today’s software development process, where Agile and microservices are norms.
If you want to learn more about Software Testing, check out our latest paper on Software Testing Lifecycle.