Pass cucumber options dynamically in cucumber-junit?

我是研究僧i 提交于 2019-12-11 19:57:40

问题


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.


回答1:


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.




回答2:


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

    }



回答3:


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.



来源:https://stackoverflow.com/questions/22774099/pass-cucumber-options-dynamically-in-cucumber-junit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!