How to execute Postman Collections through the Monitor API or the Newman Javascript library using Mocha.
Postman - Generating an API Key
Postman - Forking a Collection
Collection
Monitor
- API Key -- PM_API_KEY
- Collection ID -- PM_COLLECTION_ID
- Monitor ID -- PM_MONITOR_ID
npm install
export PM_API_KEY=***
export PM_COLLECTION_ID=***
export PM_MONITOR_ID=***
npm test
npm run test-newman
npm run test-monitor
npm run test-parallel
// Post Request to Monitor API
const response = await axios.post(
`https://api.getpostman.com/monitors/${monitorId}/run?apikey=${apiKey}`,
{}
);
// Format and Log Response
runData = response.data.run;
for (const entry of runData.executions) {
const req = entry.request;
const res = entry.response;
console.log(`\n${entry.item.name}`);
console.log(
`${req.method} ${req.url} [${res.code}, ${res.responseSize}B, ${res.responseTime}ms]`
);
}
// Await Newman Response as Promise
await new Promise((resolve, reject) => {
newman
.run({
collection: `https://api.getpostman.com/collections/${collectionId}?apikey=${apiKey}`,
reporters: ["cli"],
})
.on("done", (error, summary) => {
if (error) {
reject(error);
}
resolve(summary);
});
});
CircleCI - Setting Environment Variables
CircleCI - Workflows Configuration Examples
version: 2.1
orbs:
node: circleci/[email protected]
jobs:
test:
executor:
name: node/default
tag: "14.15.4"
steps:
- checkout
- node/install-packages
- run:
command: npm test
- store_test_results:
path: test-results
- store_artifacts:
path: test-results/newman/html-results.html
workflows:
newman-and-monitor:
jobs:
- test
https://github.com/postmanlabs/newman
https://github.com/mochajs/mocha
https://github.com/DannyDainton/newman-reporter-htmlextra
https://github.com/stanleyhlng/mocha-multi-reporters
https://github.com/standard-things/esm