setUp and tearDown for Scenrio outline (cucumber-jvm)

大兔子大兔子 提交于 2019-12-10 11:36:29

问题


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 scneario. 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:


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



来源:https://stackoverflow.com/questions/26911834/setup-and-teardown-for-scenrio-outline-cucumber-jvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!