15

I am running a maven project in Eclipse for my Cucumber tests. My test runner class looks like this:

@RunWith(Cucumber.class)
@CucumberOptions(
        tags = { "@Now" },      
//      tags = { "@Ready" },
//      tags = { "@Draft" },
        features = { "src/test/java/com/myCompany/FaultReporting/Features" },
        glue = { "com.myCompany.myApp.StepDefinitions" }
        )
public class RunnerTest {   
}

Instead of having to hard code the tags into the test runner, I am keen to pass them in using the .command file. (i.e. using System.getProperty("cucumber.tag")

However, I get an error when I add the line of code to the above test runner:

@RunWith(Cucumber.class)
@CucumberOptions(
        tags = { System.getProperty("cucumber.tag") }
//      tags = { "@Now" },      
//      tags = { "@Ready" },
//      tags = { "@Draft" },
        features = { "src/test/java/com/myCompany/FaultReporting/Features" },
        glue = { "com.myCompany.myApp.StepDefinitions" }
        )
public class RunnerTest {   
}

The error I get is: "The value for annotation attribute CucumberOptions.tags must be a constant expression".

So seems it only wants constants rather than a parameterised value. Anyone know a clever way round this?

Charlie S
  • 4,366
  • 6
  • 59
  • 97

2 Answers2

22

You can use the cucumber.options environmental variable to specify the tags at runtime

mvn -D"cucumber.options=--tags @Other,@Now" test

This supercedes the tags already contained in the test code.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Note, if running cucumber with JUnit , following command worked for me `mvn -Dcucumber.filter.tags=@Other, @Now` more such JUnit specific options are documented here: https://github.com/cucumber/cucumber-jvm/tree/main/cucumber-junit-platform-engine#configuration-options – Shrikant Prabhu Nov 07 '22 at 11:37
1

I am doing like this:-

cucmberOption.properties

#cucumber.options=--plugin html:output/cucumber-html-report 
#src/test/resources
cucumber.options.feature =src/test/resources
cucumber.options.report.html=--plugin html:output/cucumber-html-report

Java Class: CreateCucumberOptions.java

Method to load properties file:-

private static void loadPropertiesFile(){
    InputStream input = null;
    try{
        String filename = "cucumberOptions.properties";
        input = CreateCucumberOptions.class.getClassLoader().getResourceAsStream(filename);
        if(input==null){
            LOGGER.error("Sorry, unable to find " + filename);
            return;
        }
        prop.load(input);
    }catch(IOException e){
        e.printStackTrace();
    }finally{
        if(input!=null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

method to get and set CucumberOptions

private String createAndGetCucumberOption(){       
 StringBuilder sb = new StringBuilder();
 String featureFilesPath = 
 prop.getProperty("cucumber.options.feature");
 LOGGER.info(" featureFilesPath: " +featureFilesPath);
 String htmlOutputReport = 
  prop.getProperty("cucumber.options.report.html");
 LOGGER.info(" htmlOutputReport: " +htmlOutputReport);
 sb.append(htmlOutputReport);
 sb.append(" ");
 sb.append(featureFilesPath);
 return sb.toString();
}

private void setOptions(){
   String value = createAndGetCucumberOption();
   LOGGER.info(" Value: " +value);
   System.setProperty(KEY, value);
   }

And main method to run this:-

public static void main(String[] args) {
    CreateCucumberOptions cucumberOptions = new CreateCucumberOptions();
    JUnitCore junitRunner = new JUnitCore();
    loadPropertiesFile();
    cucumberOptions.setOptions();
    junitRunner.run(cucumberTest.runners.RunGwMLCompareTests.class);
 }

And RunGwMLCompareTests.class is my Cucumber class

@

RunWith(Cucumber.class)
@CucumberOptions(
        monochrome = true,
        tags = {"@passed"},
        glue =  "cucumberTest.steps")
public class RunGwMLCompareTests {

    public RunGwMLCompareTests(){

    }
}

So basically now you get set output report and feature folders through properties files and others options like glue definations java class. And to run the test cases just run your main class.

Regards,

Vikram Pathania

Vikram Pathania
  • 873
  • 2
  • 8
  • 15
  • 1
    System.setProperty(KEY, value); What do you expect the Key to be ? – nishantvas May 03 '18 at 05:38
  • private void setOptions(){ String value = createAndGetCucumberOption(); LOGGER.info(" Value: " +value); System.setProperty(KEY, value); } What is "KEY" in System.setProperty(KEY,value); here from above code snippet – Pavan T Oct 09 '18 at 19:06
  • Hi Guys, In my example I have set KEY as static variable: private static final String KEY = "cucumber.options"; Hope it helps. Regards, – Vikram Pathania Oct 09 '18 at 22:56
  • I'm running multiple feature files one after the other and setting cucumber.options in System.properties is always taking the first feature file? I tried clearing the property but still takes the first one. – Mujibur Rahman Nov 09 '18 at 12:09
  • Mujibur Rahman, In cucumber.options only set Feature file folder not specific feature file, and make sure it picks up from there and not from Runner (Cucumber.class) class. – Vikram Pathania Nov 14 '18 at 05:42