1

I'm working with a lot of API data and my plan is to do schema validation using Karate. Because I have many items which share some properties I would like to create JSON files and "call" them in the principal JSON file where I have the whole schema.

I understand I could call each json in the feature file, but I would like to know if there is any way I can put all the schemas together, like a puzzle, from multiple json files in a single json file and call just one in the feature file.

Thanks! P.S Please save my ass!

1 Answers1

0

Take a look at this example: https://github.com/ptrthomas/karate-test/tree/main/src/test/java/examples/reuse

So you can "compose" multiple JSON files in a re-usable feature file like this:

@ignore
Feature:

Scenario:
* def first = read('first.json')
* def second = read('second.json')
* def schema = { first: '#(first)', second: '#[] second' }

And then when you want to use this to match, note how the call is done in "shared" scope to keep things simple:

* call read('common.feature')
* def response = { first: { a: 1 }, second: [{ b: 'x' }] }
* match response == schema
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • This would work if I would want to concatenate json so to speak if I understand correctly. However, what I am trying to achive is sticking json files in nested objects. For example: `{"aaa": "#string", "bbb": "#string", "ccc": {"ccc1": { *second json file }, "ccc2":{ *second json file } } }` – Corina-Elena Aftodor Nov 10 '22 at 13:02
  • @Corina-ElenaAftodor as far as I know, I provided an example where you stick JSON nested into other JSON, so please look at the linked source code. if that does not work, you can assume karate is not the right fit for you :| - to be specific `first` is actually another JSON, try copying the example and using `print` to see what `schema` is – Peter Thomas Nov 10 '22 at 13:09
  • 1
    thanks for your answer. I'm not really experienced and I think it takes me more time to understand things. – Corina-Elena Aftodor Nov 10 '22 at 14:59
  • @Corina-ElenaAftodor in that case may I give you a suggestion. start by writing simple tests that do one assertion on the response. don't worry about re-using anything right now. once you get comfortable with JSON, then you can attempt re-use. I personally consider re-use a "bad thing" in tests: https://stackoverflow.com/a/54126724/143475 - and see if this video helps, I try to explain JSON in a simple way at 18:20 https://youtu.be/yu3uupBZyxc?t=1102 – Peter Thomas Nov 10 '22 at 17:35