What are microservices:
Microservices architecture is the process of developing software with many loosely coupled and independently deployable services; any microservice can be communicated individually or through another microservice. The communication to a microservice is done through an application programming interface (API). Each microservice component must have one or more APIs. Each module in a monolith application could be a candidate for microservice.
What is API testing for microservice architecture?
The application programming interface testing evaluates the created API by giving the inputs such as body content, parameters, path values, and headers (required input) and check the expected output; this can be done by multiple cases such as unit testing, functional testing, load testing, and integration testing.
There are three layers in the above image: presentation layer, business layer, and data layer.
GUI testing works here; we can test the presentation layer testing process from the application UI.
Business layer here some business logic and functional-related testing happens with an API testing approach.
The database layer will work with whatever information is sent from UI. It will store and manage the data.
API testing tests the application or software. To call the API, we can use multiple UI to send the call and get the response based on our request, and we are able to save the response to the system. For the API testing, we can write some test suites also in the tool or from where we are calling; it is based on different UI and environments.
We can write the test cases based on input and output, check whether they matched the test case’s condition, and store the test results in the system or any report format. For these processes, we need to know each API’s functionality, and then only we can test the process.
To write the test cases and test scripts, the users need to know some basic coding skills. Users can also create some other tools without requiring coding knowledge and test cases by clicking some snippets available for testing tools.
Currently, to test the API, we have more user-friendly tools; some of the API testing tools are given below:
API testing tools
- Karate DSL
- Rest Assured
Benefits of API testing tools
- Easily finds defects in the functionality - By testing the API, we can easily find the defects instead of going in the UI. We can easily find if results like functionality are working properly.
- Effective use of time- By testing the API very quickly, we can be able to do the process; it takes very little time to process
- Test coverage improvement- Overall test coverage that we can improve
- Code coverage process
Automate API testing with postman tool (with and without GUI):
Here, multiple tools are available; we can go with the postman tool to automate the API test. Two types of testing examples are given below for postman:
- API test with postman UI
- API test without postman UI
API test with postman GUI :
We can upload our swagger/open API document in the postman import option, which contains all API details such as API name method type (GET, POST, PUT, DELETE). The sample image is given below for open API.
In variable tabs, we can declare the common variable key and values for the API, like server base URL and authentication token values are commonly we can declare.
Create test cases:
After writing some basic test scripts, we can call the API by clicking the send button. Then it will call the API and get the responses; after getting the responses, the test case will automatically execute the scripts, displaying the results like Pass/Fail. We see the sample results of the test script in the result place.
After executing the API call, we can see the results in the image below; Status code is 200 is Pass, validate schema script is Pass, so they are highlighted in green.
API test by Postman without GUI (Newman library):
Without postman UI, we can test the API by using Newman; By exporting the postman collection from postman UI and using the following process, we can achieve the API test.
Newman is the NPM library. By installing the library in the command prompt itself, we can execute the API call, and it will execute the test script.
We can export the postman collection from the postman and save it in some location. There, JSON may contain the API call and test scripts, etc.
- Install Newman by using the following:
npm install -g newman
- Install html libraries for getting html report
npm install newman-reporter-htmlextra
- API Testing newman cmd:
newman run "postmanCollection.json" -r htmlextra --reporter-htmlextra-export "outputhtml.html"