Reputation: 34489
I'm trying to use Postman to test an API I'm building with express, within it I'm building up a suite of tests. Here's a quick example:
tests["Status code is 200"] = responseCode.code === 200;
// Check that we got the expected board
var expected = { _id: 11, name: "board A" };
tests["Board retrieved"] = JSON.stringify(expected) === responseBody;
I'm finding however that when my tests might fail, they don't tell me anything useful:
Is there a different way to validate the result, that will let me know the expected/actual values like traditional test runners that anyone is aware of? The only thing I can think of so far is to embed the information into the test name - which feels a bit of a cludge.
Upvotes: 3
Views: 92
Reputation: 10342
A possible work-around could be wrapping the assignation to add more functionality:
/** This function adds information in the label only if the validations returns false */
tests.assertEquals= function (expected, actual, label) {
if (expected!==actual) {
this[label + ': Expected "' +expected + '" but got "'+ actual +'"']=false;
} else {
this[label]=true;
}
}
tests.assertEquals(JSON.stringify(expected), responseBody,"Board retrieved");
As you said, it could be a cludge, but you only have this information if it's necessary and using a method to keep it 'hidden' makes it cleaner and easier to read. Another option could be to use the console.log to show the extra information instead of adding it to the label, but it just a matter of taste
Upvotes: 2