If you have 100 titles, then the best way is the forEach loop to test each title individually.
There are potential ways to test all titles in a single test, but when it fails it will be nearly impossible to tell which title failed (specially if more than one title is missing).
If you get used to console logging your variables, hopefully this should shed light on your current problem, but yes you need to define a variable for title.
testData will be an array, so you need that forEach loop.
It’s within that loop you need to define the title variable.
This should loop though your test data array, and then confirm that the title exists in the response.
const response = pm.response.json();
let testData = pm.iterationData.get("testData");
testData.forEach(obj => {
let title = obj.title
pm.test(`${title} exists`, () => {
let search = response.items.find(obj => obj.title === title);
pm.expect(search, 'title search failed').to.not.be.undefined
pm.expect("title" in search).to.be.true
pm.expect(search.title).to.not.be.undefined
pm.expect(search).to.have.property('title', title);
})
});
I’ve included a few assertions for consideration. Checking that the search is not undefined first with a custom error message appears to be an appropriate method. I changed one of the titles in the test data to something that isn’t returned in response, so you can see how it looks.
fantastic, works excellent. However it must match exactly same title, is there a way to use ‘contains’ instead of equal?
as in my response I have e.g. Title1 2023, and I just need to make sure that ‘Title1’ value exists - if there is something extra like year, it can be ignored.
ah I noticed also that in my JSON test data titles, when I was converting CSV to JSON contained comma are ignored, I have to find a way to escape them