15

我正在 Eclipse 中为我的 Cucumber 测试运行一个 Maven 项目。我的测试运行程序类如下所示:

@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 的值必须是常量表达式”。

所以似乎它只需要常量而不是参数化值。有人知道解决这个问题的聪明方法吗?

4

2 回答 2

22

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

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

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

于 2015-06-13T13:32:09.083 回答
1

我正在这样做:-

cucumberOption.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 类)来设置输出报告和功能文件夹。要运行测试用例,只需运行您的主类。

问候,

维克拉姆·帕塔尼亚

于 2017-10-06T05:07:06.853 回答