Run Cucumber7 with JUnit5 Tests from Maven Command Line

HOME

The previous tutorial will have explained to run Cucumber tests with JUnit4 or TestNG from Command-Line. Cucumber7 with JUnit5 has a lot of new configuration options. This tutorial will cover all the possible options.

To setup a framework with Cusumber7 and JUnit5, please refer to this tutorial – Integration of Cucumber7 with Selenium and JUnit5.

Below is the sample CucumberRunnerTests class for JUnit5.

import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("com/example")
@SelectClasspathResource("/features")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example")

public class CucumberRunnerTests  {

}

Run Test from Command Line

1. Open the command prompt and change the directory to the project location where pom.xml is present.

cd C:\Users\Vibha\eclipse-workspace_personnel\Cucumber7JUnit5_Demo

2. All feature files should be in src/test/resources and create the Cucumber Runner class as CucumberRunnerTest.
Note:- The Runner class name should end with Test to execute the tests from Command Line
Run the following command in the command prompt:

mvn clean test

mvn clean test runs Cucumber Features using Cucumber’s JUnit Runner.

3. The below screenshot shows the build success output.

Overriding Cucumber Options

Cucumber provides several options that can be passed to on the command line.

1. Running Scenarios using Tags from Command Line

If you are using Maven and want to run a subset of scenarios tagged with @ValidCredentials.

mvn clean test -Dcucumber.filter.tags="@ValidCredentials"

2. Running a Feature file from Command Line

Suppose you want to run a single Feature File from the command line, then use the below syntax

mvn clean test -Dcucumber.features=src/test/resources/features/LoginPage.feature

3. Passing plugin from Command Line

If we want to pass a plugin, please use the below-specified command:

mvn clean test -Dcucumber.plugin=html:target/cucumber-reports/cucumberReport.html

You can see that the cucumberReport.html is generated by the plugin.

4. Passing multiple Parameter from Command Line

If we want to pass more than one parameter, then we can use the following command

mvn clean test -Dcucumber.features=src/test/resources/features/LoginPage.feature -Dcucumber.filter.tags="@ValidCredentials"

You can see that only 1 test is executed and rest 4 tests are skipped out of total 5 tests as shown in the Report.

5. Running a Scenario without a tag from Command Line

If we want to run a single Scenario from the command line and no tag is assigned to that scenario, this is how we specify

mvn clean test -Dcucumber.features=src/test/resources/features/LoginPage.feature:11

6. Ignoring a subset of scenarios

If we do not want to run any Scenario from the command line, this is how we specify

mvn clean test -Dcucumber.filter.tags="not @ValidCredentials"

There is a total of 5 tests, but only 4 will be executed and 1 will be skipped. The output of the above program is shown below:

7. Pass glue code through command line

If we want to pass glue code from the command line, this is how we specify

mvn clean test -Dcucumber.glue=com.example

8. Pass dry run value through command line

dry-run option can either be set as true or false. If it is set as true, it means that Cucumber will only check that every step mentioned in the Feature File has corresponding code written in the Step Definition file or not. By default, dry-run is False.

mvn clean test -Dcucumber.execution.dry-run=true

This image shows the steps in the feature file that does not have step definitions.

The cucumber report shows that out of 2 tests, 1 is executed and another one is undefined.

9. Pass snippet type value through command line

The default option for snippets is UNDERSCORE. This settings can be used to specify the way code snippets will be created by Cucumber.

mvn clean test -Dcucumber.snippet-type=camelcase

You can see that the code snippet is in camelCase. In the previous example, it underscored.

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Advertisement

One thought on “Run Cucumber7 with JUnit5 Tests from Maven Command Line

  1. Thank you so much! I have lots of experience with the Ruby version of Cucumber. It comes with a “cucumber” executable, which has lots of options for running tests from the command line (like specifying the tags to use, whether or not to do a dry run, what kind of output to generate, and if I want to specify a file, which contains a list of tests to run), but with the Java version, everywhere I look, all I see is examples of how to create a custom Java class where I can hardcode all of these parameters. I just wanted to specify them from the command line, so that I can change them on the fly. This is EXACTLY the sort of information I was looking for!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s