Expect for a string containing special chars

Hi all,

I want to assert that this string is in the response:

5 execution(s) of my-azure-function-dev/function-name failed

I test it this way:

pm.test('Body Content', function() {
        pm.expect(resp[1].message.text).to.contain('5 execution(s) of my-azure-function-dev/function-name failed');
    });

I already tried to escape parenthesis, slash, hyphenโ€ฆ but I dont figure out how to match that piece of string.

How to?

Can you console log the response element on its own and share a screenshot?
Or share the error when it fails, which should hopefully tell you what its trying to compare.

console.log(resp[1].message.text);

Or define that element in its own variable for the comparison.

If the response is a string, then no escape characters should be needed.

If I define a string with that text, then the assertion appears to be work ok, so therefore Iโ€™m wondering if the issues is with the string response, rather than the assertion.

let string = "5 execution(s) of my-azure-function-dev/function-name failed"

pm.test('Body Content', function() {
    pm.expect(string).to.contain('5 execution(s) of my-azure-function-dev/function-name failed');
});

pm.test('Body Content', function() {
    pm.expect(string).to.contain('4 execution(s) of my-azure-function-dev/function-name failed');
});

First test passes, and second one fails as expected.

As the string is fairly long, you could also do this so you can check the two strings in the console log.

let actualResponse = resp[1].message.text;
let expectedResponse = "5 execution(s) of my-azure-function-dev/function-name failed"

console.log(actualResponse);
console.log(expectedResponse);

pm.test('Body Content', function() {
    pm.expect(actualResponse).to.contain(expectedResponse);
});
1 Like

Putting it in a variable and using double quotes instead of single ones helped.