# How do I print message along with my test

**URL:** <https://community.postman.com/t/how-do-i-print-message-along-with-my-test/39533>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [September 27, 2022, 10:53pm UTC](https://community.postman.com/t/how-do-i-print-message-along-with-my-test/39533 "2022-09-27T22:53:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasmukhpatel](https://avatars.discourse-cdn.com/v4/letter/h/eada6e/32.png) [@hasmukhpatel](https://community.postman.com/u/hasmukhpatel)\
**Post date:** [September 27, 2022, 10:53pm UTC](https://community.postman.com/t/how-do-i-print-message-along-with-my-test/39533/1 "2022-09-27T22:53:48Z")

</div>

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”)

```auto
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();');

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 28, 2022, 8:53am UTC](https://community.postman.com/t/how-do-i-print-message-along-with-my-test/39533/2 "2022-09-28T08:53:45Z")

</div>

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.

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

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/8/a/8a0f23877edc8814bb199bc4c4e82058028539a4.png)

---

<div class="post-metadata">

**Author:** ![hasmukhpatel](https://avatars.discourse-cdn.com/v4/letter/h/eada6e/32.png) [@hasmukhpatel](https://community.postman.com/u/hasmukhpatel)\
**Post date:** [September 28, 2022, 3:05pm UTC](https://community.postman.com/t/how-do-i-print-message-along-with-my-test/39533/3 "2022-09-28T15:05:25Z")

</div>

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();’);

```auto

```

---

<div class="post-metadata">

**Author:** ![hasmukhpatel](https://avatars.discourse-cdn.com/v4/letter/h/eada6e/32.png) [@hasmukhpatel](https://community.postman.com/u/hasmukhpatel)\
**Post date:** [September 28, 2022, 3:15pm UTC](https://community.postman.com/t/how-do-i-print-message-along-with-my-test/39533/4 "2022-09-28T15:15:57Z")

</div>

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();’);

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 28, 2022, 4:33pm UTC](https://community.postman.com/t/how-do-i-print-message-along-with-my-test/39533/5 "2022-09-28T16:33:02Z")

</div>

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…

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

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

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 28, 2022, 4:48pm UTC](https://community.postman.com/t/how-do-i-print-message-along-with-my-test/39533/6 "2022-09-28T16:48:51Z")

</div>

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).

```auto
{
    "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.

```auto
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](https://us1.discourse-cdn.com/getpostman/original/3X/6/8/6857dc268cc180acb7d2e640b64afc594ad6b201.png)

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

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [September 18, 2023, 11:36am UTC](https://community.postman.com/t/how-do-i-print-message-along-with-my-test/39533/7 "2023-09-18T11:36:06Z")

</div>

Closed on September 18, 2023, 11:36am UTC

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