Cucumber-JVM 4 Parallel execution using cucumber.api.cli.Main class

Introduction

Cucumber-JVM 4 supports parallel execution across threads out of the box. This article describes how to run features directly from Java code inside a main method, from the CLI, JUnit test method or TestNG test method. This makes use of the run() method of the Main class contained in the cucumber.api.cli package. The run() method executes the feature files directly without the use of a runner. The options that need to be passed as a String[] to the run() method are described here.

To run Cucumber in parallel using JUnit4 refer to this article or with TestNG refer to this article.

Source Code

The source code is located here.

Java, JUnit and TestNG Command

The command that triggers the running of the feature files from a Java main(), JUnit or TestNG test method is mentioned below.

String[] params = new String[]{ “–threads”, “4”, “-p”, “timeline:target/cucumber-parallel-report”, “-g”, “stepdef”, “src/test/resources/features/parallel/”};

Main.run(params, Thread.currentThread().getContextClassLoader());

Brief description of the options in the above code –

  • –threads   —  Runs the scenarios in parallel. Value is the number of threads. Default is 1.
  • -p               —  Register a plugin. To register multiple plugins repeat “-p” declaration.
  • -g               —  Path to the step definition code as a Java package structure.
  • -t                — Run scenarios that match the tag expression. (Not shown in this command)
  • [features]  — Path to the feature file. Either directory or a specific file.

The Main.main(params) method can also be used especially in a Java main() method, though it contains a System.exit() at the end.

For a detailed overview of all the available options and format refer here.

This can be used in previous versions of Cucumber other than the ‘–threads’ option which was introduced in Cucumber JVM 4.

CLI Command

To compile and run the command from CLI, cucumber jars version 4.4.0 are used here. Refer to the ‘Cucumber Jars’ section in this article for details about downloading these jars.

Compile the step definition classes from the src/test/java folder at the command line.

javac -cp “.;<path to cucumber jars folder>/*” ./stepdef/*.java

Run the following command from the src/test/java folder to execute the feature files.

java -cp “.;<path to cucumber jars folder>/*” cucumber.api.cli.Main –threads 4 -g stepdef ../resources/features/parallel/

The above commands use the source code of this article. Modify the paths accordingly for another project structure.

Parallel Execution

To achieve parallel execution the “–threads” option needs a value more than 1. This is the count of the number of threads. All the steps of a single scenario will be executed by a single thread. In case of a scenariooutline, each row of the examples table may be executed by different threads.

There are two feature filesscenarios.feature and scenarioutlines.feature. The scenarios.feature file contains two scenarios. Each scenario contains two steps. Similarly scenariooutlines.feature contains a one scenario outline with two steps and two rows in the example table.

The step definition methods print out the thread id along with the details of the step and scenario.

Thread ID 13 runs 'First step' of 'Scenario 1' from 'scenarios'.
Thread ID 11 runs 'First step' of 'First example' from 'scenariooutlines'.
Thread ID 12 runs 'First step' of 'Second example' from 'scenariooutlines'.
Thread ID 11 runs 'Second step' of 'First example' from 'scenariooutlines'.
Thread ID 13 runs 'Second step' of 'Scenario 1' from 'scenarios'.
Thread ID 12 runs 'Second step' of 'Second example' from 'scenariooutlines'.
Thread ID 14 runs 'First step' of 'Scenario 2' from 'scenarios'.
Thread ID 14 runs 'Second step' of 'Scenario 2' from 'scenarios'.

From the console print statements, we can see that the steps of ‘Scenario 1’ from scenarios.feature file is run by thread with id 13. ‘Scenario 2’ from same feature file is run by thread with id 14. The ‘First Example row’ and ‘Second Example row’ from scenariooutlines.feature is run by threads with id 11 and 12 respectively.

TimeLineFormatter

To get a visual representation of the threads in actions, one can use the TimeLineFormatter plugin. Just add the “timeline:target dir” to the “-p” option to generate the report. The timeline format of the threads of the above case is displayed below.

Leave a Reply

Your email address will not be published. Required fields are marked *