Search code examples
apirestpostmancode-snippetspostman-testcase

Postman How to check Not Equal to Condition in Postman?


I want to validate First Id Value 1 is not equal to second id value 2 in below API/Response. But when I used below Code in Postman Tests Postman not retrieving the Result.

API URL: https://jsonplaceholder.typicode.com/posts

Snippet Used:

var jsonData = JSON.parse(responseBody);tests["First and second Ids should not be same "+jsonData[0].id] != jsonData[1].id;

Response:

[
    {
        "userId": 1,
        "id": 1,
        "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
        "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
    },
    {
        "userId": 1,
        "id": 2,
        "title": "qui est esse",
        "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
    }
]

Postman Should provide the Result as passed as expected is First id value in JSON Response is not Equal to Second Id value in JSON Response.

I can check Equal to condition with above Code Snippet.


Solution

  • The syntax tests[...] is deprecated (still there for compatibility). The new and correct syntax is

    var jsonData = JSON.parse(responseBody);
    
    pm.test("First and second Ids should not be same", () => {
        pm.expect(jsonData[0].id).not.eql(jsonData[1].id);
    })