eclipse 使用 System.getProperty() 获取 @CucumberOptions 标签属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30594007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get @CucumberOptions tag property using System.getProperty()
提问by Charlie Seligman
I am running a maven project in Eclipse for my Cucumber tests. My test runner class looks like this:
我正在 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 {
}
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")
我不必将标签硬编码到测试运行器中,而是热衷于使用 .command 文件传递它们。(即使用 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".
我得到的错误是:“注释属性 CucumberOptions.tags 的值必须是一个常量表达式”。
So seems it only wants constants rather than a parameterised value. Anyone know a clever way round this?
所以似乎它只想要常量而不是参数化值。有人知道解决这个问题的聪明方法吗?
回答by Reimeus
You can use the cucumber.options
environmental variable to specify the tags at runtime
您可以使用cucumber.options
环境变量在运行时指定标签
mvn -D"cucumber.options=--tags @Other,@Now" test
This supercedes the tags already contained in the test code.
这取代了测试代码中已经包含的标签。
回答by Vikram Pathania
I am doing like this:-
我这样做:-
cucmberOption.properties
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 Class: CreateCucumberOptions.java
Java 类: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
获取和设置 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
而 RunGwMLCompareTests.class 是我的 Cucumber 类
@
@
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.
所以基本上现在您可以通过属性文件和其他选项(如胶水定义 java 类)获得设置的输出报告和功能文件夹。要运行测试用例,只需运行您的主类。
Regards,
问候,
Vikram Pathania
维克拉姆·帕塔尼亚