Pass cucumber options dynamically in cucumber-juni

2019-09-02 13:47发布

I understand that @CucumberOptions is used to pass Cucumber options. However, due to the limitation that Java annotations only allow inline constants, it is quite cumbersome to use @CucumberOptions. So, is there a dynamic way to pass Cucumber options when using cucumber-junit? Thank you very much.

3条回答
地球回转人心会变
2楼-- · 2019-09-02 14:03

This question is quite old now, but the answer is yes you can.

If you are using maven for example just add it like this.

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

You can filter yous scenarios that way when running them.

Hope this helps.

查看更多
够拽才男人
3楼-- · 2019-09-02 14:10

You can have multiple Junit Before and After methods, each one executed by a particular tag you want(@mytag). Within each method you can set up the conditions you want.

查看更多
男人必须洒脱
4楼-- · 2019-09-02 14:17

As mentioned here, Instead of depending on TestNG and jUnit, try to use common generic code and try to create best advantages as per your requirement. Add more options as you required.

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

    }
查看更多
登录 后发表回答