Use environment variable to object property name in tests

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

My question:
How in postman test use environment variable to object property name?

Details (like screenshots):
I get a response from request like this:
{
“something”: {
“81071”: “Test name 1”,
“81072”: “Test name 2”
}

How I can use environment variables to make a test of id’s i geted (81071, 81072)?

How I found the problem:
I get an object from response of my request and can’t test it with an environment variables.

I’ve already tried:
I make test like this but i have an error.

pm.test(‘Value check’, () => {
const expectedObject = {
“something”: {
“pm.environment.get(‘id1’)”: “Test name 1”,
“pm.environment.get(‘id2’)”: “Test name 2”
}
};

pm.expect(response).to.deep.include(expectedObject);

});

response = pm.response.json();

pm.collectionVariables.set("id1", "81071");
pm.collectionVariables.set("id2", "81072");

const expectedObject = {
    "args": {
        [pm.collectionVariables.get("id1")]: "Test name 1",
        [pm.collectionVariables.get("id2")]: "Test name 2"
    }
};

pm.test("Value check", () => {
    pm.expect(response).to.deep.include(expectedObject);
});
1 Like

Thank you, for help! I didn’t know that I need to use ‘[]’ to environment variable if it in the property position. Have a nice day!