Get @CucumberOptions tag property using System.getProperty()

旧巷老猫 提交于 2019-11-27 03:53:50

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/30594007/get-cucumberoptions-tag-property-using-system-getproperty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!