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.
Reusable test scripts in 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.
function checkStatus200() {
pm.test("Status code is 200", () => {
pm.response.to.have.status(200);
});
}
checkStatus200();function checkResponseTime(maxTime) {
pm.test(`Response time is less than ${maxTime}ms`, () => {
pm.expect(pm.response.responseTime).to.be.below(maxTime);
});
}
checkResponseTime(300);function commonTests() {
checkStatus200();
checkResponseTime(500);
}
commonTests();This Postman test script defines two reusable functions and calls them to check the response status and time.
// 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);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.
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.
Practice
Solution
Step 1: Understand the purpose of reusable scripts
Reusable test scripts help avoid writing the same code repeatedly, which saves time and reduces mistakes.Step 2: Identify the correct benefit
Among the options, only saving time and reducing errors matches the purpose of reusable scripts.Final Answer:
They save time and reduce errors by avoiding repeated code. -> Option AQuick Check:
Reusable scripts = save time and reduce errors [OK]
- Thinking reusable scripts slow down tests
- Believing reusable scripts only work for GET requests
- Confusing reusable scripts with writing duplicate code
Solution
Step 1: Recall JavaScript function syntax
In Postman scripts, JavaScript functions are defined using the keywordfunctionfollowed by the function name and parentheses.Step 2: Identify the correct syntax
function checkStatus() { pm.test('Status is 200', () => pm.response.to.have.status(200)); } uses the correct JavaScript syntax. Options A, B, and C use invalid keywords or syntax not supported in JavaScript.Final Answer:
function checkStatus() { pm.test('Status is 200', () => pm.response.to.have.status(200)); } -> Option DQuick Check:
JavaScript function syntax = function name() {} [OK]
- Using Python or other language syntax like 'def'
- Using arrow function without assignment
- Using unsupported keywords like 'func'
function checkStatus() {
pm.test('Status is 200', () => pm.response.to.have.status(200));
}
checkStatus();Solution
Step 1: Understand the test logic
The functioncheckStatusruns a test that expects the response status to be 200.Step 2: Analyze the response status
The actual response status is 404, which does not match 200, so the test will fail.Final Answer:
Test fails because the response status is not 200. -> Option BQuick Check:
Expected 200, got 404 = test fails [OK]
- Assuming function call causes syntax error
- Thinking test passes if function runs
- Believing 404 status is ignored automatically
function checkResponse() {
pm.test('Response time is less than 200ms', () => {
pm.expect(pm.response.responseTime).to.be.below(200);
});
}
checkResponse
What is the error and how to fix it?Solution
Step 1: Check function call syntax
The functioncheckResponseis defined correctly but called without parentheses, so it does not execute.Step 2: Fix the function call
Adding parentheses()aftercheckResponsecalls the function properly and runs the test.Final Answer:
Missing parentheses when calling the function; add () after checkResponse. -> Option AQuick Check:
Function call needs () to execute [OK]
- Confusing assertion syntax with chai library
- Changing function names unnecessarily
- Using wrong response time property
Solution
Step 1: Understand the requirement
The function must check that the JSON response has the given key and that its value is not empty.Step 2: Analyze each option
function checkKeyExists(key) { pm.test(`Response has key ${key}`, () => { pm.expect(pm.response.json()).to.have.property(key).that.is.not.empty; }); } uses correct chai assertions:to.have.property(key).that.is.not.emptywhich checks key existence and non-empty value.
function checkKeyExists(key) { pm.test('Response has key', () => { pm.expect(pm.response.json()).to.include.key(key); }); } uses incorrect assertionto.include.keywhich is invalid.
function checkKeyExists(key) { pm.test(`Response has key ${key}`, () => { pm.expect(pm.response.json()[key]).to.exist; }); } checks only existence but not if value is non-empty.
function checkKeyExists(key) { pm.test(`Response has key ${key}`, () => { pm.expect(pm.response.json()).to.have.key(key).not.empty; }); } uses invalid chainingto.have.key(key).not.empty.Final Answer:
function checkKeyExists(key) { pm.test(`Response has key ${key}`, () => { pm.expect(pm.response.json()).to.have.property(key).that.is.not.empty; }); } -> Option CQuick Check:
Check key exists and not empty = function checkKeyExists(key) { pm.test(`Response has key ${key}`, () => { pm.expect(pm.response.json()).to.have.property(key).that.is.not.empty; }); } [OK]
- Using invalid chai assertion methods
- Checking only key existence without value
- Incorrect chaining of assertions
Ready to prove it?
Pass the test and this pattern moves unknown
