从另一个main方法使用Main.run运行Cucumber项目

时间:2017-11-25 13:36:37

标签: java cucumber bdd cucumber-jvm cucumber-java

我是Cucumber的新手并试图解决简单的问题:

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

enter image description here

现在,我想从另一个主要方法运行它。我创建了另一个Java项目,添加了一个带有main方法的类,其下面的代码是默认包。

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选项尝试了很多组合,但信息相同。

EDIT2

从下面的评论中,当其他项目在classpath中时,将软件包添加到glue中,可以正常工作。

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

但是,另一个问题:

我有一些我需要与黄瓜整合的旧项目。所有.class和.java都出现在文件夹中(NO src或bin目录):     C:\工作\ RFT_WS2 \ Cuketest3 ,我在Classpath中有这个目录。我尝试过以下选项但无法理解问题:

-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文件添加到包中,请说"步骤"并且有" C:\ work \ RFT_WS2 \ Cuketest3"在classpath中,选项看起来像

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

感谢。

2 个答案:

答案 0 :(得分:3)

glue选项采用一个路径值,该值反映了要包含在类路径中的胶水类的包。

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

假设以下结构

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

编译课程

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

运行UseCukeFromMain

包含胶水类(project1/bin)的根目录必须位于类路径中。

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
LIB /

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

编译课程

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

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

创建的类文件

CukeSteps.class
project2/bin/UseCukeFromMain.class

运行UseCukeFromMain

使用.将当前目录添加到类路径中。

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
LIB /

the same as in the first example

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

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

编译课程

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

创建的类文件

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)

答案 1 :(得分:2)

要素文件需要绝对路径。 步骤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"});    
        }