How to invoke the Cucumber runner class from a different main method

白昼怎懂夜的黑 提交于 2019-12-11 01:43:45

问题


I'm new to using Command Line Interface. So I just have a question on how to invoke the runner class of the cucumber using CLI technique.

I have a Java program which contains a main method. When testers pass the argument which is test case, it will fetch the feature file. The java program invoke a custom made API which will fetch the correct feature file.

Next I'll have to invoke the Cucumber runner class to execute the test case. I need to pass this particular feature file as the argument. Two questions, Can we invoke the runner class from a different main method. I did some research and I was not able to find a concrete answer. Two questions,

  1. cucumber.api.cli.Main.main(arguments); So how do i specify the jar location of my runner class.

    `FeatureFileCreation.main("xxxxx"); - API that fetches the right feature file String[] arguments = {"-", ""}; cucumber.api.cli.Main.main(arguments);

    • How do I specify where my jar is located? How can I pass my feature file?`
  2. Should I create a main method in the runner class, something like this? For the sake of using CLI,Since I need to create a runnable jar. I should have a main method in my runner class.

`

@RunWith(Cucumber.class)
@Cucumber.Options(features="C:/Users/IBM_ADMIN/Desktop/CRAutomation/CR Regression1/src/My.feature",glue={"bell.canada.step.definition"})

public class AutomationRunnerAction { 

    public void main(){
    }
}`

Please note that, Getting the right feature file is 1 java API. I will invoking that API from one main method of one java program. The runner class with step definition and methods are a diff java program.


回答1:


Unfortunately the accept answer is not correct. If you look at the source of Main.main() you'll notice that it contains: System.exit(exitstatus) which terminates the system.

The proper way to run the commandline programatically would be to use Main.run() like this:

String [] argv = new String[]{ "-g","","./src/main/java/featureDetails/Testing.feature"};
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
byte exitstatus = Main.run(argv, contextClassLoader);



回答2:


Try this if this works. You do not need any Runner class. Just call the static main method of Main class that corresponds to running cucumber from cli.

    public static void main(String[] args) throws Throwable {

           //Your code to get feature file full path        

            Main.main(new String[]{"-g", "classpath to step definition file", "Full path to feature file"});

          // My stepdefinition is inside java package at cucumber.sample.test
          // My feature file is inside src/test/resources/features/samplethree.feature

        }

For additional parameters like tags or plugin use "-t","@Tags". Important the feature file path has to be the last option.

I am running this for Eclipse with Maven setting up classpath and jar dependencies.



来源:https://stackoverflow.com/questions/43518103/how-to-invoke-the-cucumber-runner-class-from-a-different-main-method

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