I am a QA and I am writing Integrations tests.
Here’s the brief code where I test weather or not the properties exists in response
const propertiesToCheck = ["predictionId", "customerPin", "raValue"];
// Loop through properties and test each one
propertiesToCheck.forEach(property => {
pm.test(`Response has ${property}`, function() {
pm.expect(response).to.have.property(property);
});
});
this is a code that chat recommended but it runs 4 for 4 seconds
but the basic one here that I wrote:
pm.test("Prediction ID is returned", () => {
pm.expect(pm.response.json()).to.have.property('predictionId');
pm.expect(pm.response.json().predictionId).to.be.a('number');
})
pm.test("Customer PIN is returned", () => {
pm.expect(pm.response.json()).to.have.property('customerPin');
pm.expect(pm.response.json().customerPin).to.be.a('string');
})
pm.test("RA value is returned", () => {
pm.expect(pm.response.json()).to.have.property('raValue');
pm.expect(pm.response.json().raValue).to.be.a('number');
})
Personally, I would go with the second one - It feels more focussed and those tests are independent verifiable units that are testing something specific. They can be run independently rather than having something looping through the whole reponse.
You could also add Postman variable placeholders for the property names and use a datafile with the runner or in a pipeline etc.
You could abstract that pm.response.json() helper to a variable at the top of the script rather than repeat it each pm.expect().
There are many other things that could be done but it all depends on your context and what is going to provide value to you/your team/your project.
The code the chat recommended (which looks like code I would write) is doing more than the three standalone tests which run quicker.
First of all, its using a forEach loop, and its also using string literals to customize the test case name.
I would expect the array method to take longer to execute, but 4 seconds seems a bit long though. That takes less than 1 second on my machine.
The reason I use arrays with custom test case names is based on the number of tests and code I have to write.
If its only 2-3 tests, then I would just write them out. If I have 10 attributes I want to check, then I would use the array.
The mistake you sometimes see is that you have one pm.test block but with 9-10 assertions checking for various attributes. The problem with this approach is that if any of the assertions fail, it will fail the test immediately and stop executing any more assertions. You might have issues with more than one attribute but won’t necessarily be aware of it at this stage. Using the loop means it runs once for each assertion, therefore you get feedback on all tests\assertions.
As Danny mentioned, you should always parse the response once at the top of the tests tab for you to consume in your assertions.