How to create Tests in Postman

HOME

In this tutorial, we will see how we can create Tests in the Postman.

Consider a sample JSON which has 4 sets of data. This data we will be passing to a POST request.

Create a request in Postman. Below is the URL of the request.

https://reqres.in/api/users

Paste the body of the request in the Body part of the Postman.

Create Tests for the request

Click on the “Tests”. Write the tests as shown below:

pm.test("Status code is 201", function () {
    pm.response.to.have.status(201);
});

pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
});

pm.test("The endpoint does not return unexpected status code", () => {
 
  // change 404 to the response code you do not expect
  pm.response.to.not.have.status(404);
});

pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});

pm.test("API response contians the expected header", () => {
  pm.response.to.have.header("Content-Type", "application/json; charset=utf-8");
});

pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("Postman");
});

The output of the above test is

pm.test("Value of name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.name).to.eql("Postman");
});

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  // the line below checks value of the name field is Postman.
  pm.expect(response).to.have.property("name", "Postman");

  // the line below checks value of the job field is Test (string).
  pm.expect(response).to.have.property("job", "Test");
});

The output of the above test is

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

The output of the above test is

const responseJson = pm.response.json();
pm.test("Test Data Type of the response", () => {
    
    pm.expect(responseJson.job).to.be.a("string");
    pm.expect(parseInt(responseJson.id)).to.be.a("number");
});

pm.test("The response has all properties", () => {
    //parse the response JSON and test three properties
    const responseJson = pm.response.json();
    pm.expect(responseJson.name).to.eql("Postman");
    pm.expect(responseJson.job).to.be.a('string')
    pm.expect(responseJson.id).to.have.lengthOf(3);
});

Sample Response

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  // the line below checks value of the id field is 2 (number).
  pm.expect(response).to.have.property("id", 2);

  // the line below checks value of the name field is Morty Smith (string).
  pm.expect(response).to.have.property("name", "Morty Smith");

  // the line below checks value of the origin.name field is Earth (C-137) (string).
  pm.expect(response).to.have.nested.property("location.name", "Citadel of Ricks");
});

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  pm.expect(response).to.have.nested.property("episode.3", "https://rickandmortyapi.com/api/episode/4");
});

Data Driven Testing in Postman

HOME

In this tutorial, we will see how we can use data files to feed test data to the Postman requests.

Postman provides two ways to pass sets of values to use in a collection run.

By selecting a JSON or CSV data file in the Collection Runner, you can test your requests with multiple different values as part of a single run.

1. Passing data using JSON File

Consider a sample JSON which has 4 sets of data. This data we will be passing to a POST request.

[
   {
      "name":"Test1",
      "job":"Developer"
   },
   {
      "name":"Test2",
      "job":"Scrum Master"
   },
   {
      "name":"Test3",
      "job":"Quality Analyst"
   },
   {
      "name":"Test4",
      "job":"Business Analyst"
   }
]

Create a request in Postman. Below is the URL of the request.

https://reqres.in/api/users

Paste the body of the request in the Body part of the Postman.

Here, name and job are the variables that we will be passing to the POST request. So, we have parameterized these 2 variables.

Create Tests for the request

Click on the “Tests”. Write the tests as shown below:

If you need help in creating the test, you can refer to the snippets present on the right side of the console.

Here, data.json is the name of the json we are using in this example.

To read the JSON data we need to upload a JSON file from the collection runner.

Select the more actions icon More actions icon next to the collection name. Click on the “Run Collection” option.

This shows all the requests present in this collection. Here, I want to run only CreateUser Request. So, I will uncheck the “Dummy User” option.

On the Functional tab, select Run manually.

Click on the Select File and upload the data file. The Iterations field shows the number of data sets present in the data file. Here, we have 4 sets of data, so used Iterations as 4. The Delay field defines the time in ms between two iterations.

Click on the preview to see the data that is uploaded.

Click on the “RunDataDrivenTesting” button to run the tests available in the Collection.

We can see that our API has run four times. Each time, a different set of data is picked up.

2. Passing data using CSV File

All the steps remain the same here, except instead of a JSON, we will use a .csv file.

Click on the “Preview” button to view the data present in the .csv file.

Below are the sample tests.

pm.test("Status code is 200", function () {
    pm.response.to.have.status(201);
});

var expectedName = pm.variables.get("name");
var expectedRole = pm.variables.get("role");


pm.test("Verify name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(expectedName);
});

pm.test("Verify role", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(expectedRole);
});


Run the collection again. Below is the image of the test execution in Postman.

What is Collection in Postman?

HOME

What is Collection?

