setUp and tearDown for Scenrio outline (cucumber-jvm)

4.8k views Asked by At

I'm using scenario outline in my code and wanted to execute some code before the start of scenario outline and some code after the scenario outline execution has completed.

I know there are @Before and @After annotations in cucumber but these gets executed before and after every scenario. So if I have a scenario outline and 3 rows of example data then @Before and @After will be executed each of then i.e. total 3 times each.

But I want to execute it only once, @Before scenario outline and @After scenario outline thats it! Is there any way to achieve this in cucumber-jvm?

//@Before (some code that should be executed before scenario outline execution begins)

Scenario outline
....
Examples:
|Header1  | Header2 | ..etc
|Data1    | Data2   |

//@After (some code that should be executed after scenario outline execution ends)
1

There are 1 answers

0
Dude On

As far as i know there is no direct way, but you can tag your scenario and use the before and after hooks for tags as a workaround.

Here is an example:

Feature: ExampleTagging
    @DoBeforeScenarioX
    Scenario outline
    ....
    Examples:
    |Header1  | Header2 | ..etc
    |Data1    | Data2   |

Then you can use this in your Java code:

@Before("@DoBeforeScenarioX")
public void do_this_before_scenarioX() {....}

@After("@DoBeforeScenarioX")
public void do_this_after_scenarioX() {....}

Here are tags described: https://github.com/cucumber/cucumber/wiki/Tags