1

Karate has callonce that will call a function or feature only once for all scenerios in a feaure file? Is there a similar feature for reading a json file only once in a feature file before executing all scenarios. Can this be achieved by passing a function to karate.callonce() and that function will then just use read function to read the json file. Kindly answer how can I do this correctly?

I do not want to use another feature file for this. Should be able to pass a function name to the callonce.

I tried karate.callSingle and pass read function to read the json file.

1 Answers1

0

Personally I think reading a JSON file from the file-system is so cheap that you are un-necessary worrying about this.

The only way that I know of is like this:

Feature:

Background:
* def dataFn = function(){ return read('data.json') }
* def data = callonce dataFn

Scenario: one
* print data

Scenario: two
* print data

But you are quite likely to complain here that we are initializing the function dataFn for every Scenario ;) In that case, you may need to look for another framework.

And I personally think calling a re-usable feature (for data set-up) is fine. Programming languages do this kind of re-use all the time.

EDIT: well, I just remembered that this would work:

* def data = callonce read 'data.json'

Explained here: https://github.com/karatelabs/karate#call-vs-read

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    callonce seems to be working like I wanted. his framework is so easy- to use...maybe just need some features that will even make this easier to use. I also saw callonce execute tests faster than just read where it was being called for every scenario. Another note is feature files be made reusable to perform some actions/validations/assertions unlike functions which returns values. This this case javascript/jave code can be used. This is how I want to develop the framework. Taking advantages of all the features a true framework like Karate has. – user20604814 Nov 27 '22 at 00:25
  • @user20604814 regarding "reuse" please read this: https://stackoverflow.com/a/54126724/143475 - and also kindly mark the answer as `accepted`: https://stackoverflow.com/help/someone-answers – Peter Thomas Nov 27 '22 at 03:02