0
0
Postmantesting~5 mins

Reusable test scripts in Postman

Choose your learning style9 modes available
Introduction

Reusable test scripts save time by letting you write tests once and use them many times. This helps keep your tests organized and easy to update.

When you have common checks like status code or response time for many API requests.
When you want to run the same validation on different endpoints without rewriting code.
When you want to keep your test collection clean and avoid repeating code.
When you need to update a test logic and want the change to apply everywhere automatically.
When you want to share test logic with your team easily.
Syntax
Postman
pm.test("Test name", function () {
    // test code here
});

// To reuse, create a function and call it in tests
function commonTests() {
    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);
    });
}

// Call reusable function in tests
commonTests();

Define reusable functions outside individual tests to call them multiple times.

Use Postman's pm object to access request and response data inside reusable scripts.

Examples
This example defines a simple reusable function to check if the status code is 200, then calls it.
Postman
function checkStatus200() {
    pm.test("Status code is 200", () => {
        pm.response.to.have.status(200);
    });
}

checkStatus200();
This reusable function checks if the response time is below a given limit, making it flexible.
Postman
function checkResponseTime(maxTime) {
    pm.test(`Response time is less than ${maxTime}ms`, () => {
        pm.expect(pm.response.responseTime).to.be.below(maxTime);
    });
}

checkResponseTime(300);
Combines multiple reusable tests into one function for easy reuse.
Postman
function commonTests() {
    checkStatus200();
    checkResponseTime(500);
}

commonTests();
Sample Program

This Postman test script defines two reusable functions and calls them to check the response status and time.

Postman
// Reusable test functions
function checkStatus200() {
    pm.test("Status code is 200", () => {
        pm.response.to.have.status(200);
    });
}

function checkResponseTime(maxTime) {
    pm.test(`Response time is less than ${maxTime}ms`, () => {
        pm.expect(pm.response.responseTime).to.be.below(maxTime);
    });
}

// Use reusable tests in this request
checkStatus200();
checkResponseTime(400);
OutputSuccess
Important Notes

Reusable scripts help keep your tests DRY (Don't Repeat Yourself).

Keep reusable functions simple and focused on one check for easier maintenance.

You can store reusable scripts in Postman's Collection or Environment scripts for sharing.

Summary

Reusable test scripts save time and reduce errors by avoiding repeated code.

Define reusable functions and call them in your tests.

Use Postman's pm API to write flexible and clear reusable tests.