How to skip a scenario with Cucumber-JVM at run-time in JAVA

4,032 views
Skip to first unread message

Deena P

unread,
Feb 2, 2018, 3:54:56 AM2/2/18
to Cukes

I need to skip a scenario from running based on a flag which I obtain from the application dynamically in run-time, which tells whether the feature to be tested is enabled. I don't want to throw any exception.

Sample code:

    } else {
status = zapi.getTestExecutionStatusInJIRA(scenario.getName());
if (status.equalsIgnoreCase("pass")) {
isScenarioAlreadyPassed = true;
} else {
isScenarioAlreadyPassed = false;
}
}
} else {
logger.info("Currently JIRA_Integration is disabled, enable it to update test scenario execution status in JIRA");
}

if (isScenarioAlreadyPassed) {
logger.info("Scenario: \"" + scenario.getName() + "\" is already passed in the previous test cycle");
throw new SkipException("\"Scenario: \\\"\" + scenario.getName() + \"\\\" is already passed in the previous test cycle\"");
}

Problem with above approach is failed test still shows up as Error in the test summary. I want these scenario/test to be skipped completely or proceed with next scenario/test.

Björn Rasmusson

unread,
Feb 2, 2018, 12:43:19 PM2/2/18
to Cukes

Use assumeTrue("<message>", false); (http://junit.org/junit4/javadoc/4.12/org/junit/Assume.html#assumeTrue(java.lang.String,%20boolean)), then the scenarios will show up as Skipped in the summary.

Cheers
Björn

Deena P

unread,
Feb 2, 2018, 2:08:47 PM2/2/18
to Cukes
No assumeTrue also showing as error in result summary

Björn Rasmusson

unread,
Feb 3, 2018, 5:44:22 AM2/3/18
to Cukes
Deena P wrote:
No assumeTrue also showing as error in result summary

I added a assumeTrue(false); locally to one of the step definitions of the java-calculator example and got the following summary:
8 Scenarios (1 skipped, 7 passed)
36 Steps (3 skipped, 33 passed)

You must be using a to old version of Cucumber-JVM. The latest Cucumber-JVM release is v2.3.1, AFAIK you have to use v2.0.0 or later to get this summary.

Cheers
Björn
 

Deena P

unread,
Feb 3, 2018, 9:45:13 AM2/3/18
to Cukes
Thanks for the response Björn.

After upgrading cucumber-JVM, cucumber scenario execution status is showing correctly in the terminal. like the one you mentioned.

Skipped step

Skipped step

1 Scenarios (1 skipped)
2 Steps (2 skipped)
0m0.535s

But again the problem, maven build is getting failed due to the org.junit.AssumptionViolatedException

Tests run: 12, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.791 s
[INFO] Finished at: 2018-02-03T20:10:08+05:30

Björn Rasmusson

unread,
Feb 3, 2018, 11:23:37 AM2/3/18
to Cukes
Deena P wrote:
Thanks for the response Björn.

After upgrading cucumber-JVM, cucumber scenario execution status is showing correctly in the terminal. like the one you mentioned.

Skipped step

Skipped step

1 Scenarios (1 skipped)
2 Steps (2 skipped)
0m0.535s

But again the problem, maven build is getting failed due to the org.junit.AssumptionViolatedException

Tests run: 12, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.791 s
[INFO] Finished at: 2018-02-03T20:10:08+05:30

I do not get a maven build failure (using a Junit runner class). maven-surefire does get an assumptionFailed notification, and does correctly mark the test as skipped:

8 Scenarios (1 skipped, 7 passed)
36 Steps (3 skipped, 33 passed)
0m0.601s

org
.junit.AssumptionViolatedException: got: <false>, expected: is <true>
    at org
.junit.Assume.assumeThat(Assume.java:95)
    at org
.junit.Assume.assumeTrue(Assume.java:41)
    at cucumber
.examples.java.calculator.ShoppingStepdefs.the_following_groceries(ShoppingStepdefs.java:22)
    at
✽.the following groceries:(cucumber/examples/java/calculator/shopping.feature:4)

[WARNING] Tests run: 8, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 1.521 s - in cucumber.examples.java.calculator.RunCukesTest
[INFO]
[INFO] Results:
[INFO]
[WARNING] Tests run: 8, Failures: 0, Errors: 0, Skipped: 1
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ java-calculator ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Cucumber-JVM ....................................... SUCCESS [  0.928 s]
[INFO] Cucumber-JVM: Core ................................. SUCCESS [ 24.236 s]
[INFO] Cucumber-JVM: JUnit ................................ SUCCESS [  3.881 s]
[INFO] Cucumber-JVM: Java ................................. SUCCESS [ 20.609 s]
[INFO] Examples ........................................... SUCCESS [  0.052 s]
[INFO] Examples: Java Calculator .......................... SUCCESS [  4.569 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

 

Deena P

unread,
Feb 3, 2018, 11:28:12 AM2/3/18
to Cukes
ok, I was using testNg runner. Let me try with Junit.

Thank you very much!
Reply all
Reply to author
Forward
0 new messages