I have this test in JavaScript in the Tests tab of the Parent folder (I will have the same 401 test in multiple child folders – to reduce duplications in test)
const response = pm.response.json();
pm.test("Status code is 401", ()=> {
pm.response.to.have.status(401);
});
In the Child folder
, I am trying to post a POST request, while having this test in the Tests tab:
pm.test('Error message', () => {
pm.expect(response.error).to.eql('Missing Authorization header.');
})
I’m getting:
PASS
Status code is 401
FAIL
Error message | ReferenceError: response is not defined |
I understand that the 2nd test (the one in the child folder) is failing because the variable response
is not defined in the Child folder.
My question is: why is the variable not passed from the Parent folder to the Child folder? As I can see, the 401 function is being passed, but the variable is not. I know the solution, I just do not understand the logic behind the variable not being passed, but the function is passed.
Setting the variable as a global variable in the parent folder script and then accessing the global variable in the child folder script works and is fully understood. What I do not understand is why, in my example, the function for testing the 401 status is being passed from the Parent folder to the Child folder, but not the variable response