The Comprehensive Guide to API Testing with Postman & Newman Report Generation

API Testing is a crucial aspect of software development, ensuring that the interactions between different software components work as expected. In this guide, we will delve into the world of API testing, covering everything from the basics to advanced techniques.

What is API Testing?

API Testing involves testing the application programming interfaces (APIs) that enable communication between different software applications. By testing APIs, developers and testers can validate the functionality, performance, security, and reliability of the APIs.

Why is API Testing Important?

API Testing is essential because it helps identify bugs and issues in the early stages of development, saving time and resources in the long run. It also ensures that APIs function correctly and provide the expected results to users and other software components.

Prerequisites

Before proceeding with this, you should have a basic knowledge of API, automation, and manual testing.

Related read: Navigating API Testing: An Essential Guide for QA in Software Development

What is Postman?

Postman is a scalable API testing tool that quickly integrates into the CI/CD pipeline. It started in 2012 as a side project by Abhinav Asthana to simplify API workflow in testing and development. API stands for Application Programming Interface which allows software applications to communicate with each other via API calls.

Why Use Postman?

With over 4 million users nowadays, Postman Software has become a tool of choice for the following reasons:

  • Accessibility: To use the Postman tool, one would just need to log in to their accounts making it easy to access files anytime, anywhere as long as a Postman application is installed on the computer.
  • Use of Collections: Postman lets users create collections for their Postman API calls. Each collection can create subfolders and multiple requests. This helps in organizing your test suites.
  • Collaboration: Collections and environments can be imported or exported making it easy to share files. A direct link can also be used to share collections.
  • Creating Environments: Having multiple environments aids in less repetition of tests as one can use the same collection but for a different environment. This is where parameterization will take place which we will discuss in further lessons.
  • Creation of Tests: Test checkpoints such as verifying for successful HTTP response status can be added to each Postman API calls which help ensure test coverage.
  • Automation Testing: Through the use of the Collection Runner or Newman, tests can be run in multiple iterations saving time for repetitive tests.
  • Debugging: The Postman console helps to check what data has been retrieved making it easy to debug tests.
  • Continuous Integration: With its ability to support continuous integration, development practices are maintained.

How to Use Postman to Execute APIs

Below is the Postman Workspace. Let’s explore the step-by-step process of How to use Postman and the different features of the Postman tool!

Postman-Overview
Fig: Postman Tool Overview
  1. New: This is where you will create a new request, collection, or environment.
  2. Import: This is used to import a collection or environment. There are options such as import from file, folder, link, or paste raw text.
  3. Runner: Automation tests can be executed through the Collection Runner.
  4. Open New: Open a new tab, Postman Window, or Runner Window by clicking this button.
  5. My Workspace: You can create a new workspace individually or as a team.
  6. Invite: Collaborate on a workspace by inviting team members.
  7. History: Past requests that you have sent will be displayed in History. This makes it easy to track actions that you have done.
  8. Collections: Organize your test suite by creating collections. Each collection may have subfolders and multiple requests. A request or folder can also be duplicated as well.
  9. Request Tab: This displays the title of the request you are working on. By default, “Untitled Request” would be displayed for requests without titles.
  10. HTTP Request: Clicking this would display a dropdown list of different requests such as GET, POST, COPY, DELETE, etc. In Postman API testing, the most commonly used requests are GET and POST.
  11. Request URL: Also known as an endpoint, this is where you will identify the link to where the API will communicate.
  12. Save: If there are changes to a request, clicking save is a must so that new changes will not be lost or overwritten.
  13. Params: This is where you will write parameters needed for a request such as key values.
  14. Authorization: To access APIs, proper authorization is needed. It may be in the form of a username and password, bearer token, etc.
  15. Headers: You can set headers such as content type JSON depending on the needs of the organization.
  16. Body: This is where one can customize details in a request commonly used in POST requests.
  17. Pre-request Script: These are scripts that will be executed before the request. Usually, pre-request scripts for the setting environment are used to ensure that tests will be run in the correct environment.
  18. Tests: These are scripts executed during the request. It is important to have tests as they set up checkpoints to verify if response status is ok, retrieved data is as expected, and other tests.

