Karate:How to make data initialization only execute once

693 Views Asked by At

In this feature file, the @setup Scenario will run twice due to there are two other scenarios outline use it Feature:

@setup Scenario:

def data = [{a: 1}, {a: 2}, {a: 3}] Scenario Outline: row

print 'a: ', a

Examples: | karate.setup().data |

Scenario Outline: test row

  • print 'test a: ', a

Examples: | karate.setup().data |

how to make the Scenario with @setup only run once?

I want make the Scenario with @setup only run once.

enter image description here

2

There are 2 best solutions below

2
Peter Thomas On

EDIT: after this question, we decided to add the option to karate.setupOnce() that achieves this in a more elegant way: https://github.com/karatelabs/karate/issues/2210

Here is your solution. You can run it and see for yourself.

Feature:

@setup
Scenario:
* def fun = function(){ karate.log('function executed'); return [{a: 1}, {a: 2}] }
* def data = callonce fun

Scenario Outline:
* print 'first:', __row

Examples:
| karate.setup().data |

Scenario Outline:
* print 'second:', __row
    
Examples:
| karate.setup().data |
1
lovelemon On

in 1.3.1 version Feature:

@setup Scenario:

  • def fun = function(){ karate.log('function executed'); return [{a: 1}, {a: 2}] }
  • def data = callonce fun

Scenario Outline:

  • print 'second:', __row Examples: | karate.setupOnce().data |