Running Cucumber tests directly from executable jar

I would divide the problem you are thinking of in two parts.

  • Create an executable jar
  • Run Cucumber from your own main method

Creating an executable jar using Maven can be done in different ways. One way of doing it is described here:
http://www.thinkcode.se/blog/2011/03/05/create-an-executable-jar-from-maven

It is a small example that only focuses on executing something from a command line like this:

java -jar executable-example.jar

The example contains all dependencies. They are all bundled in the same jar. No need for any additional jars.

Next step would be to execute Cucumber from a main method. My approach would be to write a main that executes the Cucumber main method used for the command line version of Cucumber. The main method used to run cucumber from a command line lives in the cucumber-java library. You will find it at cucumber.api.cli.Main

Running a main method from another main method is done like this:

public static void main(String[] args) throws Throwable {
    String[] arguments = {"foo", "bar"};
    cucumber.api.cli.Main.main(arguments);
}

where arguments are the command line arguments you always want to execute Cucumber with.

Given these two steps, you should be able to execute Cucumber from your own executable jar wherever you are able to execute a jar at all.

Notice that you are mixing library version for Cucumber in your pom. I would use the latest version of all libraries. Compare cucumber-java, cucumber-testng and cucumber-junit. The latest Cucumber version is 1.2.4. I would use it for all of them.

More information about running Cucumber from a command line can be found here: https://cucumber.io/docs/cucumber/api/#from-the-command-line

Leave a Comment