I wish to log ALL pm.test names from test tab.
for example:
// Store all test names
const testNames = Object.keys(pm.testResult.getTestNames());
// Log test names to console
console.log("List of pm.test names:");
testNames.forEach(name => console.log(name));
Additionally also with current execution status for mp.test
how to achieve that?
You can’t access the test results programmatically.
Please see the following topic for more information and a potential work around.
https://community.postman.com/t/get-testresults-value-passed-failed-saved-in-a-variable
If you just want the test case names, you could potentially call the Postman Collection API which returns all of the details related to a collection as a JSON response.
You could then search for the particular request and get access to the code in the tests tab. Have a look at the submit requests on the 15 day of Postman for Testers or 30 Days of Postman for Developer challenges to see examples on how to query the collection JSON. (I’ll show an example at the bottom of this post).
What is your use case for this. (Over just reading the test results tab)?
Example of working with the Collection API. (Although this seems like overkill).
let collection = pm.response.json().collection
pm.test("Status code is 200", () => {
pm.response.to.have.status(200);
});
pm.test("Correct collection returned", () => {
pm.expect(collection.info.name).equals("Day 11: Validation")
})
let folder = collection.item.find(fol => {return fol.name === "Validation"})
pm.test("Requests added correctly", () => {
pm.expect(folder.item.length, 'check number of requests').equals(1)
let berryRequest = folder.item.find(req => { return req.name === "pokemon berry"})
pm.expect(berryRequest.name, 'check name').equals("pokemon berry")
pm.expect(berryRequest.request.method, 'check method').equals("GET")
})
pm.test("Validation data added", () => {
let berryRequest = folder.item.find(req => { return req.name === "pokemon berry"})
let event = berryRequest.event.find(e => {return e.listen === "test"})
let script = event.script.exec.toString()
pm.expect(script, 'check parse').contains("pm.response.json")
pm.expect(script, 'check successful').contains("200")
pm.expect(script, 'check unsuccessful').contains("404")
pm.expect(script, 'check test').contains("pm.test")
pm.expect(script, 'check schema').contains("jsonSchema")
})