Skip to content

Instantly share code, notes, and snippets.

@jordanwalsh23
Last active February 5, 2024 04:47
Show Gist options
  • Select an option

  • Save jordanwalsh23/9ffe5620f2b5b1683b969b0577254193 to your computer and use it in GitHub Desktop.

Select an option

Save jordanwalsh23/9ffe5620f2b5b1683b969b0577254193 to your computer and use it in GitHub Desktop.
Pre-Request Script for Postman Testing Framework
//Set the Test Case ID
pm.collectionVariables.set("testCaseId", pm.execution.location[pm.execution.location.length - 2]);
failTest = (_pm, testFailedException) => {
let testCaseId = _pm.collectionVariables.get("testCaseId")
let results = JSON.parse(_pm.collectionVariables.get("results") || "{}");
console.log("Checking Test: ", testCaseId);
console.log("Got Results", results)
if (!results[testCaseId]) {
results[testCaseId] = [];
}
console.log("Logging an exception for: " + testCaseId);
results[testCaseId].push({
reason: testFailedException,
location: _pm.execution.location
});
_pm.collectionVariables.set("results", JSON.stringify(results));
_pm.test(`${testCaseId}`, () => {
_pm.expect.fail(results[testCaseId][0].reason.message)
})
}
continueTesting = (_pm) => {
//Get the current TestCaseId from the ephemeral variable.
let testCaseId = _pm.collectionVariables.get("testCaseId")
let results = JSON.parse(_pm.collectionVariables.get("results") || "{}");
if (results && results[testCaseId] && results[testCaseId].length > 0) {
//Skip this test
return false;
} else {
return true;
}
}
if(!continueTesting(pm)) {
pm.execution.skipRequest();
}
//get the tests to run
let testsToRun = pm.collectionVariables.get("testsToRun");
if (testsToRun && testsToRun != "") {
//Check what level we are at. We only care about this check at level 4.
if (pm.execution.location && pm.execution.location.length == 4) {
//Because we're == 4 we should be on a test step.
//Create the array of different test types
let testTypesArray = testsToRun.split(",");
//get the test case name
let testCaseId = pm.collectionVariables.get("testCaseId");
console.log(testCaseId)
let skipTest = true;
testTypesArray.every(testType => {
var regex = new RegExp(`(${testType.trim()})`, 'gi');
//Match the test case type
let testCaseType = testCaseId.match(regex);
console.log(`Checking if ${testCaseId} matches ${testType}... Result: ${testCaseType ? true : false}.`);
if (testCaseType) {
skipTest = false;
return false;
}
return true;
})
if (skipTest) {
console.log("We should skip this test.")
//Current test case does not match the supplied list. We should skip this test case.
let results = JSON.parse(pm.collectionVariables.get("results") || "{}");
if (!results[testCaseId]) {
results[testCaseId] = [];
}
results[testCaseId].push({
reason: "Test doesn't match expected test type.",
location: pm.execution.location
});
pm.collectionVariables.set("results", JSON.stringify(results));
pm.execution.skipRequest();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment