Warm tip: This article is reproduced from stackoverflow.com, please click
cucumber cucumber-java java maven cucumber-jvm

How run cucumber using jar?

发布于 2020-04-20 10:41:09

I have a project with cucumber and maven. I can run and build my project successfully from Intellij IDEA. And I can run project from the command line using: mvn clean test -Dcucumber.options="src/test/resources/features --tags @TEST2"

Now I need to this project from the command line in another machine that does not have IDEA or cucumber installed. I have an idea that I need to create a jar-file and run exactly it from command line.

CucumberTest.java itself:

@RunWith(Cucumber.class)
@CucumberOptions(
        monochrome = true,
        glue = {"ru.ab.cd.stepDefs", "ru.abcd.tag.stepdefs"},
        features = {"src/test/resources/features/"},
        tags = {"@TEST1"},
        plugin = {"pretty", "html:target/cucumber-html-report"}
)

public class CucumberTest {
}

TestRunner.java, that runs CucumberTest.java. I made this class specifically for being able to run from the command line. I wanted to pass the value for arguments from the command line. But I still don't understand what values should be passed and try to find out by passing testArguments.

public class TestRunner{

    public static void main(String[] args) throws Throwable {
        String[] testArguments = {"/BitBucketProjects/abc/src/test/resources/features/smoke/TEST2.feature"};
        cucumber.api.cli.Main.main(testArguments);
    }
}

Result when I run TestRunner.java. The test itself did not start. All steps are defined, if I run the same test through the CucumberTest.java, everything is successful.

UUUUUUU

1 Scenarios (1 undefined)
7 Steps (7 undefined)
0m0,014s


You can implement missing steps with the snippets below:

@Допустим("^пользователь переходит на страницу авторизации$")
public void пользователь_переходит_на_страницу_авторизации() {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Допустим("^пользователь находится на странице \"([^\"]*)\"$")
public void пользователь_находится_на_странице(String arg1) {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Допустим("^пользователь загружает тестовые данные из json-файла$")
public void пользователь_загружает_тестовые_данные_из_json_файла() {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Допустим("^пользователь авторизуется с ролью \"([^\"]*)\"$")
public void пользователь_авторизуется_с_ролью(String arg1) {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Допустим("^ПРОВЕРКА\\. Ссылка \"([^\"]*)\" отображается на текущей странице$")
public void проверка_Ссылка_отображается_на_текущей_странице(String arg1) {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Допустим("^ПРОВЕРКА\\. Таблица \"([^\"]*)\" отображается на текущей странице$")
public void проверка_Таблица_отображается_на_текущей_странице(String arg1) {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}


Process finished with exit code 0

Help!

  1. What parameters should I pass to A in order for the test to run?
  2. I need to use tags as parameters. How to do it?
Questioner
NeverSleeps
Viewed
36
NeverSleeps 2020-02-05 22:41

The error said that cucumber does not see classes with step definition. Changed the class and the error disappeared

public class RunnerTest {
    private static String[] defaultOptions = {
            "--glue", "ru.ab.cd.stepDefs",
            "--glue", "ru.abcd.tag.stepdefs",
            "--tags", "@TEST2",
            "src/test/resources/features/"
    };

    public static void main(String[] args) throws Throwable {
        Stream<String> cucumberOptions = Stream.concat(Stream.of(defaultOptions), Stream.of(args));
        cucumber.api.cli.Main.main(cucumberOptions.toArray(String[]::new));
    }
}
  • "--glue" - a package that contains classes with the implementation of steps and hooks
  • "--tags" - a filter for running tests by tags. This line can be deleted and the value passed through the console: --tags @TEST2

  • "src/test/resources/features/" - the last line that has no prefix like "--keyword" - is the path to the folder with .feature files. The framework will search for files in this and in all child folders.