gpt4 book ai didi

java - 从另一个主要方法使用 Main.run 运行 Cucumber 项目

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:02:12 26 4
gpt4 key购买 nike

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

我创建了一个 Java 项目并将所有与 cucumber 相关的 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 选项尝试了很多组合,但消息是一样的。

编辑2

从下面的评论来看,当其他项目在类路径中时,将包添加到粘合中,效果很好。

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,我在类路径中有这个目录。我尝试了以下选项但无法理解问题:

-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”,选项看起来像

-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 选项采用一个路径值,该路径值反射(reflect)要包含在类路径中的粘合类的包。

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

假设结构如下

/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

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)

关于java - 从另一个主要方法使用 Main.run 运行 Cucumber 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47486573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com