-1

In cucumber suppose my one than statement is failed then my all than statement is skipped by cucumber for that scenario and it started executing next scenario ... Do anyone have any way to assist cucumber to run next step without skipping all other than statement for that scenario.. do we have any provision for same?

I am using cucumber, maven with java

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125

4 Answers4

3

This is a bad practice. If you have the need for something like this, it only means that your Cucumber scenario is not written properly.

Having said that, if there is a step that is expected to fail but its failure does not imply a failure of the whole scenario, you will have to implement some sort of "failsafe" workaround within your glue code. For example try...catch clause that will acknowledge the failure, perhaps log it but will not fail the scenario due to thrown exception.

Cucumber steps should not be polluted with internal logic.

Eugene S
  • 6,709
  • 8
  • 57
  • 91
  • Hi Eugene ... My then statement failing due to assert failer not due to any other cause .. what I can do in such case and catch the exception won't help in such case – Shubham Jain Feb 01 '18 at 03:58
  • @ShubhamJain How about actually failing this scenario then? Why would you want to continue testing a failing behavior? On the other hand, if this assert is not critical then remove it. There is no other way. – Eugene S Feb 01 '18 at 04:00
  • Their is some than which is not depending on other than so I like to continue the scanrio .. if I make another scanrio the browser will up again and I need to navigate to that point as an prerequisite which an overload to do for every assert – Shubham Jain Feb 01 '18 at 04:04
  • 1
    @ShubhamJain A failing assert throws an java.lang.AssertionError. If you catch this error cucumber should move on to the next step in the scenario. – Grasshopper Feb 01 '18 at 06:19
  • but that will mark by failed testcase as pass in report :( – Shubham Jain Feb 01 '18 at 06:46
  • moveover After hook will never execute in that case – Shubham Jain Feb 01 '18 at 06:47
  • @ShubhamJain I understand what you are saying about the overload but unfortunately this is something that you will have to cope with. There can be only one single test in each scenario and this is a bad practice to include multiple tests as it is very likely to cause false positives and general confusion. – Eugene S Feb 01 '18 at 07:11
  • Ok Eugene ... Thanks for you and grasshopper efforts .. voting you up ... I kept this thread open for now in case if any solution appear in near future – Shubham Jain Feb 01 '18 at 07:42
2

If a step in a scenario fails, then the entire scenario fails. To do anything else undermines several principles of testing. Once a failure has happened executing the subsequent steps make no sense as we don't have a consistent starting point ( something has already gone wrong)

If you want to run a single scenario and exclude a particular step, just remove it from the scenario.

In this case its up to you to use the tool properly. Cucumber is not going to help you do stupid things with it.

diabolist
  • 3,990
  • 1
  • 11
  • 15
1

You can either handle it using try - - - catch block or you can use soft assertion
Soft Assertions are the type of assertions that do not throw an exception when an assertion fails and would continue with the next step after assert statement.
This is usually used when our test requires multiple assertions to be executed and the user want all of the assertions/codes to be executed before failing/skipping the tests.
AssertJ is library providing fluent assertions. It is very similar to Hamcrest which comes by default with JUnit. Along with all the asserts AssertJ provides soft assertions with its SoftAssertions class inside org.assertj.core.api package

Consider the below example:

public class Sample {
 @Test
 public void test1() {
  SoftAssert sa = new SoftAssert();
  sa.assertTrue(2 < 1);
  System.out.println(“Assertion Failed”);
  sa.assertFalse(1 < 2);
  System.out.println(“Assertion Failed”);
  sa.assertEquals(“Sample”, “Failed”);
  System.out.println(“Assertion Failed”);
 }
}

Output:

Assertion Failed
Assertion Failed
Assertion Failed

PASSED: test1

Even now the test PASSED instead of FAILED. The problem here is the test would not fail when an exception is not thrown. In order to achieve the desired result we need to call the assertAll() method at the end of the test which will collate all the exceptions thrown and fail the test if necessary.

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
SachinB
  • 348
  • 6
  • 18
  • Can you please give an example of soft assertion ... Did you mean using if-else condition – Shubham Jain Feb 01 '18 at 14:42
  • Thats great ... Can you please provide some reference URL of same in your answer ... I will try it for sure once in office currently I m out of office.... Thanks for your valuable comment. – Shubham Jain Feb 01 '18 at 18:07
  • Hi SachinB ... I tried your suggestion .. First of all SoftAssert sa= new SoftAssert() is a class of TestNG inside org.testNg.assert class while I am using Junit with cucumber, cucumber is not as much compatible with TestNG. Another thing org.assertj.core.api lib provided SoftAssertions and yes it is working for me. Thanks :) – Shubham Jain Feb 02 '18 at 08:55
0

Extending the SachinB answer.

We can use assertj to achive same.

We need to use it's lib/dependency as below

    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.9.0</version>
    </dependency>

You need to create object of SoftAssertions() which is provide by assetj

package you need to import

import org.assertj.core.api.SoftAssertions;

Example code

public class myclass {

 SoftAssertions sa = null;

 @Then("^mycucucmberquote$")
 public void testCase2() {
  sa = new SoftAssertions();
  sa.assertThat("a").contains("b");
 }

 @Then("^mycucucmberquoteLastThen of that scario$")
 public void testCase3() {
  try {
   sa.assertAll();
  } catch (Exception e) {

  }
 }
}

sa.assertAll(); implemented function fails and it will provide the stack trace of failed steps.

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125