Table of contents
1.
Introduction
2.
Let’s write our first test script
3.
🍁 Using Single Assertions
3.1.
⭐ Implementation
3.1.1.
Output
4.
🍁Using multiple Assertions
4.1.
⭐Implementation 
4.1.1.
Output
5.
🍁 Making assertions on the HTTP response
5.1.
☘️Testing response body
5.1.1.
⭐Implementation
5.2.
☘️ Testing status codes
5.2.1.
⭐Implementation
5.3.
☘️ Testing headers
5.3.1.
⭐ Implementation
5.3.2.
⭐Implementation 
5.4.
☘️ Testing cookies
5.4.1.
⭐ Implementation
5.5.
☘️ Testing response times
5.5.1.
⭐ Implementation
6.
🍁Common assertion examples
6.1.
☘️ Array of strings
6.1.1.
⭐ Implementation
6.2.
☘️ Array is empty
6.2.1.
⭐ Implementation
6.3.
☘️ Assertion for Object Verification
6.3.1.
⭐ Implementation
7.
Frequently Asked Questions
7.1.
What are the variables in Postman?
7.2.
How do I give Postman JSON data?
7.3.
What is get request in Postman?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Examples of Test Scripts

Author Neha Chauhan
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Postman is an API development platform for designing, building and testing API (Application Programming Interface). It is one of the best platform for testing API. It allows its users to test APIs utilizing a graphical user interface. 

Test script eg

 

Scripts is a piece of code that can be executed at a specific point of time in the Postman testing environment. A script can be added to a collection, a folder, a request within a collection, and a request not saved to a collection. Postman has two types of Scripts- pre-request and test scripts.Test scripts are executed after a response has been received.

In this article, we will discuss some of the test script examples in different scenarios. 

Let’s write our first test script

Go to the ‘Tests’ tab and write your first test script. 

output

 

This test script is just a statement that we are printing on the console after the request has been executed. We can have many different kinds of test scripts that perform different functions. 

🍁 Using Single Assertions

The sole purpose of a test is to identify that for a situation, given parameters of the system are as expected. To force that the system's parameters are correct, we assert the expected values with the actual values during a test run. Assertions claim that expected and actual values during a test are the same. If they don't match, the test fails with the output pointing directly to the failure.

Enter the following Javascript code

⭐ Implementation

pm.test("Status code is 200", function () {
  pm.response.to.have.status(200);
});
You can also try this code with Online Javascript Compiler
Run Code

 

Output

output

 

The function inside the test represents an assertion. This test checks the response code returned by the API. If the response code is 200, the test will pass otherwise, it will fail.

🍁Using multiple Assertions

We can use multiple assertions as a single test. The test as a whole will fail if anyone of the assertions fails. Simply put, all assertions must be passed for the test to be successful.  

In this example, we use https://simple-books-api.glitch.me/books/1’ and send a request to retrieve a book of id equal to 1.

On running the above GET request the JSON response is 

{
    "id": 1,
    "name": "The Russian",
    "author": "James Patterson and James O. Born",
    "isbn": "1780899475",
    "type": "fiction",
    "price": 12.98,
    "current-stock": 12,
    "available": true
}

 

We run a test script to check whether the response book is fictional and if the book’s name is a string value. 

⭐Implementation 

pm.test("The response has all properties", () => {
    //parse the response JSON and test three properties
    const responseJson = pm.response.json();
    pm.expect(responseJson.type).to.eql('fiction');
    pm.expect(responseJson.name).to.be.a('string');
    
});
You can also try this code with Online Javascript Compiler
Run Code

 

Output

output

🍁 Making assertions on the HTTP response

We can test many aspects of HTTP response like body, status codes, headers, cookies and response times. 

☘️Testing response body

We can test for a particular values in the response body. 

⭐Implementation

pm.test("The book 'Just as I Am' is present ", () => {
  const responseJson = pm.response.json();
  pm.expect(responseJson.name).to.eql("Just as I Am");
  pm.expect(responseJson.id).to.eql(2);
});
You can also try this code with Online Javascript Compiler
Run Code

 

Output

output

☘️ Testing status codes

We can test for testing status code. 

⭐Implementation

pm.test("Status code is 200", () => {
  pm.response.to.have.status(200);
});
You can also try this code with Online Javascript Compiler
Run Code

 

Output

output

☘️ Testing headers

We can test if a response header is present. 

⭐ Implementation

pm.test("Content-Type header is present", () => {
  pm.response.to.have.header("Content-Type");
});
You can also try this code with Online Javascript Compiler
Run Code

 

Output

output

 

We can Test for a response header having a particular value

⭐Implementation 

pm.test("Content-Type header is application/json", () => {
  pm.expect(pm.response.headers.get('Content-Type')).to.eql('application/json; charset=utf-8');
});
You can also try this code with Online Javascript Compiler
Run Code


Output

output

☘️ Testing cookies

We can check whether the cookie is present in the response. 

⭐ Implementation

pm.test("Cookie JSESSIONID is NOT present", () => {
  pm.expect(pm.cookies.has('JSESSIONID')).to.be.false;
});
You can also try this code with Online Javascript Compiler
Run Code

 

Output

output

☘️ Testing response times

We can also test if the response time is within a specified range. 

⭐ Implementation

pm.test("Response time is less than 350ms", () => {
  pm.expect(pm.response.responseTime).to.be.below(350);
});
You can also try this code with Online Javascript Compiler
Run Code

 

Output

output
 

🍁Common assertion examples

☘️ Array of strings

We write a test to check whether a particular text is within an array of strings. 

⭐ Implementation

pm.test("Text is present", function(){
pm.expect(['Java', 'Postman']).to.include('Postman')
});
You can also try this code with Online Javascript Compiler
Run Code

 

Output

output

☘️ Array is empty

We can write a test to check whether the array is empty. 

⭐ Implementation

pm.test("Array contains element", function(){
    pm.expect(['Java', 'Postman']).to.be.an('array').that.is.not .empty
});
You can also try this code with Online Javascript Compiler
Run Code

 

Output

output

☘️ Assertion for Object Verification

Let us write an Assertion for object verification with ‘eql’. It is used to compare the properties of the object i and j in the below example.

⭐ Implementation

pm.test("Not Equal", function(){
let i = {
    "subject" : "Coding"
}
let j= {
    "subject" : "Ninja"
}
pm.expect(i).to.not.eql(j)
});
You can also try this code with Online Javascript Compiler
Run Code

 

Output

output

Frequently Asked Questions

What are the variables in Postman?

Postman variables are key-value pairs. Because each variable name represents its key, referencing the variable name gives you access to its value. Variables can be used to pass data between requests and tests, such as when chaining requests in a collection.

How do I give Postman JSON data?

Change the method next to the URL in Postman to 'POST,' and then under the 'Body' tab, select the 'raw' radio button and then 'JSON (application/json)' from the drop-down. You can now enter the JSON that will be sent with the POST request. If everything goes well, you should see the new data in your 'db. json' file.

What is get request in Postman?

A GET request gets the information from the server. The server responds to the GET request without affecting any data on the server, that is, there is no creation, updation, addition, or deletion of data on the server when you are making a GET request.

Conclusion

Congratulations🎉 You have successfully finished reading this article. We discussed how to create a test script. We also discussed using single assertions, multiple assertions and how to make assertions on the HTTP request. 

After reading this article, if you are interested in knowing more about Postman, we recommend you to read some of our articles -

🔥 Web Testing

🔥 API

🔥 API Testing

 

You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!

We wish you Good Luck!🎈 Please upvote our blog 🏆 and help other ninjas grow.

Live masterclass