Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run cucumber feature file from java code not from JUnit Runner

I want to run cucumber feature file from java code.

currently we are running form JUnit Runner

package com.compareglobalgroup.testscript;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

    @CucumberOptions(features = { "src/test/resources/feature/BB" }, glue = { "com.compareglobalgroup.stepdefs.BB",
        "com.compareglobalgroup.cucumber.hooks" }, plugin = {
                "json:cucumberreport/json/cucumberreport.json" }, tags = { ""
                        + "@Test" })
    public class TestRunnerBB extends AbstractTestNGCucumberTests {

}

I don't want to use this instead I want to run this using java program because I want to pass tags at run time from command line or jenkins.

like image 913
bugCracker Avatar asked Oct 18 '17 09:10

bugCracker


2 Answers

Call the static main method of Main class in the package cucumber.api.cli that corresponds to running cucumber from the command line.

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

    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/featurefile.feature

        } 

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

BTW - In the your example you are running with a TestNG cucumber runner.

like image 111
Grasshopper Avatar answered Oct 01 '22 00:10

Grasshopper


I would recommend qaf-gherkin where you don't need to use junit or other runner. All you need to specify step provider package and feature file(s) or dir to run. For example:

<test name="Gherkin-QAF-Test">
   <parameter name="step.provider.pkg" value="com.qmetry.qaf.automation.impl.step.qaf" />
   <parameter name="scenario.file.loc" value="resources/features" />
   <classes>
      <class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
   </classes>
</test>

You can walk through qaf-step-by-step-tutorial

like image 25
user861594 Avatar answered Sep 30 '22 22:09

user861594