AfterAll global hook cucumber-jvm

前端 未结 2 1721
南笙
南笙 2020-12-21 08:47

I\'m using cucumber-jvm in my integration tests and I need to execute some code after all scenarios are finished, just once.

After reading carefully som

相关标签:
2条回答
  • 2020-12-21 08:57

    As of Cucumber 2.4.0, the following class gave me the equivalent of @BeforeClass in Junit.

    You can place this in test/java/cucumber/runtime.

    package cucumber.runtime; //cannot change.  can be under /test/java
    
    import cucumber.runtime.io.ResourceLoader;
    import cucumber.runtime.snippets.FunctionNameGenerator;
    import gherkin.pickles.PickleStep;
    
    import java.util.List;
    
    public class NameThisClassWhatever implements Backend {
        private Glue glue;
        private List<String> gluePaths;
    
        @Override
        public void loadGlue(Glue glue, List<String> gluePaths) {
            this.glue = glue;
            this.gluePaths = gluePaths;
    
            //Any before steps here
        }
    
        @Override
        public void disposeWorld() {
            //any after steps here
        }
    
        @Override
        public void setUnreportedStepExecutor(UnreportedStepExecutor executor) { }
    
        @Override
        public void buildWorld() { }
    
        @Override
        public String getSnippet(PickleStep p, String s, FunctionNameGenerator fng) {
            return null;
        }
    
        public NameThisClassWhatever(ResourceLoader resourceLoader)  { }
    }
    
    0 讨论(0)
  • 2020-12-21 09:00

    Probably a better way would be to use a build tool, like Ant, Maven or Gradle for set-up and tear-down actions, which are part of integration tests.

    When using Maven Fail Safe Plug-in, for setting up integration tests. There is the phase pre-integration-test, which is typically used for setting up the database and launch the web-container. Then the integration-tests are run (phase integration-test). And afterwards the phase post-integration-test is run, for shutting down and closing / removing / cleaning up things.

    INFO In case the Cucumber tests are run through JUnit, the following might also be worth considering

    In case it is simpler, smaller set up stuff, you can have a look at the JUnit @BeforeClass and @AfterClass. Or implement a JUnit @ClassRule, which has it's own before() and after() methods.

    @ClassRule
    public static ExternalResource resource = new ExternalResource() {
      @Override
      protected void before() throws Throwable {
        myServer.connect();
      }
    
      @Override
      protected void after() {
        myServer.disconnect();
      }
    };
    
    0 讨论(0)
提交回复
热议问题