2

I am looking for a solution to assert a json response in karate dynamically and maintain the data differently for each test case. Below is the example response:

[{
    "id": "Level 1 id",
    "name": "Name 1",
    "description": [{
            "locale": "locale 1",
            "description": "description 1"
        },
        {
            "locale": "locale2",
            "description": "description2"
        }
    ],
    "type": "type1",
    "Value": 100.0,
    "ObjectType": "test123",
    "Object": [{
            "id": "1",
            "groupName": "OBJECT1",
            "filterCriteria": "filter1",
            "TypeMethod": "method1",
            "nGroup": "AND1",
            "criteria": [{
                    "name": "multiple objects but no arrays"
                },
                {
                    "name": "multiple objects but no arrays"
                },
                {
                    "name": "multiple objects but no arrays"
                }
            ]
        },
        {
            "id": "2",
            "groupName": "OBJECT2",
            "filterCriteria": "filter2",
            "TypeMethod": "method2",
            "nGroup": "AND2",
            "criteria": [{
                    "name": "multiple objects here but no arrays"
                },
                {
                    "name": "multiple objects here but no arrays"
                },
                {
                    "name": "multiple objects here but no arrays"
                },
                {
                    "id": "3",
                    "groupName": "OBJECT3",
                    "filterCriteria": "filter3",
                    "TypeMethod": "method3",
                    "nGroup": "AND3",
                    "criteria": [{
                            "name": "multiple objects but no arrays"
                        },
                        {
                            "name": "multiple objects but no arrays"
                        },
                        {
                            "name": "multiple objects but no arrays"
                        }
                    ]
                }
            ]
        }
    ]
}]

I want to assert id, name, description and the Object array whose values change based on different id's. I am aware about schema approach but i need to assert on specific values. I have also tried match feature by keeping json files for each test case but that becomes difficult to maintain in different test environments. Can somebody help me with the best approach to assert and how can the test data be maintained.

haxpor
  • 2,431
  • 3
  • 27
  • 46
Varun Patil
  • 145
  • 7

1 Answers1

1

Don't do it.

This is my advice after observing some teams go wrong with Karate. These teams typically come from a programming or Java background and expect that test-cases also have to display "re-use" and be "DRY".

For an example of what happens when you try to do too-much re-use and combine multiple test-flows into one "path", see this other answer on Stack Overflow: https://stackoverflow.com/a/54126724/143475

That said, you can have one single JSON file and then replace some parts for different scenarios using the set keyword or embedded expressions. Please take some time to read the docs and examples linked. It will answer all your questions. And then ask some specific questions on Stack Overflow instead of a "blanket" question like what you have asked here.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248