API testing: useful tools, Postman tutorial and hints

08 Dec 2017
API testing: useful tools, Postman tutorial and hints

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 Globaldev, 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 at Globaldev.

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.

api_testing_useful_tools_postman_tutorial_and_hints_image_1

Example

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

Postman query to an API

api_testing_useful_tools_postman_tutorial_and_hints_image_2

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

write tests to validate that response

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

api_testing_useful_tools_postman_tutorial_and_hints_image_3

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. Globaldev 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.

api_testing_useful_tools_postman_tutorial_and_hints_image_4

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. Globaldev 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.

api_testing_useful_tools_postman_tutorial_and_hints_image_5

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

api_testing_useful_tools_postman_tutorial_and_hints_image_6

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.html

api_testing_useful_tools_postman_tutorial_and_hints_image_7

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. What makes API Testing special?
  2. API Building and Testing Made Easier with Postman
  3. How to write automated tests for APIs using Postman
  4. API testing tips from a Postman professional
  5. API testing with JMeter