Working with GET Requests

Get requests are used to retrieve information from the given URL. There will be no changes done to the endpoint.

In the workspace:

  1. Set your HTTP request to GET.
  2. In the request URL field, input the link.
  3. Click Send.
  4. You will see 200 OK Messages.
  5. There should be 10 user results in the body which indicates that your test has run successfully.
Http-Request-to-GET
Fig: HTTP Postman Guest Request

Note: There may be cases in a Get Postman request that may be unsuccessful. It can be due to an invalid request URL or authentication is needed.

Working with POST Requests

Post requests are different from Get requests as there is data manipulation with the user adding data to the endpoint. Using the same data from the previous tutorial in Get request, let’s now add our user.

Step 1) Click a new tab to create a new request.

Create-New-Request
Fig: Create New Request

Step 2) In the new tab:

  1. Set your HTTP request to POST.
  2. Input the same link in the requested URL.
  3. Switch to the Body tab.

HTTP-Request-Post

Step 3) In Body:

  1. Click raw.
  2. Select JSON.

unnamed-21

Step 4) Copy and paste just one user result from the previous get request like below. Ensure the code has been copied correctly with paired curly braces and brackets. Change ID to 11 and name to any desired name. You can also change other details like the address.

[
{
"id": 11,
"name": "Krishna Rungta",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}
]

unnamed-22

Note: Online Post requests should have the correct format to ensure that requested data will be created. It is a good practice to use Get first to check the JSON format of the request.

unnamed-23

Step 5) Next:

  1. Click Send.
  2. Status: 201 Created should be displayed.
  3. Posted data are showing up in the body.

unnamed-24

How to Parameterize Requests

Data Parameterization is one of the most useful features of Postman. Instead of creating the same requests with different data, you can use variables with parameters. These data can be from a data file or an environment variable. Parameterization helps to avoid repetition of the same tests and iterations can be used for automation testing.

Parameters are created through the use of double curly brackets: {{sample}}. Let’s take a look at an example of using parameters in our previous request:

unnamed-25

Now let’s create a parameterized get request.

Step 1)

  1. Set your HTTP request to GET
  2. Input this link. Replace the first part of the link with a parameter such as {{url}}. Request url should now be {{url}}/users.
  3. Click send.

There should be no response since we have not set the source of our parameter.

unnamed-26

Step 2) To use the parameter you need to set the environment.

  1. Click the eye icon.
  2. Click edit to set the variable to a global environment which can be used in all collections.

unnamed-27

Step 3) In variable:

  1. Set the name to the URL which is https://jsonplaceholder.typicode.com
    click Save.
  2. Click Save.

unnamed-28

Step 4) Click close if you see the next screen.

unnamed-29

Step 5) Go back to your Get request then click Send. There should now be results for your request.

unnamed-30

Note: Always ensure that your parameters have a source such as an environment variable or data file to avoid errors.

Optimize Your Testing Process: Postman & Newman for Effortless API Testing. Hire Our Developers!

How to Create Postman Tests

Postman Tests are JavaScript codes added to requests that help you verify successful or failed status, comparison of expected results, etc. It usually starts at pm. test. It can be compared to asserts, and verify commands available in other tools.

Let’s do some basic API testing using Postman for our parameterized requests from the previous lesson.

Step 1) Go to your GET user request from the previous tutorial.

  1. Switch to the tests tab. On the right side are snippet codes.
  2. From the snippets section, click on “Status code: Code is 200”.

The pane is auto-populated.

unnamed-31

Step 2) Now click Send. The test result should now be displayed.

unnamed-32

Step 3) Go back to the test tab and let’s add another test. This time we will compare the expected result to the actual result.

From the snippets section, click on “Response body:JSON value check”. We will be checking if Leanne Graham has the user ID 1.

unnamed-33

