Collection Runner fails a test that succeeds when run manually

Hi, I’m having a problem where the collection runner is failing a test that succeeds when run manually.

My test checks to see if the response from the server contains the staff number I sent in.
I have a CSV data file with four staff numbers and an environment variable with the same {{StaffNumber}} template set as one of the test staff numbers.

pm.test("Response contains correct user", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.staffnumber).to.equal(pm.iterationData.get("staffnumber"));
});

When the request is run manually, this test passes as I would expect it to. However if I run it using the collection runner with a data file, the test fails with the result:

AssertionError: expected undefined to equal 40000871

… rinse and repeat for every staff number in the data file.
When I inspect each runner iteration, the response body does infact have the correct value there, in exactly the same format as when I run it manually.

What am I doing wrong here that I can’t figure out?

Hey @jacksos

Welcome to the community!! :rocket:

Just due to the relationship between the request builder and the Collection Runner, are you saving the changes in the Request before running them through the runner? Basically, is there a little orange dot on the request tab.

Could you share an example of the response body, just so we can check that out please?

Is it staffnumber or staffNumber in the response?

You could also try adding console.log(jsonData) before the pm.expect() line, just so you can see what that variable is returning, might be something going on in there?

Hi there @danny-dainton,

Thanks for the reply!
While I was in the console I noticed the capitalization on the jsonData.staffnumber was wrong.
Correcting it switched the error response around and succeeded in the runner but not manually.

I’ve realised that while not in a collection runner session that pm.iterationData is null.
So where I was getting successes before, was because both side of the test equal undefined.

So I have corrected my test to include a check and run a different but still validating test, which has resolved my issue.

pm.test("Response contains correct user", function () {

    var respData = pm.response.json();

    if (pm.environment.get('runner') == 'true')
    {
        pm.expect(respData.staffNumber).to.equal(pm.iterationData.get("StaffNumber"));
    }
    else {
        pm.expect(respData.staffNumber).to.equal(parseInt(pm.environment.get("StaffNumber"), 10));
    }
    
});

Apologies, that was a stupid mistake. :sweat_smile:
But thanks for mentioning the console which poked me in the right direction!

1 Like

Glad you got it sorted!! If you get stuck again, feel free to reach out for help!! :trophy: