Postman Sandbox Assertions, Data‑Driven Testing, Workflow, Newman CLI, and Jenkins Integration
This guide explains how to use Postman's JavaScript sandbox for pre‑request and test scripts, create assertions, run collections in batch, apply data‑driven testing, control request order with workflows, execute tests via Newman, and integrate the process into Jenkins for continuous testing.
Postman provides a JavaScript sandbox that allows you to write pre‑request scripts (similar to a unit test setUp() ) and test scripts that run after a response is received.
Assertion example : For a POST request to postman-echo.com/post , the expected response status is 200, the returned user value must match the defined variable, and the response time should be under 0.5 s. The test script sets a variable user and then checks status, response content, and response time using pm.test and pm.expect .
pm.variables.set("user",'zxw');
// Status code check
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// Get variable value
username = pm.variables.get("user");
console.log(username);
// Verify response content
pm.test("Check username", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.json['user']).to.eql(username);
});
// Verify response time
pm.test("Response time is less than 500ms", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});Batch running collections : Use the Collection Runner to execute multiple API requests together, configuring environment variables, iteration count, and delay between requests.
Data‑driven testing : Import an external JSON file (e.g., data.json ) containing different parameter sets, such as usernames and passwords, to run the same request with varied data. Example data file:
[
{"username": "jack", "passwd": "6666"},
{"username": "Bob", "passwd": "5555"},
{"username": "Marry","passwd": "8888"}
]Set a request delay of 1000‑3000 ms to avoid throttling and remember to save environment variables.
Building a workflow : Control request execution order by adding postman.setNextRequest('Request 3') (or other request names) in the Test script of each request.
postman.setNextRequest('Request 3')
postman.setNextRequest('Request 2')
postman.setNextRequest('Request 4')Newman CLI : Newman, a Node.js‑based tool, runs Postman collections from the command line. Example command:
newman run Postman_API.postman_collection.json -d data.json -r htmlOptions: run specifies the collection, -d provides the data file, and -r selects the report format (HTML).
Jenkins integration : Create a Jenkins job, add a Windows batch command step, and execute the Newman command. Example batch script:
c:
cd C:\Users\Shuqing\Desktop\pmtest\
newman run Postman_API.postman_collection.json -d data.json -r htmlThe job runs the collection and produces an HTML test report.
Additional features : Postman can export scripts in various languages and capture mobile‑device traffic.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.