4

I would like to package my acceptance tests into an executable JAR where all necessary libraries are included for running the tests and generating the reports. I would also like to either run all tests or a single test.

So far, I'm able to run all tests and although reports are being generated in the location I specified in serenity.properties, the index.html is NOT being generated.

Normally, I would run my tests using maven verify goal which would run the serenity-maven-plugin, but since I'm running from a JAR, I'm not sure how I can achieve the same thing.

I have a main class that looks as follows:

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(features = "classpath:com/cfhayes/test/LookupADefinition.feature")
public class DefinitionTestSuite {
  public static void main(String[] args) throws Exception {
    JUnitCore.main("com.cfhayes.test.DefinitionTestSuite");
  }
}

And my feature file uses tags so that I can specify a single scenario to run:

Feature: Lookup a definition

  @TEST-0001
  Scenario: Looking up the definition of 'apple'
    Given the user is on the Wikionary home page
    When the user looks up the definition of the word 'apple'
    Then they should see the definition 'A common, round fruit produced by the tree Malus domestica, cultivated in temperate climates.'

  @TEST-0002
  Scenario: Looking up the definition of 'pear'
    Given the user is on the Wikionary home page
    When the user looks up the definition of the word 'pear'
    Then they should see the definition 'An edible fruit produced by the pear tree, similar to an apple but elongated towards the stem.'

I'm hoping there is a way I can use JVM args with the executable JAR that would somehow allow me to set the cucumber options. I tried doing something like this:

java -jar my-test-jar.jar -Dcucumber.options="--tags @TEST-0001" 

...but that still runs ALL tests.

Any ideas would be greatly appreciated.

cfhayes
  • 41
  • 1
  • 4
  • Exactly what I need right now. Did you come up with any solution? – yuva Jul 25 '17 at 19:37
  • 1
    Yes, the issue I had was simply the order of the command line arguments. Here's the command line I had to use: java -Dcucumber.options="--tags @TEST-0001" -jar my-test-jar.jar No other changes were needed. – cfhayes Jul 27 '17 at 03:42
  • 1
    Here's a working example you can take a look at: https://github.com/ch88251/cucumber-serenity-example – cfhayes Jul 27 '17 at 05:44
  • Thank you so much, works like a charm. – yuva Jul 27 '17 at 16:33

2 Answers2

5

The way you construct the command might be incorrect. The cucumber options are taken as the arguments to com.cfhayes.test.DefinitionTestSuite::main rather than the java option. Try this:

java -Dcucumber.options="--tags @TEST-0001" -jar my-test-jar.jar

or handle the cucumber options in your class instead.

Mykola Gurov
  • 8,517
  • 4
  • 29
  • 27
0
import io.cucumber.core.cli.Main;
public static void main(String args[]) throws Throwable {
try {
Main.main(new String[] { 


"-g","com.sadakar.cucumber.common",
"-g","com.sadakar.cucumber.runner",
            
"classpath:features", 

"-t","@SmokeTest",

        
"-p", "pretty", 
"-p", "json:target/cucumber-reports/cucumber.json", 
"-p", "html:target/cucumber-reports/cucumberreport.html",

"-m"
});} catch (Exception e) {
e.printStackTrace();
System.out.println("Main method exception : " + e);}}

The above code worked for me to execute the cucumber tests from runnable jar with test frame work as TestNG.

Executing jar: java -jar ProductsAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar

sadakar
  • 28
  • 9