Get @CucumberOptions tag property using System.getProperty()
Asked Answered
H

2

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?

Hoffman answered 2/6, 2015 at 10:51 Comment(0)
M
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.

Mendel answered 13/6, 2015 at 13:32 Comment(1)
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: github.com/cucumber/cucumber-jvm/tree/main/…Roma
R
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

Rogovy answered 6/10, 2017 at 5:7 Comment(5)
System.setProperty(KEY, value); What do you expect the Key to be ?Hostage
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 snippetDefant
Hi Guys, In my example I have set KEY as static variable: private static final String KEY = "cucumber.options"; Hope it helps. Regards,Rogovy
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.Dew
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.Rogovy

© 2022 - 2024 — McMap. All rights reserved.