API testing: useful tools, Postman tutorial and hints

“API testing: useful tools, Postman tutorial and hints”

API testing helps examine application business logics. It’s a part of integration testing, and no quality assurance process is complete without it.

However, manual API testing is quite a tiresome process, so QA Engineers often choose to automate it whenever appropriate. Now, there are many testing automation tools and techniques available, but we’d like to walk you through some of our most favorite ones to help you save time and achieve better results.

What is an API?

Application Programming Interface (or API) is a set of rules, methods, tools and protocols used when building mobile applications. In other words, it’s a way for different software components to interact with each other. API reduces development time and provides developers with access to a number of practical features and services.

The majority of mobile apps have three layers to them:

  1. Database, where data is extracted from and stored in databases (DB).
  2. Business Logic, which consists of APIs; its main purposes are data processing between different layers, logical decision making and application management.
  3. Presentation, which represents user interface (UI).

Why is it necessary to test APIs?

At SteelKiwi, we test APIs to improve application test coverage on the Business Logic layer. Testing APIs is important, because whenever they don’t work properly, major issues may occur in application logic, performance, and security.

API Testing Tools

There are three widely popular API test-tools: Postman, curl, and SoapUI.

Postman is a powerful tool used to test web services. It was developed for sending HTTP requests in a simple and quick way.

Curl is a command-line tool used to deliver requests via HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, LDAP, DAP, DICT, TELNET, FILE, IMAP, POP3, SMTP and RTSP protocols.

SoapUI is a free tool used to test SOAP and RESTful Web Services.

Now, we could, of course, cover all of them in one sitting, but it would just be too much to read. Therefore, we decided to only tell you about Postman this time, as it’s really powerful, and we personally love it here are SteelKiwi.

Using Postman to Test API

Postman offers a simple user-friendly interface. To send a simple request, one only needs to enter it, fill in the necessary headers, select the HTTP method, and the ‘send’ button.

Postman Scripts are a Great Feature

One of the great features offered by Postman is the ability to create individual JavaScript test scripts that validate separate API responses. For those new to writing test scripts, Postman provides code snippets with examples of validations for response time, response code, etc. Postman also supports pre-request scripts which are run before an actual request has been sent.

Example
First, let’s create a Postman query to an API and receive a JSON response containing a book description:

{
	"id": 0,
	"book_name": "Photography",
	"page_quantity": 346,
	"is_exist": true
}

Our next step would be to write tests to validate that response:

tests["Status code is 200"] = responseCode.code === 200;
// Checking whether the response has a status 200. If so, the request is successful.

tests["content-type header is present"] = postman.getResponseHeader("content-type");
// Checking whether the Content-type header is present.

var jsonData = JSON.parse(responseBody);
tests["Checking id parameter from JSON data"] = jsonData.id === 1;
// Checking whether the JSON parameter "id" exists and whether its value is 1.

var jsonData = JSON.parse(responseBody);
tests["Checking book_name parameter from JSON data"] = jsonData.book_name === "Photography";
// Checking whether the JSON parameter "book_name" exists and whether its value is "Photography".

var jsonData = JSON.parse(responseBody);
tests["Checking page_quantity parameter from JSON data"] = jsonData.page_quantity === 346;
// Checking whether the JSON parameter "page_quantity" exists and whether its value is "346".

	var jsonData = JSON.parse(responseBody);
tests["Checking is_exist parameter from JSON data"] = jsonData.is_exist === true;
// Checking whether the JSON parameter "is_exist" exists and whether its value is "True".

var jsonData = JSON.parse(responseBody);
tests["Example of failed test"] = jsonData.cover_color === “Red”;
// Example for a failed test. The cover_color parameter doesn’t exist in the JSON response.

After having run the request, Postman shows its outcome in the Test Results response tab:

Postman Collections

Collections are another valuable feature available in Postman. They allow organizing all requests into separate folders (e.g. by resources, suites, workflows, etc.), which is important as more and more tests are being run. SteelKiwi QA Engineers each create their own test collections and then share them with other team members. It’s also possible to save collections in the JSON format and import them afterwards. An easy way to create a collection is to click the create a Collection Button’ located on the left panel and fill in the collection name and description.

To illustrate, let’s create an Example collection and add a request to it.

Testing Automation with the Postman Monitor

Our third favorite feature is the Postman Monitor. It allows running collections automatically with established intervals from different regions all over the world. SteelKiwi QA Engineers create test scripts, add them to collections, and create Postman monitors. This helps them monitor whether API endpoints operate properly and return relevant data, thus saving time and effort.

After a monitor has been set up, APIs can be monitored from the Postman website.

Automatization with Newman

Another Postman tool for test automation is called Newman, which is a command line collection runner. Newman should be used when there’s a need to integrate tests with Jenkins, Bamboo, TeamCity, Travis CI, or any other code deployment pipeline tool.

First, Newman has to be downloaded and installed on the operating system. To run collections, they should be exported in the JSON format. Then, they can either be run from the command line or by using the command below to create a new job in CI.

$ newman run <collection-file-source>

Alternatively, they can be launched with an API documentation URL:

$ newman run https://www.getpostman.com/collections/my_api_documentation

After the test run has been finished, QA Engineers receive a test report. Newman supports console outputs along with the JSON and HTML reports.

Let’s import the collection we created earlier into JSON and run it through Newman via the command line:

$ newman run example.postman_collection.json

We can also create an HTML report using the following command, where /home/test/report.html is the path to that report:

$ newman run example.postman_collection.json --reporters cli,html --reporter-html-export /home/test/report.htm

Conclusion

As you can see, Postman is a powerful tool for API testing. However, in order to achieve the best results and save time and effort, it must be used properly (e.g. appropriate implementation of techniques, knowing when an API can be tested automatically, and when it should be tested manually, etc.).

If you make enough effort and master your Postman skills, you’ll be able to significantly increase the quality of any target product.

Useful Links

  1. API Testing: Why It Matters, and How To Do It
  2. What makes API Testing special?
  3. API Building and Testing Made Easier with Postman
  4. How to write automated tests for APIs using Postman
  5. API testing tips from a Postman professional