In Postman, a collection is a group of API requests that are saved in Postman and can be organized into folders. Within a collection, any number of folders can be formed. All API requests can be saved and stored in a collection, which can be shared around the team in the Postman workspace.
Organizing comparable requests into folders and collections enables the customer to better organize and document their demands.

For example, if you are testing or validating a restful API that has 10 different endpoints. Then, it makes sense to organize them in a collection that would make things like applying collection variables, import/export easier and could be run as part of a single collection.

How to create a Collection?

Step 1: Create a Collection, click on Collections, and then click on the “+” plus button.

Step 2:  Provide a name to the collection – “Demo”.

How to create a request in Collection?

API Testing

HOME

Rest Assured

Postman

How to send DELETE Requests in Postman?

HOME

In this tutorial, we will create DELETE request in Postman and see how to execute it.

The HTTP DELETE method is used to delete a resource from the server. Unlike GET requests, the DELETE requests may change the server state.
The DELETE method is defined to be idempotent, which means that sending the same HTTP DELETE request multiple times will have the same effect on the server and will not additionally affect the state or cause additional side effects.

We will use the following URL for this Postman tutorial.

https://dummy.restapiexample.com/api/v1/delete/2

To create the first DELETE request in Postman, follow the following steps:

Step 1: Create a Collection, click on Collections, and then click on the “+” plus button.

Step 2:  Provide a name to the collection – “API Testing”.

Step 3: To create a new request, click on “Add a request” if it is a new Collection. Otherwise, click on the 3 dots and select “Add request”.

Step 4: Once you create a new request, then you will get the following window:

Step 5: Enter the “name” in the request. Here, the name is “DeleteUser”.

Step 5: Enter the “URL” in the address bar.

Step 6: Now, select the “DELETE” request from the list of request methods.

Step 7: Press the “Send” button.

Step 8: Once you press the send button, you will get the response from the server. Make sure you have a proper internet connection; otherwise, you will not get a response.

Status

You can check the status code. Here, we got the status code 200, which means we got a successful response to the request.

Body

In the Body tab of the response box, we have multiple options to see the response in a different format.

Format Type

Each request has a defined response to it as defined by the Content-Type header. That response can be in any format. Such as in the above example, we have JSON code file.

Below are the various format type present in Postman.

XML

HTML

Text

Headers

Headers are the extra information that is transferred to the server or the client. In Postman, headers will show like key-value pairs under the headers tab. Click on the Headers link as shown in the below image:

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

How to send PUT Requests in Postman?

HOME

In this tutorial, we will create a PUT request in Postman and see how to execute it.

The PUT method is used to update resources available on the server. Typically, it replaces whatever exists at the target URL with something else. You can use it to make a new resource or overwrite an existing one. 

Difference between POST and PUT

  1. The PUT method is used to alter a single resource, whereas the POST method is used to add a child resource.
  2. POST method responses cannot be cached, while PUT method responses may.
  3. In PUT, you can use UPDATE queries, whereas, in POST, you can use CREATE queries.
  4. The client determines which URI resource to have in the PUT method, and the server decides which URI resource to have in the POST method.
  5. PUT is more specific, but POST is more abstract.
  6. If you send the same PUT request many times, the result will be the same; however, if you send the same POST request multiple times, the results will be different.
  7. The PUT method is idempotent, whereas the POST method is not.

We will use the following URL for this Postman tutorial.

	https://dummy.restapiexample.com/api/v1/update/1

Sample Request Body

{
   "name":"Update_Test",
   "salary":"40600",
   "age":"25"
}

To create the first PUT request in Postman, follow the following steps:

Step 1: Create a Collection, click on Collections, and then click on the “+” plus button.

Step 2:  Provide a name to the collection – “API Testing”.

Step 3: To create a new request, click on “Add a request” if it is a new Collection. Otherwise, click on the 3 dots and select “Add request”.

Step 4: Once you create a new request, then you will get the following window:

Step 5: Enter the “name” in the request. Here, the name is “UpdateUser”.

Step 6: Enter the “URL” in the address bar.

Step 7: Now, select the “PUT” request from the list of request methods.

Step 8 – Add a Request body to the Post request

For this, select the Body tab.

Now in the Body tab, select raw and select JSON as the format type from the drop-down menu, as shown in the image below. This is done because we need to send the request in the appropriate format that the server expects. Copy and paste the request body example mentioned at the beginning of the tutorial to the postman request Body. 

Step 9: Press the “Send” button.

Step 10: Once you press the send button, you will get the response from the server. Make sure you have a proper internet connection; otherwise, you will not get a response.

Status

You can check the status code. Here, we got the status code 200, which means we got a successful response to the request. In the case of new resource creation, the status code should be 201. But as this is a dummy API, we are getting a status code of 200.

