1

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
  • use the once keyword in the Scenario Outline. This keyword can be used to ensure that a particular set of steps is only executed once in a given test run Commented Dec 6, 2022 at 9:33
  • How to use the once keyword? Commented Dec 6, 2022 at 9:39

2 Answers 2

1

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 |
Sign up to request clarification or add additional context in comments.

1 Comment

should karate.setupOnce() be used only within "Examples" section?
0

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 |

2 Comments

the @setup still run twice as the picture said.
@lovelemon you are right, we can improve this: github.com/karatelabs/karate/issues/2210

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.