top of page
  • tzurpaldi

How to Add API Testing to an Automation Testing Project: The Complete Guide

Updated: Jun 3



testing-api

What is an API?

API stands for Application Programming Interface. It’s the way different applications or services can communicate and share information or functions using code, without relying on a graphical user interface. For example, you can use the weather forecast service’s API to provide relevant information to a website serving surfers. API is an essential and useful tool in the world of programming and technology. It allows you to save time and resources, integrate with other services, and create innovative applications. Anyone can build an API, but there are standards and methodologies like SOAP and REST that define how requests and responses are sent and received. API testing ensures that the API we’ve built works as expected.


Why are API Tests Important?

When using an API, a high level of quality and consistency is required. A broken API will cause users to abandon an application that doesn’t respond as expected, and it will force developers to make adjustments and changes to their processes or stop using an API with low reliability (if they have an alternative…). To ensure high consistency and quality, API testing is necessary.


API tests are a type of software testing that operates on the API interface, ensuring that it functions as expected and reliably complements its functionality without compromising performance. Such tests can be performed both manually and automatically. API tests fall under the category of “integration testing” for testing the server-side of the system being tested. Their advantage over end-to-end (e2e) tests lies in quickly, reliably, and relatively inexpensively checking the core of the system.


How to Automate API Testing?

In API automation testing, software tools are used to automatically run tests on APIs, saving time and resources, increasing efficiency and accuracy, and enabling repetitive testing at every stage of the product development.


To automate API testing, you need to choose tools suitable for the API type and testing requirements. There are many tools available in the market, each with its advantages and disadvantages. Consider the following parameters when comparing them:

  • Support for different API types: REST, SOAP, GraphQL, etc.

  • Ability to create, manage, and maintain requests and responses.

  • Capability to test parameters, status codes, response bodies, etc.

  • Usage of variables, logic, conditions, and loops.

  • Scripting languages supported like: JavaScript, Python, Java, etc.

  • Integration with other systems and tools: CI/CD, version control systems, test management systems, etc.


Specific tools for API testing include Postman and SOAPUI, which allow making API requests without writing code. Additionally, there are tools that record all calls in a scenario and create tests for API testing (e.g., Loadmill).

Since there isn’t a universally recommended way to test APIs in code-based systems, it’s essential to focus on how we structure the infrastructure and tests for APIs in such systems.


Creating an API Testing Infrastructure

Before writing the first test, ensure that the code structure aligns with our API testing needs. We’ll need to build the client code (referred to as a proxy) centrally so that any test requiring calls can access it. For this purpose, create a class (e.g., PetsProxy) that defines a group of actions in the API, sharing a common path.

// pets-proxy.js

export class PetsProxy {
baseUrl = 'my-base-url/path/pet';

getPetById(id) {
        const url = `this.baseUrl/${id}`;
        const response = client.get(url);
        return response;
    }

createPet(name, id, category) {
        const url = this.baseUrl;
        const response = client.post(
            {name: name, 
            category: {name: category, id: id}
        });
        return response;
    }
}

In this example, the PetsProxy class contains a shared path named baseUrl that is suitable for both methods within the class: getPetById, which retrieves a pet based on an identification number, and createPet, which adds a pet to the database. We use a generic HTTP client (such as Axios) represented by the term “client” to wrap methods like get, post, put, etc. These methods accept zero or more parameters and return the response to the request.


All that remains now is to create a test that will verify the API using PetsProxy, for instance.


// tests/pet-suite.js

it("example-proxy-test", ()=> {
        const proxy = new PetsProxy();
        
        const response = proxy.getPetById(1);
        
        expect(response.statusCode).toEqual(200);
    })

As you can see, using PetsProxy makes writing tests easy, convenient, and readable. But… let’s move on to the next section.


Automating the Test Infrastructure

No, it’s not a mistake in the title. We can automatically generate our proxies using a tool called OpenAPI Generator. This tool leverages the API schemas to create client-side code. To work with this tool, ensure that your API has OpenAPI documentation of version 3.0 or higher.


The advantage of automatically generating proxies with OpenAPI Generator is that we don’t need to write all the code manually (which is evident 😊). But more importantly, if anything changes in the API schemas—such as paths or parameters we need to send—we can automatically regenerate all the proxies, making their maintenance incredibly straightforward!


Since there are many articles on how to work with OpenAPI Generator, there’s no need to repeat it here. You can start with this article and see a live demo right here 👇.





Summary

An API allows the core system to operate through requests and responses. In many cases, organizations expose their API to their clients or use it for performing actions from within their applications. Therefore, API testing is crucial for organizations, and fortunately, it’s considered one of the highest-return investments in testing. It’s advisable to include API tests in every automated testing system. Furthermore, as described in the article, using proxies can elevate your e2e tests to another level. With proxies, you can efficiently and rapidly set up conditions for each test and verify data at the end of each test.

24 views0 comments

コメント


bottom of page