With the popularization of microservices architecture APIs became ubiquitous. As they are important part of many systems it is necessary to tests also them. Using software like SoapUI or Postman one can do both manual and automated tests.
API stands for Application Programming Interface. It is a software to software interface, not a user interface. With APIs, applications converse with each other with no client learning or intervention. It can be executed by composing capacity brings in the system.
There are many terms related to API testing. This great glossary might be helpful at the beginning.
How to test API?
In API testing we are using dedicated tools to send calls to the API and get the response. It testing regards the segment under test as a black box.
The objective of API testing is to confirm right execution and blunder treatment of the part preceding its coordination into an application.
Basic knowledge of HTTP messages is required. You can read about basics of HTTP in here. What is the most important is that if we want to sent simple HTTP request we need to provide:
- HTTP method (e.g. GET, POST etc.)
- URL (e.g. http://www.google.pl)
- Query parameters (optional) (e.g. index?login=janedoe)
Which method should we use to test given endpoint? We need to check documentation of particular API to be sure.
What will be tested?
For this article purposes I decided to test one of many public APIs available on the Internet. Namely, I used Jewish calendar REST API. It has simple documentation and, what is important — it’s free to use. The API supports responses in JSON and XML formats.
We will check if correct values of parameters are producing correct responses and also we will check if incorrect input is producing proper error messages.
There is only one endpoint in mentioned API therefore we will be mostly concerned with values of query parameters. We also will do performance check later in this article.
Manual testing using Postman
As described above we need to provide HTTP method, URL and query parameters to test chosen API. We need to use below input fields:
As an example we will use following test cases:
- check if today’s date(22.02.2019) is converted to jewish calendar in JSON format
- check if today’s date(22.02.2019) is converted to jewish calendar in XML format
- check if date from BCE produces an error with proper HTTP status
In first case we will use GET method and below URL (with query parameters):
The output is correct JSON file.
In second case we will use GET method and below URL (with query parameters):
The output is correct XML file.
Finally, in third case we will use GET method and below URL (with query parameters):
The output is HTTP status 400 Bad Request and an error Gregorian year out of valid range 0001–9999, which is expected behavior.
Automated testing using Postman
You can find complete documentation of those scripts here.
As an example we will automate check of performance of responses. We assume that API must respond within 300ms, otherwise test will fail.
Other tests can be found on my GitHub.
This article shows only the absolute minimum of API testing, but I hope it clearly shows the idea behind it. There are many other API testing tools to try e.g. SoapUI. Below are the links for further reading.
- API testing glossary: https://blog.testproject.io/2018/10/23/api-testing-glossary/
- Postman website: https://www.getpostman.com/
- API used in article: https://www.hebcal.com/home/219/hebrew-date-converter-rest-api
- API Testing article: https://dzone.com/articles/api-testing-is-great-so-why-isnt-everyone-doing-it
- Script testing using Postman: https://learning.getpostman.com/docs/postman/scripts/test_scripts/