2

I have a collection of simple API requests, and I'm running into an issue with one of the tests I've created.

I have a request which creates some content, and I use the built-in '$randomMACAddress' variable to generate a random name for the list I'm creating. This is stored in an env var called 'listName'. In a test further down in the flow, I assert that the list I'm retrieving has a name which matches '$randomMACAddress':

    pm.test("List details are correct"), function () {
        pm.expect(jsonData.name).equal($randomMACAddress);
    }

This test PASSES.

I have the same check in another test, but this time it fails, and Postman tells me the following:

ReferenceError: $randomMACAddress is not defined

The test in that request is as follows:

    pm.test("List details are correct", function () {
        pm.expect(jsonData.id).equal(pm.globals.get('listID'));
        pm.expect(jsonData.name).equal($randomMACAddress);
        pm.expect(jsonData.products[0].skuId).equal('xyz');
    });

The requests/tests are run at the same time (collection runner) and I'm baffled as to why that assertion fails on the latter test.

Tried to initialise things differently, but that hasn't worked.

Jason
  • 75
  • 1
  • 1
  • 8

1 Answers1

0

The way to access the stored data would be via the pm.variables.get("variable_name") function and not using $randomMACAddress.

More info here:

https://learning.getpostman.com/docs/postman/environments_and_globals/variables

Also, the chai syntax would be to.equal()

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80