You can’t access the test results programmatically.
Please see the following topic for more information.
https://community.postman.com/t/get-testresults-value-passed-failed-saved-in-a-variable
What you can do though is edit the test cast and create the variable and result here instead.
For example.
pm.test("Status code is 200", () => {
// set inital status to fail
pm.collectionVariables.set("testStatus", "FAILED")
// your assertion
pm.response.to.have.status(200);
// If the assertion fails, then the current pm.test execution will end immediately.
// No further code in this test block will execute.
// The testStatus will remain as failed.
// If the next line executes, it means that the assertion passed.
pm.collectionVariables.set("testStatus", "PASSED")
});
If I force the assertion to fail by changing the status code to 201.
The status of the variable is now failed.