Body

In the Body tab of the response box, we have multiple options to see the response in a different format.

Format Type

Each request has a defined response to it as defined by the Content-Type header. That response can be in any format. Such as in the above example, we have JSON code file.

Below are the various format type present in Postman.

XML

HTML

Text

Headers

Headers are the extra information that is transferred to the server or the client. In Postman, headers will show like key-value pairs under the headers tab. Click on the Headers link as shown in the below image:

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Postman Tutorials

HOME

How to send POST Requests in Postman?

HOME

In this tutorial, we will create a POST request in Postman and see how to execute it.

What is a POST Request?

POST requests are used to send data to the API server to create or update a resource. The data sent to the server is stored in the request body of the HTTP request.
HTTP POST request provides additional data from the client to the server message body.

We will use the following URL for this Postman tutorial.

https://dummy.restapiexample.com/api/v1/create

Sample Request Body

{
   "name":"API_Test",
   "salary":"45600",
   "age":"23"
}

To create the first POST request in Postman, follow the following steps:

Step 1: Create a Collection, click on Collections, and then click on the “+” plus button.

Step 2:  Provide a name to the collection – “API Testing”.

Step 3: To create a new request, click on “Add a request” if it is a new Collection. Otherwise, click on the 3 dots and select “Add request”.

Step 4: Once you create a new request, then you will get the following window:

Step 5: Enter the “name” in the request. Here, the name is “CreateUser”.

Step 6: Enter the “URL” in the address bar.

Step 7: Now, select the “POST” request from the list of request methods.

Step 8 – Add a Request body to the Post request

For this, select the Body tab.

Now in the Body tab, select raw and select JSON as the format type from the drop-down menu, as shown in the image below. This is done because we need to send the request in the appropriate format that the server expects. Copy and paste the request body example mentioned at the beginning of the tutorial to the postman request Body. 

Step 9: Press the “Send” button.

Step 10: Once you press the send button, you will get the response from the server. Make sure you have a proper internet connection; otherwise, you will not get a response.

Status

You can check the status code. Here, we got the status code 200, which means we got a successful response to the request. In the case of new resource creation, the status code should be 201. But as this is a dummy API, we are getting a status code of 200.

Body

In the Body tab of the response box, we have multiple options to see the response in a different format.

Format Type

Each request has a defined response to it as defined by the Content-Type header. That response can be in any format. Such as in the above example, we have JSON code file.

Below are the various format type present in Postman.

XML

HTML

Text

Headers

Headers are the extra information that is transferred to the server or the client. In Postman, headers will show like key-value pairs under the headers tab. Click on the Headers link as shown in the below image:

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

How to send GET Requests in Postman?

Last Updated On

HOME

In this tutorial, we will create a GET request in Postman and see how to execute it.

Table of Contents

  1. What is GET Request?
  2. Implementation Steps
    1. Create a Collection
    2. Add a request to the Collection
    3. Enter the details of request
    4. Verify the Response

What is GET Request?

A GET request gets the information from the server. When you make the GET request on the server, then the server responds to the request.
GET requests will not affect any data on the server. This means, there is no creation, updation, addition, or deletion of data on the server when you are making a GET request.

We will use the following URL for this Postman tutorial.

http://dummy.restapiexample.com/api/v1/employees

Implementation Steps

To create the first GET request in Postman, follow the following steps:

Create a Collection

Step 1: Create a Collection, click on Collections, and then click on the “+” plus button.

Step 2:  Provide a name to the collection – “API Testing”.

Add a request to the Collection

Step 3: To create a new request, click on “Add a request”, if it is a new Collection. Otherwise, click on the 3 dots and select “Add request”.

Step 4: Once you create a new request, then you will get the following window:

Enter the details of request

Step 5: Enter the “name” in the request. Here, the name is “GET Demo”.

Step 5: Enter the “URL” in the address bar.

Step 6: Now, select the “GET” request from the list of request methods.

Send the Request

Step 7: Press the “Send” button.

Verify the Response

Step 8: Once you press the send button, you will get the response from the server. Make sure you have a proper internet connection; otherwise, you will not get a response.

Status

You can check the status code. Here, we got the status code 200, which means we got a successful response to the request.

Body

In the Body tab of the response box, we have multiple options to see the response in a different format.

Format Type

Each request has a defined response to it as defined by the Content-Type header. That response can be in any format. Such as in the above example, we have JSON code file.

Below are the various format type present in Postman.

XML

HTML

Text

Headers

Headers are the extra information that is transferred to the server or the client. In Postman, headers will show like key-value pairs under the headers tab. Click on the Headers link as shown in the below image:

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!