我理解
@CucumberOptions
用于传递 Cucumber 选项。但由于Java注解只允许内联常量的限制,使用@CucumberOptions
相当麻烦。那么,在使用 cucumber-junit 时是否有一种动态方式来传递 Cucumber 选项呢?非常感谢。
这个问题现在已经很老了,但答案是可以。
如果您使用的是 Maven,只需像这样添加即可。
mvn test -Dcucumber.options="--tags @your_tag"
您可以在运行场景时以这种方式过滤场景。
希望这有帮助。
如here所述,不要依赖 TestNG 和 jUnit,而是尝试使用通用通用代码并尝试根据您的要求创造最佳优势。 根据需要添加更多选项。
private void defaultRun() {
List<String> arguments = new ArrayList<String>();
rguments.add("featureFiles");
String[] tags = tagsToExecute;
for (String tag : tags) {
arguments.add("--tags");
arguments.add(tag);
}
arguments.add("--plugin");
arguments.add(html);
arguments.add("--plugin");
arguments.add(json);
arguments.add("--plugin");
arguments.add(rerun);
String[] gluepackages = gluePackage.split(",");
for (String packages : gluepackages) {
if (!packages.contains("none")) {
arguments.add("--glue");
arguments.add(packages);
}
}
final String[] argv = arguments.toArray(new String[0]);
try {
executetests(argv);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public byte executetests(final String[] argv) throws InterruptedException, IOException {
RuntimeOptions runtimeOptions = new RuntimeOptions(new ArrayList(Arrays.asList(argv)));
MultiLoader resourceLoader = new MultiLoader(this.getClass().getClassLoader());
ResourceLoaderClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, this.getClass().getClassLoader());
Runtime runtime = new Runtime(resourceLoader, classFinder, this.getClass().getClassLoader(), runtimeOptions);
runtime.run();
System.out.println(runtime.exitStatus());
return runtime.exitStatus();
}
扩展 @Pedro Lopez 的答案 - Cucumber 将从几个
不同的地方获取
cucumber.features
和
cucumber.filter.tags
等属性:
Cucumber 将按照优先顺序解析系统属性、环境变量、@CucumberOptions 和 cucumber.properties 文件中的属性。请注意,CLI 参数优先于所有参数。按照您喜欢的方式设置这些属性,它应该会找到它们。例如,如果您碰巧使用 TestNG 作为您的运行程序:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Example Suite">
<parameter name="cucumber.filter.tags" value="@Gherkin and not @Zucchini" />
<test name="Vegetable garden" preserve-order="true">
<parameter name="cucumber.features" value="classpath:com/example/features/vegetable"/>
<parameter name="cucumber.glue" value="com.example.vegetables.glue"/>
<classes>
<class name="com.example.RunCucumberTests"/>
</classes>
</test>
<test name="Herb garden" preserve-order="true">
<parameter name="cucumber.features" value="classpath:com/example/features/herbs"/>
<parameter name="cucumber.glue" value="com.example.herbs.glue"/>
<classes>
<class name="com.example.RunCucumberTests"/>
</classes>
</test>
</suite>
package mypack.cukes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder;
import cucumber.runtime.Runtime;
public class MyRunnerTest {
public String[] tagsToExecute = {"@SmokeUtilizationData, @SmokeSummaryData, @SmokeCompliance"};
public String html = "com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html";
public String gluePackage = "mypack.cukes.stepDefinitions";
public String[] argv = null;
public void defaultRun() {
List<String> arguments = new ArrayList<String>();
arguments.add("src/main/java/mypack/cukes/features");
String[] tags = tagsToExecute;
for (String tag : tags) {
arguments.add("--tags");
arguments.add(tag);
}
arguments.add("--plugin");
arguments.add(html);
arguments.add("--glue");
arguments.add(gluePackage);
argv = arguments.toArray(new String[arguments.size()]);
try {
executeTests(argv);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException ioex) {
ioex.printStackTrace();
}
}
public byte executeTests(final String[] argv) throws InterruptedException, IOException {
RuntimeOptions runtimeOptions = new RuntimeOptions(new ArrayList(Arrays.asList(argv)));
MultiLoader resourceLoader = new MultiLoader(this.getClass().getClassLoader());
ResourceLoaderClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, this.getClass().getClassLoader());
Runtime runtime = new Runtime(resourceLoader, classFinder, this.getClass().getClassLoader(), runtimeOptions);
runtime.run();
System.out.println(runtime.exitStatus());
return runtime.exitStatus();
}
public static void main(String[] args) {
System.out.println("Testing MyRunnerTest");
MyRunnerTest myRun = new MyRunnerTest();
myRun.defaultRun();
}
}
顺便说一句,我的 pom 文件显示:1.2.4
import io.cucumber.tagexpressions.TagExpressionParser;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import io.cucumber.testng.PickleWrapper;
import org.testng.annotations.DataProvider;
import java.util.ArrayList;
import java.util.List;
/**
* This runner is a hybrid runner which can run normally as local runner with Cucumber Options or run with tags variable passed from Pipeline tools or from any other sources
* <br></br>
* <br></br>
* When the tags are provided from Cucumber Options locally it needs to be in Cucumber tags format with 'and', 'or', 'not'.
* <br></br>
* But the pipeline tag variable can be passed as "@tag1, @tag2, @tag3, ..." format too.
* <br></br>
* This runner works with either of them or both combined. Let's say You have passed '@smoke' tag in @CucumberOptions annotation which have 100 scenarios, then additionally you can also pass a runtime tag "@Login" and filter again.
*/
@CucumberOptions( features = "src/test/resources/features/",
glue = { "stepDefinitions", "hooks" },
plugin = { "pretty", "com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:" },
tags = "" ) //Pass tags here as usual
public class CucumberRunnerTest extends AbstractTestNGCucumberTests
{
@DataProvider
@Override
public Object[][] scenarios()
{
Object[][] scenarios = super.scenarios();
String dynamicTags = System.getenv("tags"); //passed from Pipeline
//Or add any other sources you want to get the tags from
if ( dynamicTags != null && !dynamicTags.equals("") )
{
List<Object> filteredScenarios = new ArrayList<>();
for ( int i = 0; i < scenarios.length; i++ )
{
PickleWrapper pickleWrapper = ( PickleWrapper ) scenarios[i][0];
List<String> tags = pickleWrapper.getPickle().getTags();
if ( dynamicTags.contains(", ") )
{
String[] tagsArray = dynamicTags.split(", ");
for ( String tag : tagsArray )
{
if ( tags.contains(tag) )
{
filteredScenarios.add(scenarios[i]);
break;
}
}
}
else
{
if ( TagExpressionParser.parse(dynamicTags).evaluate(tags) ) filteredScenarios.add(scenarios[i]);
}
}
return filteredScenarios.toArray(new Object[0][0]);
}
return scenarios;
}
}
尝试一次。如果有效请点赞,以便更多人可以轻松找到并使用最新代码。