Hi Team,
I have a question regarding Postman test results.
We add some scripts to add assertion. With these assertions, we get results as Pass or Fail. But I need to get the result of Test case logged or stored in Environment variable. How can I achieve this?
Test scripts written in Postman are an extension of mocha + chai for testing and assertions. There currently isn’t a way to access test results programmatically from within the sandbox.
Like @gbadebo-bello mentioned, there’s isn’t direct programmatic access but you could try something like this to track the tests passing and store that as a variable:
// counter for passed tests
// pm.test.index() gives you the total number of executed tests in the script
let pass = 0
let totalToPass = pm.test.index()
pm.test("Foo to equal Foo", () => {
pm.expect("Foo").to.equal("Foo")
pass += 1
})
pm.test("Bar to equal Bar", () => {
pm.expect("Bar").to.equal("Bar")
pass += 1
})
pm.test("1 to equal 1", () => {
pm.expect(1).to.equal(1)
pass += 1
})
console.log(pass)
pm.globals.set("TotalTestsPass", pass)
Other logic could be applied to spilt that by Passed / Failed numbers too.