Get TestResults value PASSED/FAILED saved in a variable

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?

I need help to figure out the best way here.

Regards,
Pradeep.

Hi @pradeepramakutty. Welcome to the Postman Community.

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.

Hi @danny-dainton and @gbadebo-bello ,
Thanks for your reply and solution.

This worked good and helpful in my case.
Thanks once again :slight_smile:

Regards,
Pradeep.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.