Do you think IIT Guwahati certified course can help you in your career?
No
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.
Scriptsis 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.
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
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
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
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 -