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.
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 @allenheltondev. 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);
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
Youâd only see the fails in the console that way though right? If the assertion passed, it wouldnât log anything 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?
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;
});
Thanks guys, my solution as @allenheltondev 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);