简体   繁体   中英

Continue execution even if one step fails in cucumber

I want to continue executing my scenarios even if some fail in between in the cucumber report it should show it as a failure but should not stop the execution.

I tried using soft assertion class and using this

 SoftAssertion sa = new SoftAssertion();
if (response.getStatusCode() == 200) {
                Sytem.out.println("pAASED")
            } else {
                sa.fail("this is the failure");
            }

but suppose I have two scenarios and for the first one the response is not 200 it is going to else block and failing the scenario

java.lang.AssertionError: this is the failure
    at org.junit.Assert.fail(Assert.java:88)

it is not even going for the second scenario. Can someone help how can I achieve the expected result(ie it should continue the execution irrespective of one getting failed)

Here you have two different things.

  1. Error Code As Response
  2. Exception

As per definition of exception from Oracle website :

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

In this way exception is totally different from normal flow of execution. So if you want to proceed even in case of exception you need to use the concept of exception handling . As below:

try {
    response = yourMethodCall();
} catch (Exception e) {
    log.error(e)
}

In above sample code:

  1. Replace yourMethodCall() with actual method call.
  2. Catch proper minimum exception. (In your case it should be AssertionError )
  3. Consider logging of your exception for better debugging, as in the code. For that create log object.

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