What is the cucumber-jvm equivalent of Cucumber.wants_to_quit?

这一生的挚爱 提交于 2019-12-02 19:43:45

问题


I am writing tests using cucumber-jvm and I want the system to stop running tests on the first scenario that fails. I found example code written for Cucumber Ruby that does this via an After hook. I am searching for the correct java class and method to call that will be the equivalent of setting Cucumber.wants_to_quit = true in Ruby.

Here is my example code:

@After
public void quitOnErrors(Scenario scenario) {
    if (scenario.isFailed()) {
                    // Need the correct class/method/property to call here.
        cucumber.api.junit.Cucumber.wants_to_quit = true;   
    }
}    

回答1:


I could not find any way to do it natively with Cucumber-JVM, but you can always do this:

static boolean prevScenarioFailed = false;

@Before
public void setup() throws Exception {
    if (prevScenarioFailed) {
        throw new IllegalStateException("Previous scenario failed!");
    }
    // rest of your setup
}

@After
public void teardown(Scenario scenario) throws Exception {
    prevScenarioFailed = scenario.isFailed();
    // rest of your teardown
}



回答2:


The accepted answer for cucumber-jvm quit on first test failure using

throw new IllegalStateException()

doesn't work in my experience.

Try the cucumber command line switch -y instead.

I didn't find any hard documentation for -y, but it was suggested here and a developer in that conversation committed to implementing it. I've tested it and it works as expected.

I have not found a cucumber-jvm version of Cucumber.wants_to_quit? but perhaps this will cover your use case.




回答3:


Creating cucumber hooks in the step definition file helps to stop the test after a scenario fails. This involves creating @Before and @After methods. Take a look at this example:

@Before
  public void setUp() {
    if (prevScenarioFailed) {
      throw new IllegalStateException("Previous scenario failed!");
    }


  }

  @After()
  public void stopExecutionAfterFailure(Scenario scenario) {
    prevScenarioFailed = scenario.isFailed();
  }


来源:https://stackoverflow.com/questions/15272523/what-is-the-cucumber-jvm-equivalent-of-cucumber-wants-to-quit

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