Why is my test always passing

As an example for the below test, I want to achieve that if cat == “Names Check” and then itm == “Name Here” so my test case will pass.
But if it won’t match, it should give Fail. Like as example, if we put cat == “ABCNames Check”, it should fail as it won’t match the response.
But I am always getting the PASS. What should we do? Thanks.

pm.test("Name log Check", function () {
    let jsonData = pm.response.json().data.get
    for (let i = 0; i < jsonData.audits.length; i++) {
        if (pm.response.json().data.get.cat[i].category == "Names Check") {
            if (pm.response.json().data.get.itm[i].item == "Name Here") {                pm.expect((jsonData.email[i]).modifiedBy).to.include("test@123.com");
            }
        }
    }
});

You don’t have any tests in that test block therefore it will always pass.

You need an assertion (pm.expect) in there somewhere.

If you provide an example response (with at least two records), I will show you how to loop through the response properly (Hint, there won’t be an IF statements).

On a side note, you parse your response here…

let jsonData = pm.response.json().data.get

But you keep parsing the response in following lines.

pm.response.json().data.get.cat[i].category == "Names Check"
pm.response.json().data.get.itm[i].item == "Name Here" 

Instead of for example

jsonData.cat[i].category

Thanks Mike. But assertion was there on scrolling it towards right side code snippet.

Moreover, I got the point of using data.get twice, but that is just the sample.

pm.test("Name log Check", function () {
    let jsonData = pm.response.json().data
    for (let i = 0; i < jsonData.audits.length; i++) {       
        f = 0
        if (pm.response.json().cat[i].category == "Names Check") {
            f = 1;
            if (pm.response.json().itm[i].item == "Name Here") {
                pm.expect((jsonData.email[i]).modified).to.include("test@123.com");
            }
        }
        pm.expect(f).is.eql(1);
    }
});

Tried above, but in that if first if condition is failing then test case is failing. But we need to fail on the basis of the second if condition to get the failure.

Do you have an example response?

Pretty sure we can do this without the loop or IF statements.

Just to clarify though…

You have an array called “cat” at the top level of your response.

You also have an array called “itm” (again at the top level of your response).

The category and item arrays appear to be separate objects, so don’t currently appear to be linked. (Or is that just because you have parsed directly to the data element?)

If one of the elements in the category array is called “Names Check”.

If it exists, you want to check the item array (at the same array index) to see if it has a particular name.

If the corresponding item name exists, then you want to test that the email address is a specific email address. (which again appears to be in its own top level array and not linked to the item or category array).

This sounds like one test (with multiple parameters) and I suspect we can remove the IF statements (but the multiple arrays at the top level that don’t currently seem to be linked may make this more difficult).

Really need to see an example response to understand this, and what the ultimate test is here.

Suspect it will be a simple JavaScript find and a case of asserting on the results.

Hi Mike, sorry for confusing a lot. Below is the dummy response-

{
    "data": {
             "dummy": [
                    {
                        "cat": "Testing cat1",
                        "itm": "Test A",
                        "modified": {
                            "email": "test1@123.com",
                        }
                    },
                    {
                        "cat": "Testing cat2",
                        "itm": "Test B",
                        "modified": {
                            "email": "test2@123.com",
                        }
                    },
                    {
                        "cat": "Testing cat1",
                        "itm": "Test C",
                        "modified": {
                            "email": "test3@123.com",
                        }
                    }
                    }
                ]
            }
        }
    }
}

What we cant to achieve is, wanna create different test for “Testing cat1 + Test A” then “Testing cat2 + Test B” then “Testing cat1 + Test C”. and only if the combination of cat + itm matches, then only it should give Pass else, it should fail it or skip it.

So to achieve that, created 2 if conditions under for loop for that array. But as there can’t be else so it is always getting result as PASS even if won’t match the result inside the for loop along with if conditions, it exits from it and make it Pass.

Something like…

const response = pm.response.json()

