Cucumber-JVM: Running a @Before step only for certain scenarios

You can make use Cucumber’s @Before and @After to execute only for certain scenarios like the below. This is especially useful if you, say, wanted to make a (Mongo) DB connection for a few test scenarios.


@Before("@mongo")
public void beforeScenario() {
  // actions
}

@After("@mongo")
public void afterScenario() {
  // actions
}

To complete the hook, you simply add “@mongo” tag to the scenarios in your feature file. Cheers!

Maven: How to only execute certain Cucumber tags

I needed Maven to only run certain tests that had a specific tag (i.e., @regression).  I couldn’t easily find information via the Cucumber website, so I’m logging it her for archiving.  Note: This command also works in Jenkins via the “Goals and options” section.

clean install -Dcucumber.options="--tags @regression"