Java 如何使用 Cucumber-junit 设置 Cucumber 特征的路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9663117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:48:17  来源:igfitidea点击:

How do I set the path to my Cucumber features using cucumber-junit?

javacucumbercucumber-jvmcucumber-junit

提问by cringe

I try to build my first executable specifications with Java and Maven. I created a simple project with this structure:

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

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

In the junit test MyFeatureTest.javaI have this:

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

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

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

Now https://github.com/cucumber/cucumber-jvm/wiki/IDE-supportsays that I should add the following line:

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

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

I tried to modify that to

我试图将其修改为

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

but the annotation @Cucumber.Optionsisn't available at all. My pom.xmlhas this dependencies:

但注释@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>

Am I missing something?

我错过了什么吗?

UpdateI was missing something: The cucumber feature file has to be in a subdirectory src/test/resources/mypackage/. Otherwise it won't be picked up by the junit test.

更新我遗漏了一些东西:黄瓜特征文件必须在子目录中src/test/resources/mypackage/。否则它不会被junit测试选中。

I can run my feature tests when I put them in the same directory src/main/test/, so it's not a blocker for me. But I'd like to understand the whole setup.

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

采纳答案by lanoxx

Take a look at my question here:

这里看看我的问题:

You can specify a location on the classpath by setting the feature attribute in the options annotation like

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

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

Edit:

编辑:

in new versions code is

在新版本中,代码是

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

回答by cringe

Ok, I can only put this on monday morning... The directory layout I used wasn't correct, I forgot to put the cucumber features into subdirectories matching my package structure.

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

Make sure you create the needed package directories also in src/test/resources/!

确保您也在src/test/resources/!

回答by Jewel

You can use

您可以使用

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

for scan all of feature file in package

用于扫描包中的所有特征文件

回答by Robert Altena

Once you use

一旦你使用

import cucumber.api.CucumberOptions;

You will need to add the following to your pom.xml, otherwise "mvn test" will not work. And you can only run your tests from the IDE. See: https://github.com/cucumber/cucumber-java-skeleton/blob/master/pom.xml

您需要将以下内容添加到 pom.xml 中,否则“mvn test”将不起作用。而且您只能从 IDE 运行您的测试。参见:https: //github.com/cucumber/cucumber-java-skeleton/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>

回答by user24502

Or you could just build your cucumberoptions arguments, like in https://cucumber.io/docs/reference/jvm#cli-runnerdescribed and pass it to cucumber.api.cli.Main.run(). This is a small example:

或者你可以只构建你的黄瓜选项参数,就像在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());

and use it in you JUnit Test. So you don't have to create a seperate class for each testclass just with other arguments.

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

回答by markdsievers

The classpathoption is not obvious in the Cucumber documentation (it is not in the JavaDoc either), I ended up inferring it from the CLI documentation(edit: link is dead, can't find an equivalent), which has other location options documented. See the List configuration optionssection in the docs. It is also not obvious how to get the feature defintions from another module of a multi module Maven projectg.

classpath选项在 Cucumber 文档中并不明显(它也不在 JavaDoc 中),我最终从CLI 文档中推断出它(编辑:链接已死,找不到等效项),其中记录了其他位置选项。请参阅List configuration options文档中的 部分。如何从多模块 Maven 项目的另一个模块获取功能定义也不是很明显。

This is what got me going (running from IDE and command line) in a Maven multi module project.

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

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

where my feature files were located in

我的功能文件所在的位置

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
        ...

It pleases me not to see src/test/resourcesin the path. Note that there is no leading /in the path. Using the classpath is less brittle, because the classpath (rather than the current working directory) must be well defined.

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

回答by MD ABDULLAH

By putting the feature file under src/test/javawhere the runner and steps file or by putting it under src/main/javathe problem will get resolved.

通过将功能文件放在src/test/java下运行程序和步骤文件或将其放在src/main/java 下,问题将得到解决。

Don't forget to say thanks.:)

别忘了说声谢谢。:)