Table of contents
1.
Introduction
2.
Write test scripts in postman
2.1.
Validating responses 
3.
Testing collections and folders
4.
Running tests with monitors
4.1.
Monitoring an API endpoint and entire API
5.
Running API tests
6.
Monitoring HTTP response codes and latency
7.
Frequently Asked Questions
7.1.
What are Postman's restrictions?
7.2.
What does a status code of 301 mean?
7.3.
Why does Postman mostly use Base64 encoding?
8.
Conclusion
Last Updated: Mar 27, 2024

How to write tests in postman and run them with monitors in Postman?

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

Introduction

Developers can easily create, share, test, and document APIs with Postman, an API client. Users are given the ability to generate, store, and read both simple and complicated HTTP/s requests as well as their responses in order to achieve this. It is a program that offers a graphical user interface for testing APIs. The collection feature and the ability to construct several testing settings are a couple of Postman's benefits. 

Postman

To write postman tests verify that your API is operating as intended, that service integrations are reliable, and that any modifications haven't harmed already-existing functionality. JavaScript can be used to create test scripts for Postman API queries. When something goes wrong with your API project, test code might also help with the debugging process. You could, for instance, create a test to verify your API's error handling by submitting a request with inaccurate or incomplete information.

Individual requests, collections, and folders in a collection can all have tests added to them. You can add code snippets to Postman and then modify them to fit your test logic.

Write test scripts in postman

Dynamic variables, test assertions on response data, and data passing across requests are all capabilities of test scripts.

Step 1: Enter your JavaScript manually or choose Snippets adjacent to the code editor in the Tests tab for a request.

Step 2: Tests are run upon the reception of the response. When you choose Send, Postman executes your test script upon the API response data's return.

Validating responses 

You can use the pm. response object in a test to verify the information a request returns. Create tests by giving them a name and a function that produces a boolean (true or false) value indicating whether the test passed or failed using the pm. test function.

A text string that will show in the test result output is the first parameter for the. test method.

To run your request and display the Test Results in the response box, select Send. Additionally, you can see how many tests were passed, skipped, and failed.

Your test result messages will have a different format if you use the pm. expect syntax. To write your tests in a comprehensible style that works with your application and testing logic, you can use a variety of syntax options.

Snippets are still another approach. There is a collection of frequently used test code snippets in the Snippets section. Once you choose one, it will be added to your editor. Beginning your scripts more quickly is possible with snippets.

Testing collections and folders

A collection's folders, requests, and collections can all contain test scripts. After each request in a collection, a test script connected with that collection will be executed. Every direct child request in a folder causes a test script associated with it to run. You can test the workflows in your API project by adding scripts to collections and folders. This ensures that your requests address common scenarios and gives application users a trustworthy experience.

Step 1: Click the view more actions icon and choose edit to update collection and folder scripts.

Step 2: To add or change your script, select the Tests tab.

When you conduct a collection, the collection runner shows the test results, along with numerous other details including the response time in milliseconds.

Running tests with monitors

Postman Monitors offer a mechanism to schedule the automatic execution of test scripts and other tests. When configuring a collection-based monitor, you select the requests and test scripts you wish to perform as well as the frequency at which Postman should run the collection.

Monitoring an API endpoint and entire API

Create a collection containing various iterations of the same endpoint in various queries in order to monitor a certain endpoint. The goal is to thoroughly cover the endpoint by testing the responses for each option.

The process for monitoring a complete API is identical to that for monitoring a single endpoint, with the minor exception that the common API host is stored in an environment variable, causing requests to distinct API endpoints to have separate request parameters and a different request path. A similar sequence also enables data chaining across requests, enabling comprehensive API testing.

Running API tests

Exact knowledge of how each endpoint functions is essential in an API when different endpoints are linked to one another. The full response, or only a portion of it, can be preserved as an environment variable in situations where data is transmitted from one request to another. Since the original value will be lost when setting non-atomic values (such as objects and arrays), take particular care. The following options are available for handling it:

// set the value
postman.setEnvironmentVariable('complexObj', JSON.stringify(myComplexObjOrArray, null, 2));

// Fetch the value
var foo;
try {
    foo = JSON.parse(postman.getEnvironmentVariable('complexObj'));
}
catch (e) {
    console.error(e);
    foo = { __parseError: true };
}
if (foo.__parseError) {
    // handle parse errors here
}

Monitoring HTTP response codes and latency

By examining the value of responseCode.code within test scripts, response code tests may be carried out.

The responseTime variable's values in test scripts can be compared to track website response latency.

Frequently Asked Questions

What are Postman's restrictions?

For handling a thousand or more API requests, Postman is inadequate. Managing the collections and requests can be challenging if the project is too large. If we wish to control the workspace using code, it is not appropriate because there would be a lot of code duplication for calls to dynamic APIs.

What does a status code of 301 mean?

A permanent redirect from one online page to another is represented by the status code "301." It informs the search engine that the current page is out-of-date and that it needs to index the URL of the new page.

Why does Postman mostly use Base64 encoding?

The main reason base64 encoding is employed is that it performs data transmission in a textual format that is simpler to send in HTML form statistics requests. Another reason we utilize this is that encoding with exactly 64 characters is highly dependable in every language we employ.

Conclusion

To conclude this blog, we discussed how to write test scripts in postman, and validate responses. We also looked at testing collections and folders. Then we discussed running tests with monitors, monitoring API endpoints, and an entire API. last but not least, we discussed running API tests and monitoring HTTP response codes and latency. 

If you desire to explore more about web testing, refer to this article. For API and API testing, you may go through these articles

For more content, Refer to our guided paths on Coding Ninjas Studio to upskill yourself.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Thankyou
Live masterclass