pm.test(`Testing cat1`, () => {
    let search = response.data.dummy.find(obj => {return obj.cat === 'Testing cat1'})
    // console.log(search);
    pm.expect(search.cat).to.eql("Testing cat1");
    pm.expect(search.itm).to.eql("Test A")
    pm.expect(search.modified.email).to.eql("test1@123.com")
})

All three pm.expects will have to succeed for the test to pass.

You can also define an object for the test data\expected response like following (which would allow you to modify the test data a bit easier). You could even read the information from a collection or environment variable

const response = pm.response.json()

let expectedResult = {
    "category" : "Testing cat1",
    "item" : "Test A",
    "email" :"test1@123.com"
}

pm.test(`Testing cat1`, () => {
    let search = response.data.dummy.find(obj => {return obj.cat === expectedResult.category})
    console.log(search);
    pm.expect(search.cat).to.eql(expectedResult.category);
    pm.expect(search.itm).to.eql(expectedResult.item)
    pm.expect(search.modified.email).to.eql(expectedResult.email)
})

image

Changing the email to “test2@123.com” generates a failure as expected.

image

Sorry for the third post in a row, but this extends the previous code to loop through a set of test data.

Which will allow you to test all three categories in a loop. The third entry is failing as expected as that category is not in the example response you provided. (We might want to change the find function to filter, and also check if there is more than one response returned in the search). This might cause issues if you do have duplicate categories. Find returns the first matching elements, where filter returns all matching elements. This might just be an issue with the example response, but something to consider when you are testing.

It uses a JavaScript concept called string literals to customize the test case name.

const response = pm.response.json()

let expectedResults = [
    {
    "category" : "Testing cat1",
    "item" : "Test A",
    "email" :"test1@123.com"
    },
    {
    "category" : "Testing cat2",
    "item" : "Test B",
    "email" :"test2@123.com"
    },
    {
    "category" : "Testing cat3",
    "item" : "Test C",
    "email" :"test3@123.com"
    }
]

expectedResults.forEach(expectedResult => {
    pm.test(`Testing ${expectedResult.category}`, () => {
        let search = response.data.dummy.find(obj => {return obj.cat === expectedResult.category})
        console.log(search);
        pm.expect(search.cat).to.eql(expectedResult.category);
        pm.expect(search.itm).to.eql(expectedResult.item)
        pm.expect(search.modified.email).to.eql(expectedResult.email)
    });
});

image

1 Like

Thanks Mike for your help… I got your approach, but I think the problem is that the ordering fo the response for cat1+item1, cat2+item2, etc is dynamic and it won’t came at same order. :frowning:

Not quite understanding what you mean by that? What do you mean by Dynamic?

I get a bit queasy when I hear the word dynamic in a test. You need to control your test data, and your response data should be the same every time you run the request\test.

The order shouldn’t matter if you are searching the results, but you need to be careful with the parameters for the search. I don’t know enough about your dataset so I explained the difference between the JavaScript find and filter functions just in case its possible to have duplicate categories. (It may or may not be an issue that you need to cater for in your tests).

There is an assumption here, that you know the item, category and email that you want to test (and that all three elements are in the same object like your example response). Which in theory should be fairly straight forward to search using find or filter.

If the category and item that you want to match are in the same object, then you can extend the JavaScript find function to include both elements at the same time.

For example.

const response = pm.response.json()

pm.test(`Testing cat1`, () => {
    let search = response.data.dummy.find(function (obj) {
        return obj.cat === 'Testing cat1' && obj.itm === 'Test A';
    });
    console.log(search);
    pm.expect(search).to.not.be.undefined
    pm.expect(search.modified.email).to.eql('test1@123.com');
});

There is no real point in testing the item and category, you just need to check that the search is not undefined.

You could probably added the expected email into the parameters for the search and then you only need to check if its undefined.

However, I can’t the result being any different to the code I posted earlier. It’s basically another way of doing the same thing. Plus this will be only testing one record in the response. You have three in your example. What about those. Do they need testing?

I guess I’m not fully understanding the logic what you want to test.

Let’s break it down into expected vs actual results.

What is your expected result.

Is is to test that the email for (category1 = x and item1 = y) matches z.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.