Step 4)

  1. Replace “Your Test Name” from the code with “Check if a user with id1 is Leanne Graham” so that the test name specifies exactly what we want to test.
  2. Replace jsonData.value with jsonData[0].name. To get the path, check the body in Get result earlier. Since Leanne Graham is user-id 1, JSON data is in the first result which should start with 0. If you want to get the second result, use jsonData[1] and so on for succeeding results.
  3. In eql, input “Leanne Graham”.
pm.test("Check if user with id1 is Leanne Graham", function () {
var jsonData = pm.response.json();
pm.expect(jsonData[0].name).to.eql("Leanne Graham");
});

unnamed-34

Step 5) Click send. There should now be two passed test results for your request.

unnamed-35

Note: Different kinds of tests can be created in Postman. Try to explore the tool and see what tests will fit your needs.

How to Create Collections

Collections play an important role in organizing test suites. It can be imported and exported making it easy to share collections amongst the team. In this tutorial, we will learn how to create and execute a collection.

Let’s start by creating a collection:

Step 1) Click on the New button at the top left corner of the page.

unnamed-36

Step 2) Select Collection. Create collection window should pop up.

unnamed-37

Step 3) Input the desired collection name and description then click Create. A collection should now be created.

unnamed-38

Step 4) Go back to the previous Get request. Click Save:

unnamed-39

Step 5)

  1. Select Postman Test Collection.
  2. Click Save to Postman Test Collection.

unnamed-40

Step 6) Postman test collection should now contain one request.

unnamed-41

Step 7) Repeat steps 4-5 for the previous Post request so that the collection will now have two requests.

How to Run Collections using Newman

Another way to run a collection is via Newman. The main differences between Newman and Collection Runner are the following:

  1. Newman is an add-on for Postman. You will need to install it separately from the Native App.
  2. Newman uses the command line while Collection Runner has a GUI.
  3. Newman can be used for continuous integration.

To install Newman and run our collection from it, do the following:

Step 1) Install nodejs using this link.

Step 2) Open the command line and enter.

 npm install -g newman

Newman should now be installed on your computer.

unnamed-43

Step 3) Once Newman has been installed, let’s go back to our Postman workspace. In the Collections box, click on the three dots. Options should now appear. Select Export.

unnamed-44

Step 4) Choose Export Collection as Collection v2.1 (Recommended) then click Export.

unnamed-45

Step 5) Select your desired location then click Save. It is advisable to create a specific folder for your Postman tests. A collection should now be exported to your chosen local directory.

Step 6) We will also need to export our environment. Click on the eye icon beside the environment dropdown in Global, and select Download as JSON. Select your desired location then click Save. The environment should be in the same folder as your collection.

unnamed-46

Step 7) Environment should now be exported to the same local directory as Collection.

Step 8) Now go back to the command line and change the directory to where you have saved the collection and environment.

Step 9) Run your collection using this command:

 newman run PostmanTestCollection.postman_collection.json -e Testing.postman_globals.json

Run results should now appear such as below.

unnamed-47

For guide is a reference to some basic Newman codes for execution:

1. Run a collection only. This can be used if there is no environment or test data file dependency.

newman run <collection name>

2. Run a collection and environment. The -e indicator is for the environment.

newman run <collection name> -e <environment name>

3. Run a collection with the desired no. of iterations.

newman run <collection name> -n <no.of iterations>

4. Run with data file.

newman run <collection name> --data <file name> -n <no.of iterations> -e <environment name>.

5. Set delay time. This is important as tests may fail if it is run without delay due to requests being started without the previous request completing processing on the endpoint server.

newman run <collection name> -d <delay time>
coma

Conclusion

  • API Testing using Postman: Postman is an application for testing APIs. Postman is one of the most popular tools used in API testing by sending requests to the web server and getting the response back.
  • Accessibility, Use of Collections, Collaboration, and Continuous Integration, are some of the Key features to learn in Postman
  • It’s recommended you create an account in Postman, so your collections are available online.
  • You can parameterize requests in Postman.
  • You can create Tests to verify a Postman request.
  • You can generate an API testing report using Newman.

Keep Reading

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?