[Resolved] Does anyone know how to show a value from response in name of pm.test

Hi, community,

I have an endpoint that returns me the following response:

[{"name":"external","value":"1.220","timestamp":1660280400000},
{"name":"external","value":"1.187","timestamp":1660284000000},
{"name":"external","value":"1.100","timestamp":1660287600000}]

Is it possible to show concatenated string from the last 3 elements of the array in the name of the test? (in iteration results it should be something like: Pass + 1.10, 1.18, 1.22 as name of the test)

Is it possible to Fail the test if the value of the last (newest) element is less than 0.8 or greater than 2?

The option to show the output in the console is not appropriate for me because it will be hundreds of requests to the endpoint in the collection (to get values from different sensors) and it will be necessary to keep eyes only on sensors with recent values which are out of a sensor-specific range (failed tests).

@abcdefghi0jklmnopqr0

One thing at a time, the concatenated string for the test name is fairly easy. You need to create the string outside of the test. Probably the first action in your tests tab. You probably need to have extra code that checks if the response is null first, and writes something else to the variable you are going to use in the test case name so its never blank.

const response = pm.response.json();
var concat = (response[response.length - 1].value) +","+(response[response.length - 2].value)+","+(response[response - 3].value);
console.log(concat);

pm.test(`${concat} - Test concat in test name`, () => {
    pm.response.to.have.status(200);
});

1 Like

@abcdefghi0jklmnopqr0

The following is the logic for your second question around asserting a value between two numbers.

Number.prototype.between  = function (a, b) {
    var min = Math.min(a,b),
        max = Math.max(a,b);
    return this > min && this < max;
};

pm.test('Test value between two numbers', () => {
    var Value = 0.8;
    var checkValue = (Value.between(0.7, 2.1));
    console.log(checkValue)
    pm.expect(checkValue).to.eql(true);
});
1 Like

Thank you so much for the answers.
I just used two checks in the test as a workaround

    pm.expect(current).be.above(pm.iterationData.get("low"), "low")
    pm.expect(current).be.below(pm.iterationData.get("high"), "high")

but your solution is more elegant, hope it will be also faster :slight_smile: