3

我知道这@CucumberOptions是用来传递 Cucumber 选项的。但是由于Java注解只允许内联常量的限制,使用起来相当麻烦@CucumberOptions。那么,在使用 cucumber-junit 时,是否有一种动态的方式来传递 Cucumber 选项?非常感谢。

4

4 回答 4

5

这个问题现在已经很老了,但答案是肯定的,你可以。

例如,如果您使用 maven,只需像这样添加它。

mvn test -Dcucumber.options="--tags @your_tag"

您可以在运行场景时以这种方式过滤场景。

希望这可以帮助。

于 2015-11-17T23:48:42.187 回答
2

正如这里提到的,不要依赖 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();

    }
于 2016-12-12T11:31:48.677 回答
0

您可以有多个 Junit Before 和 After 方法,每个方法都由您想要的特定标记 (@mytag) 执行。在每种方法中,您都可以设置所需的条件。

于 2018-12-11T05:29:21.830 回答
0

这是一个受 Sugat Mankar 的美丽线索启发的解决方案,但具有完整的代码,并且已修复错误(尽管它为您工作,您必须具有与我一样的 Gherkin 功能文件的包结构和位置):

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 文件说: <cucumber.version>1.2.4</cucumber.version>

于 2020-07-29T03:34:07.403 回答