postman
  1. postman-test-scripts

Test Scripts for Postman

Syntax

Test scripts are written in JavaScript, which is a scripting language used by Postman.

Example

Here's an example of a test script in Postman:

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

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

pm.test("Response body is valid JSON", function () {
    pm.response.to.have.jsonBody();
    pm.response.to.be.success;
});

Output

The output will vary depending on the specific test script being implemented. In the above example, the output will display whether each test passed or failed.

Explanation

Test scripts in Postman are used to verify the responses received from APIs or web applications. They are written in JavaScript and can check for a wide range of conditions, including status codes, response times, and response body content. Test scripts run in the background and are executed after sending a request to an API or web application.

Use

Test scripts are essential when working with APIs or web applications in Postman. They ensure the correctness of the response from API endpoints and web applications. These scripts can be used to check status codes, response times, response content in desired format like json, xml, html etc.

Important Points

  • Test scripts in Postman require a basic understanding of JavaScript.
  • Test cases should be independent of each other.
  • Good use of error handling that allow repetitive check in a single script instead of writing individual test scripts for each case.

Summary

Test scripts in Postman allow you to verify the responses received from APIs or web applications. By using test scripts, you can ensure that the API response is correct, the response time is acceptable, and the response body contains the expected content. Test scripts should be written with care to ensure that they are accurate, reliable, reusable, and easy to maintain.

Published on: