运行 Cucumber 项目使用来自另一个主要方法的 Main.run

Running Cucumber project using Main.run from another main method

我是 Cucumber 的新手,正在尝试解决简单的问题:

我创建了一个 Java 项目并将所有与黄瓜相关的 jar 引用到该项目的构建路径(称为 "CukeTest4"),下面是显示 java 文件和特征文件。当我 运行 此功能文件作为 Eclipse 中的 Cucumber 功能时,它 运行 没问题。

现在,我想 运行 从另一个主要方法进行此操作。我创建了另一个 Java 项目,添加了一个带有主要方法的 Class,下面的代码在默认包中。

import cucumber.api.cli.Main;

public class UseCukeFromMain {
    public static void main(String[] args) throws Throwable 
    {
        Main.main(new String[]{"-g", "C:/work/workspaces/neon2_wks_new1/Cuketest4/src/com/cuke", "C:/work/workspaces/neon2_wks_new1/Cuketest4/src/com/cuke/cukefeature.feature"});
    }
}

我已经在 java 文件中提供了该方法的实现,因为它在 Eclipse 中运行良好,但在下面显示了实现该方法的消息

[33mU[0m

1 Scenarios ([33m1 undefined[0m)
1 Steps ([33m1 undefined[0m)
0m0.000s


You can implement missing steps with the snippets below:

@Given("^I want to write a step with precondition$")
public void i_want_to_write_a_step_with_precondition() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}

我已经为 -g 选项 尝试了很多组合,但消息是一样的。

编辑2

根据下面的评论,当另一个项目在 class路径中时将包添加到胶水,工作正常。

Main.main(new String[]{"-g", "com.cuke", "C:/work/workspaces/neon2_wks_new1/Cuketest4/src/com/cuke/cukefeature.feature"};

但是,另一个问题:

我有一些旧项目需要与 Cucumber 集成。所有 .class 和 .java 都存在于文件夹中(没有 src 或 bin 目录): C:\work\RFT_WS2\Cuketest3 ,我在 Class 路径中有这个目录。我尝试了以下选项但无法理解问题:

-g "" path/to/feature //(NOT WORKING)
-g "classpath:" path/to/feature //(NOT WORKING)  
-g "Cuketest3" // Added "C:\work\RFT_WS2" in classpath (NOT WORKING) 

现在,如果我将 .java 文件添加到包中,比如 "steps" 并在 class 路径中包含 "C:\work\RFT_WS2\Cuketest3",选项看起来像

-g "steps" path/to/feature //(WORKING)

我的问题是如何让它找到默认包的方法实现。

还有如何添加多个胶水选项,例如

我试过的案例无效

Main.main(new String[]{"-g", "com.cuke,com.cuke1", "C:/work/workspaces/neon2_wks_new1/Cuketest4/src/com/cuke/cukefeature.feature"};

Main.main(new String[]{"-g", "com.cuke", "com.cuke1", "C:/work/workspaces/neon2_wks_new1/Cuketest4/src/com/cuke/cukefeature.feature"};

谢谢。

glue 选项采用路径值,该路径值反映要包含在 classpath 中的 glue classes 包。

在下面找到一个简化的工作示例

假设结构如下

/tmp/cuke-test/features/cukefeature.feature
/tmp/cuke-test/lib
/tmp/cuke-test/project1/src/com/cuke/CukeSteps.java
/tmp/cuke-test/project2/src/UseCukeFromMain.java

cukefeature.feature

Feature: simple test
  Scenario: test programatic call of Cucumber
  Given we have feature file
  When some glue code exists
  Then those steps should not fail

lib

cucumber-core-2.1.0.jar
cucumber-html-0.2.6.jar
cucumber-java-2.1.0.jar
cucumber-jvm-deps-1.0.6.jar
cucumber-testng-2.1.0.jar
gherkin-5.0.0.jar
jcommander-1.64.jar
snakeyaml-1.17.jar
tag-expressions-1.0.1.jar
testng-6.11.jar

CukeSteps.java

package com.cuke;

import cucumber.api.PendingException;
import cucumber.api.Scenario;
import cucumber.api.java.Before;
import cucumber.api.java.en.*;

public class CukeSteps {
    @Given("^we have feature file$")
    public void we_have_feature_file() throws Throwable {
        System.out.println("execute Given step");
    }

    @When("^some glue code exists$")
    public void some_glue_code_exists() throws Throwable {
        System.out.println("execute Then step");
    }

    @Then("^those steps should not fail$")
    public void those_steps_should_not_fail() throws Throwable {
        throw new PendingException();
    }
}

UseCukeFromMain.java

import cucumber.api.cli.Main;

public class UseCukeFromMain {
    public static void main(String[] args) throws Throwable {
        Main.main(new String[]{
            "--glue",
            "com/cuke", // the package which contains the glue classes
            "/tmp/cuke-test/features/cukefeature.feature"}
        );
    }
}

编译 classes

javac -cp "lib/*" -d project1/bin/ project1/src/com/cuke/*.java
javac -cp "lib/*" -d project2/bin/ project2/src/*.java

运行 UseCukeFromMain

包含胶水 classes (project1/bin) 的根目录必须在 class 路径中。

java -cp "project2/bin:project1/bin:lib/*" UseCukeFromMain

输出

execute Given step
execute Then step

1 Scenarios (1 pending)
3 Steps (1 pending, 2 passed)
0m0.104s

cucumber.api.PendingException: TODO: implement me
    at com.cuke.CukeSteps.those_steps_should_not_fail(CukeSteps.java:21)
    at ✽.those steps should not fail(/tmp/cuke-test/features/cukefeature.feature:6)

编辑 使用默认包中的步骤定义

假设结构如下

features/cukefeature.feature
lib/
project1/src/CukeSteps.java
project2/src/UseCukeFromMain.java

cukefeature.feature
库/

the same as in the first example

CukeSteps.java

// note: there is no package statement

import cucumber.api.PendingException;
import cucumber.api.Scenario;
import cucumber.api.java.Before;
import cucumber.api.java.en.*;

public class CukeSteps {
    @Given("^we have feature file$")
    public void we_have_feature_file() throws Throwable {
        System.out.println("execute Given step");
    }

    @When("^some glue code exists$")
    public void some_glue_code_exists() throws Throwable {
        System.out.println("execute Then step");
    }

    @Then("^those steps should not fail$")
    public void those_steps_should_not_fail() throws Throwable {
        throw new PendingException();
    }
}

UseCukeFromMain.java

import cucumber.api.cli.Main;

public class UseCukeFromMain {
    public static void main(String[] args) throws Throwable {
        Main.main(new String[]{
            "--glue",
            "",  // to used Step definitions in default package
            "features/cukefeature.feature"}
        );
    }
}

编译classes

选项 -d . 在当前目录中创建 class 个文件。

javac -cp "lib/*" -d . project1/src/*.java
javac -cp "lib/*" -d project2/bin/ project2/src/*.java

创建了 class 个文件

CukeSteps.class
project2/bin/UseCukeFromMain.class

运行 UseCukeFromMain

使用 ..

将当前目录添加到 class 路径
java -cp "project2/bin:.:lib/*" UseCukeFromMain

输出

execute Given step - default package
execute Then step - default package

1 Scenarios (1 pending)
3 Steps (1 pending, 2 passed)
0m0.096s

cucumber.api.PendingException: TODO: implement me
    at CukeSteps.those_steps_should_not_fail(CukeSteps.java:19)
    at ✽.those steps should not fail(features/cukefeature.feature:5)

编辑 使用来自不同包的步骤定义。

假设结构如下

features/cukefeature.feature
lib
project1/src/com/cuke1/CukeStepsB.java
project1/src/com/cuke/CukeStepsA.java
project2/src/UseCukeFromMain.java

cukefeature.feature
库/

the same as in the first example

步骤定义分为两个 classes,在不同的包中

CukeStepsA.java

package com.cuke;

import cucumber.api.PendingException;
import cucumber.api.Scenario;
import cucumber.api.java.Before;
import cucumber.api.java.en.*;

public class CukeStepsA {
    @Given("^we have feature file$")
    public void we_have_feature_file() throws Throwable {
        System.out.println("execute Given step - package com.cuke");
    }
}

CukeStepsB.java

package com.cuke1;

import cucumber.api.PendingException;
import cucumber.api.Scenario;
import cucumber.api.java.Before;
import cucumber.api.java.en.*;

public class CukeStepsB {
    @When("^some glue code exists$")
    public void some_glue_code_exists() throws Throwable {
        System.out.println("execute Then step - package com.cuke1");
    }

    @Then("^those steps should not fail$")
    public void those_steps_should_not_fail() throws Throwable {
        throw new PendingException();
    }
}

UseCukeFromMain.java

import cucumber.api.cli.Main;

public class UseCukeFromMain {
    public static void main(String[] args) throws Throwable {
        Main.main(new String[]{
            "--glue",
            "com/cuke",
            "--glue",
            "com/cuke1",
            "features/cukefeature.feature"}
        );
    }
}

编译classes

javac -cp "lib/*" -d project1/bin/ project1/src/com/cuke/*.java project1/src/com/cuke1/*.java
javac -cp "lib/*" -d project2/bin/ project2/src/*.java

创建了 class 个文件

project1/bin/com/cuke1/CukeStepsB.class
project1/bin/com/cuke/CukeStepsA.class
project2/bin/UseCukeFromMain.class

运行 UseCukeFromMain

java -cp "project2/bin:project1/bin:lib/*" UseCukeFromMain

输出

execute Given step - package com.cuke
execute Then step - package com.cuke1

1 Scenarios (1 pending)
3 Steps (1 pending, 2 passed)
0m0.114s

cucumber.api.PendingException: TODO: implement me
    at com.cuke1.CukeStepsB.those_steps_should_not_fail(CukeStepsB.java:16)
    at ✽.those steps should not fail(features/cukefeature.feature:5)

功能文件需要绝对路径。 step def 目录 需要classpath 格式。

   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"});    
        }