How do I print message along with my test

I have following code and I want to print message when I run
e.g. I want to add pm.test(“verifying json body or not”)

postman.setGlobalVariable("verifyJsonBodyOrNot", function verifyJsonBodyOrNot() {
    let jsonHelpers = {};

    jsonHelpers.verifyJsonBody = function verifyJsonBody(){
        pm.response.to.be.withBody;
        pm.response.to.be.json;
    }

    jsonHelpers.verifyNoJsonBody = function verifyJNosonBody(){
        pm.response.to.not.be.withBody;
        pm.response.to.not.be.json;
    }

    return jsonHelpers;
} + '; verifyJsonBodyOrNot();');

When you say print, do you mean to the console log?

The obvious way to use console.log is to show the values of variables, but you can also just show a string.

console.log("Verifying Json body or not");

image

Thanks for reply @michaelderekjones .
No I don’t want to print on console. in collection it should say test ran or not. I want to create test for each jsonhelper, I was trying following code but getting error

postman.setGlobalVariable(“verifyJsonBodyOrNot”, function verifyJsonBodyOrNot() {
let jsonHelpers = {};

jsonHelpers.verifyJsonBody = pm.test("Verifying response have Json body"), function verifyJsonBody(){
    pm.response.to.be.withBody;
    pm.response.to.be.json;
}

jsonHelpers.verifyNoJsonBody = pm.test("Verifying response doesn't have Json body"),function verifyJNosonBody(){
    pm.response.to.not.be.withBody;
    pm.response.to.not.be.json;
}

return jsonHelpers;

} + ‘; verifyJsonBodyOrNot();’);

currently I am doing this way
postman.setGlobalVariable(“verifyJsonBodyOrNot”, function verifyJsonBodyOrNot() {
let jsonHelpers = {};

jsonHelpers.verifyJsonBody = function verifyJsonBody() {
    tests["Verifying response have Json body"] = pm.response.to.be.withBody;
    tests["Verifying response have Json"] = pm.response.to.be.json;
}

jsonHelpers.verifyNoJsonBody = pm.test("Verifying response doesn't have Json body"), function verifyJNosonBody() {
    tests["Verifying response have Json body"] = pm.response.to.not.be.withBody;
    tests["Verifying response have Json"] = pm.response.to.not.be.json;
}

return jsonHelpers;

} + ‘; verifyJsonBodyOrNot();’);

Sorry, I don’t quite get what you are trying to achieve.

All you are doing at the moment, is setting a global variable (which is probably not the scope you need) with the text of the code that follows (as a string).

It’s not actually doing anything else as far as I can tell.

What is it you actually want to test? That the response is JSON or not?

A quick way to do that…

var contentType = pm.response.headers.get("Content-Type");

pm.test("Is response JSON?", () => {
    pm.expect(contentType).to.contain("application/json");
});`

If you want to validate the JSON schema, then this is covered in the Postman training.

Considering the following JSON. (The response to a basic request to Postman Echo).

{
    "args": {
        "test": "123"
    },
    "headers": {
        "x-forwarded-proto": "https",
        "x-forwarded-port": "443",
        "host": "postman-echo.com",
        "x-amzn-trace-id": "Root=1-633479d9-3cb2e3034e5c1f901ba6eddc",
        "user-agent": "PostmanRuntime/7.29.2",
        "accept": "*/*",
        "postman-token": "d8b0a5d9-98d0-4dab-852c-48d57f6446b4",
        "accept-encoding": "gzip, deflate, br",
        "cookie": "sails.sid=s%3APz63PSoDDYaxSdy8PzrIXSpFjIjGSUzt.fVDpmspnKx0VtTF6G20Cr498EQhe0Ij7iz1ALbDdsXo"
    },
    "url": "https://postman-echo.com/get?test=123"
}

Then you can validate the schema using the following.

const schema = {
    'type': 'object',
    'properties': {
        'args': {
            type: 'object'
        },
        'headers': {
            type: 'object'
        },
        'url': {
            type: 'string'
        }
    },
    required: ['args', 'headers', 'url']
};

const response = pm.response.json();

pm.test('Schema is valid', () => {
    pm.expect(response).to.have.jsonSchema(schema);
});

image

Still not sure if this is actually what you are trying to achieve.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.