AfterAll global hook cucumber-jvm

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 08:55:44

问题


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 some posts like this and reviewed this reported issue, I've accomplished it doing something like this:

public class ContextSteps {

   private static boolean initialized = false;

   @cucumber.api.java.Before
   public void setUp() throws Exception {
      if (!initialized) {
         // Init context. Run just once before first scenario starts

         Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
              // End context. Run just once after all scenarios are finished
            }
         });

         initialized = true;
      }
   }
}

I think the context initialization (equivalent to BeforeAll) done in this way is fine. However, although it's working, I'm not sure at all if the AfterAll simulation using Runtime.getRuntime().addShutdownHook() is a good practice.

So, these are my questions:

  • Should I avoid Runtime.getRuntime().addShutdownHook() to implement AfterAll?
  • Are there other better choices to emulate the AfterAll behaviour in cucumber-jvm?

Thanks in advance for the help.


回答1:


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();
  }
};



回答2:


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)  { }
}


来源:https://stackoverflow.com/questions/31563261/afterall-global-hook-cucumber-jvm

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