简体   繁体   中英

All @Before and @After scenario methods are getting called before a scenario execution

I have a feature file with some scenarios in it, and I have step definitions ( .java files) defined for all the scenarios. In each step definition, I have @Before ( beforeScenario ) and @After ( afterScenario ) functions. The problem is that before each scenario gets executed, all the defined @Before and @After methods in all the step definitions are getting called.

Is there a way where I could implement it such that only when a scenario is being executed, the @Before and @After scenario methods in that scenario's step definition file are being executed, not the methods in other step definitions.

It's an default nature of before to run before each scenario but if you want a specific before to execute then do as below with tag You can use with tag name as below:

Feature File:

@First 
Scenario: This is First Scenario
 Given this is the first step
 When this is the second step
 Then this is the third step

Hook/step defination java file:

 @Before("@First")
    public void beforeFirst(){
        System.out.println("This will run only before the First Scenario");
    } 

Above before will run for this scenario only, you can use same tag with other scenario in feature file where you want this before to invoke:

Source and for more details:

https://www.toolsqa.com/cucumber/tagged-hooks-in-cucumber/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM