我尝试使用 Java 和 Maven 构建我的第一个可执行规范。我创建了一个具有以下结构的简单项目:

specification
|-src
  |-test
    |-java
      |-mypackage
        |-MyFeatureTest.java
    |-resources
      |-MyFeature.feature

在junit测试中 MyFeatureTest.java 我有这个:

import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
public class HomepageTest {
}

现在 https://github.com/cucumber/cucumber-jvm/wiki/IDE-support 说我应该添加以下行:

@Cucumber.Options(paths={"my/super.feature:34"})

我尝试将其修改为

@Cucumber.Options(paths={"src/test/resources/"})

但注释 @Cucumber.Options 根本不可用。我的 pom.xml 有这个依赖关系:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.10</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>info.cukes</groupId>
  <artifactId>cucumber-java</artifactId>
  <version>1.0.0.RC20</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>info.cukes</groupId>
  <artifactId>cucumber-junit</artifactId>
  <version>1.0.0.RC20</version>
  <scope>test</scope>
</dependency>

我错过了什么吗?

更新 我错过了一些东西:黄瓜功能文件必须位于子目录中 src/test/resources/mypackage/. 。否则,junit 测试不会检测到它。

当我将它们放在同一目录中时,我可以运行我的功能测试 src/main/test/, ,所以这对我来说不是一个障碍。但我想了解整个设置。

有帮助吗?

解决方案

看看我的问题 这里:

您可以通过在选项注释中设置功能属性来指定类路径上的位置,例如

@Cucumber.Options(features="src/test/resources")

编辑:

在新版本中代码是

@CucumberOptions(features="src/test/resources")

其他提示

classpath 选项在 Cucumber 文档中并不明显(它也不在 JavaDoc 中),我最终从 CLI 文档, ,其中记录了其他位置选项。请参阅 List configuration options 文档中的部分。如何从多模块 Maven 项目的另一个模块获取功能定义也并不明显。

这就是让我开始(从 IDE 和命令行运行)Maven 多模块项目的原因。

@CucumberOptions(
        features = {"classpath:product"},
        //...
)
public class RunCukesTest extends AbstractTestNGSpringContextTests {

我的功能文件所在的位置

main-project
    sub-module-1
        src/test/java/com/foo/
            RunCukesTest.java
        src/test/resources/product/
            feature_1.feature
            feature_2.feature
    sub-module-2
        ...

我很高兴不看到 src/test/resources 在路径中。请注意,没有前导 / 在路径中。使用类路径不那么脆弱,因为必须明确定义类路径(而不是当前工作目录)。

您可以使用

@CucumberOptions(
    format = "pretty",
    tags = {"~@Ignore"},
    features = "src/test/resources/com/"  //refer to Feature file
)

用于扫描包中的所有功能文件

好吧,我只能把这个放在周一早上......我使用的目录布局不正确,我忘记将黄瓜功能放入与我的包结构匹配的子目录中。

确保您也在中创建了所需的包目录 src/test/resources/!

一旦你使用

import cucumber.api.CucumberOptions;

您需要将以下内容添加到 pom.xml 中,否则“mvn test”将不起作用。而且您只能从 IDE 运行测试。看: https://github.com/cucumber/cucumber-java-sculpture/blob/master/pom.xml

<properties>
    <java.version>1.7</java.version>
    <junit.version>4.12</junit.version>
    <cucumber.version>1.2.2</cucumber.version>
    <maven.compiler.version>3.3</maven.compiler.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <compilerArgument>-Werror</compilerArgument>
            </configuration>
        </plugin>
    </plugins>
</build>

或者你可以构建你的 cucumberoptions 参数,就像 https://cucumber.io/docs/reference/jvm#cli-runner 描述并将其传递给 cucumber.api.cli.Main.run()。这是一个小例子:

String arg = "classpath:MyFeature.feature --dry-run";
String[] args = arg.split(" ");
cucumber.api.cli.Main.run(args, Thread.currentThread().getContextClassLoader());

并在 JUnit 测试中使用它。因此,您不必仅使用其他参数为每个测试类创建单独的类。

通过将功能文件放在 src/测试/java 跑步者和步骤文件的位置或将其放在下面 src/主/java 问题将会得到解决。

别忘了说 谢谢.:)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top