How to print the results of pm.test in console?

I want to show the results of pass/fail that comes from pm.test in console, I want to print it using e.g. console.log.

Thanks.

The result of the assertions is already available in the tests tab of the response pane for a request.

If you need additional information, you can use console.log and the data will show up in the console.

Hi @allen.helton. thanks mate, but I need to know how to print the pm.test results specifically in the console.
The purpose is I am making a sort of a test management environment where I send results of each test cases’ steps for record keeping.

The console.log doesn’t show the results of pm.test, and specifically I am trying to show the results of my schema validation.

I would love if you have a solution on how to print the results of schema validation, not necessarily using pm.test.

Thanks.

I really think I’m missing something fundamental in what you’re trying to do.

I’m assuming you’re using javascript in the tests tab of the request to run the assertion. When I do schema validation in Postman I use ajv. So if I wanted to print the results of ajv to the console I would just do:

const valid = ajv.validate(mySchema, jsonData);
const errors = ajv.errorsText(valid.errors);
console.log(errors);
1 Like
pm.test("Status code is 200", function () {

    try {

        pm.response.to.have.status(500);

    } catch (e) {

        console.log(e)

        throw Error(e)

    }

});

just wrap except with try catch , and throw the exception back as an error

1 Like

You’d only see the fails in the console that way though right? If the assertion passed, it wouldn’t log anything :smiley: I think you would also only see the first failed assertion and not all assertions.

The original question was about passes and fails, although the question is a little vague.

@tarekalba Can you provide more context and an example of what you’re trying to achieve, please?

2 Likes

If pass then add a custom console message in the try block , if fail then print in console inside catch block.

But as @dannydainton pointed out this use case doesn’t sound as good practice @tarekalba make sure you are not reinventing the wheel .

if you just have to print message with assertiong you can do like

 pm.test("Explicitly ", function () {
    let Ajv = require('ajv');
    let ajv = Ajv()
    let result = ajv.validate(schema, jsonData);
    pm.expect(result, JSON.stringify(ajv.errors)).to.be.true;
});
1 Like

Thanks guys, my solution as @allen.helton is the following:

pm.test(“Validation schema”, () => {pm.response.to.have.jsonSchema(schema);});

var Ajv = require(‘ajv’),

ajv = new Ajv({logger: console});

const valid = ajv.validate(schema, pm.response.json());

const errors = ajv.errorsText(valid.errors);

console.log(errors);