使用System.getProperty()

时间:2015-06-02 10:51:39

标签: java eclipse properties cucumber-jvm test-runner

我在Eclipse中运行maven项目进行我的Cucumber测试。我的测试运行器类看起来像这样:

@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 {   
}

我不想将标签硬编码到测试运行器中,而是希望使用.command文件传递它们。 (即使用System.getProperty(“cucumber.tag”)

但是,当我将代码行添加到上面的测试运行器时,我收到错误:

@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 {   
}

我得到的错误是: “注释属性CucumberOptions.tags的值必须是常量表达式。”

所以看起来它只需要常量而不是参数化值。有人知道一个聪明的方法吗?

2 个答案:

答案 0 :(得分:16)

您可以使用cucumber.options环境变量在运行时指定标记

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

这取代了测试代码中已包含的标签。

答案 1 :(得分:1)

我这样做: -

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类:CreateCucumberOptions.java

加载属性文件的方法: -

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();
            }
        }
    }
}

获取和设置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);
   }

运行此方法的主要方法: -

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

RunGwMLCompareTests.class是我的Cucumber类

@

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

    public RunGwMLCompareTests(){

    }
}

所以基本上现在你可以通过属性文件和其他选项来设置输出报告和功能文件夹,例如胶水定义java类。运行测试用例只需运行主类。

问候,

Vikram Pathania