How to Force a Cucumber Scenario to Fail

how to force a cucumber scenario to fail?

You can get the after hook to fail using your normal assertions. Have not done much with Capybara/rspec exceptions, but I think you can do:

page.should have_selector?(:dialog_message, 1, :text => 'Error')

However, if you do this or do the scenario.fail!(), you will still not logout. You need to wrap it in a begin-ensure block.

Try this:

After do |scenario|
begin
page.should have_selector?(:dialog_message, 1, :text => 'Error')
ensure
logout
end
end

Update

If you do not want to call the standard assertions and directly fail the scenario, you can do the following - you need to use fail instead of fail!:

After() do |scenario|  
begin
#Checking for Error popups
if page.has_selector?(:dialog_message, 1, :text => 'Error')
fail(ArgumentError.new('Unexpected Error dialog!'))
#Or you can just do fail('Unexpected Error dialog') if you do not care about the type.
end
ensure
logout
end
end

Is it possible to make cucumber test fail?

Do you use JUnit as your test framework? If so then use

fail("Reason of fail")

This is a static method from class Assert
http://junit.sourceforge.net/javadoc/org/junit/Assert.html

mvn test doesn't fail when Cucumber scenario fails

After further Googling, I finally figured out the problem was due to having both JUnit and TestNG as dependencies in my project while using the JUnit Cucumber runner. Since I couldn't remove the JUnit and TestNG dependencies as they were inherited from a parent POM, I changed the Cucumber runner to use TestNG instead and it worked.


I later found another, simpler solution: https://stackoverflow.com/a/19928639/4179032.

How to skip all cucumber-jvm scenarios when first scenario is failed

You can make use of Assume.assumeTrue(false) to skip tests. But this will require some changes in test runner and code changes.

  1. Create a separate runner for the scenario which checks the environment details are all working. Let's call it RunFirstTest.java and give tags a value of @Smoke. Other option values default to what you have.
@RunWith(Cucumber.class)
@CucumberOptions(plugin={ }, tags={"@Smoke"}, glue=" ", features=" ")
public class RunFirstTest {

  1. Add the @Smoke tag to the scenario in the feature file that checks the environment etc. Optionally you could look at splitting the feature files.

  2. Create a new class to hold a static flag. It is a simple implementation you might look at making this more robust.

public class SkipFlag {   
public static boolean skipFlag = false; }

  1. Create an After hook with the value option set to @Smoke. Thus it will run only for the smoke scenario.
@After(value={"@Smoke"})  
public void afterSkip(Scenario scen) {
if(scen.isFailed())
SkipFlag.skipFlag = true;
}

  1. Create a second runner for the main tests. Let's call it RunMainTest.java and give its tags value of @MainTests. Other option values default to what you have.

@RunWith(Cucumber.class) @CucumberOptions(plugin={" "},
tags={"@MainTests"}, glue=" ", features=" ") public class RunMainTest
{

@BeforeClass
public static void before() {
if(SkipFlag.skipFlag)
Assume.assumeTrue(false);
}
}


  1. Add the @MainTests tag to the other scenarios in the feature file. Optionally you could look at splitting the feature files and give the name of the feature file in the features option value.

  2. Run this by using maven failsafe plugin. In the configuration of this plugin add the inclusion of these 2 runners in the pom.xml.

 <configuration>
<includes>
<include>RunFirstTest</include>
<include>RunMainTest</include>
</includes>
<runOrder>alphabetical</runOrder>
</configuration>

The includes part might be optional if you only have 2 runners. The most important point is that the RunFirstTest should be the first to run, so alphabetically should be first.


  1. Run it with maven.

Hope it works.



Related Topics



Leave a reply



Submit