Execute Cucumber step before/after a specific feature

前端 未结 3 1981
青春惊慌失措
青春惊慌失措 2020-12-17 08:28

I want to specify certain setup and tear down steps for each specific feature file. I\'ve seen hooks that allows code to execute before every scenario, and hooks to execute

相关标签:
3条回答
  • 2020-12-17 09:11

    Do you use cucumber-jvm? I found an article that fits your requirement.

    http://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/

    Basically, do not use JUnit @BeforeClass and @AfterClass for this, as they are unaware of Cucumber Hook Tags. You would want Init and Teardown methods to run for certain scenarios only right?

    0 讨论(0)
  • 2020-12-17 09:21

    Try this :

    In feature file :

    @tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly
    Feature : My new feature ....
    

    In Stepdefinitions.java :

    @Before("@tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly")
    public void testStart() throws Throwable {
    }
    
    @After("@tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly")
    public void testStart() throws Throwable {
    }
    
    0 讨论(0)
  • 2020-12-17 09:25

    It is if you are using junit to run your tests. We use the annotations to create a unit test class and a separate steps class. The standard @Before stuff goes in the steps class, but the @BeforeClass annotation can be used in the main unit test class:

    @RunWith(Cucumber.class)
    @Cucumber.Options(format = {"json", "<the report file"},
        features = {"<the feature file>"},
        strict = false,
        glue = {"<package with steps classes"})
    public class SomeTestIT {
        @BeforeClass
        public static void setUp(){
           ...
        }
    
        @AfterClass
        public static void tearDown(){
           ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题