带有 JUnit 5 的 Cucumber:未找到功能,未执行测试

Cucumber with JUnit 5: features not found, tests aren't executed

我在 JUnit 4 中使用 Cucumber 已经有一段时间了,但目前我需要第一次在 JUnit 5 中使用它,但它似乎不起作用。我有以下依赖项:

    ...
    <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-launcher</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
      <scope>test</scope>
    </dependency>
    ...
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-java</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-junit</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-junit-platform-engine</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-picocontainer</artifactId>
      <scope>test</scope>
    </dependency>
    ...

我正在使用配置如下的 failsafe maven 插件:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <configuration>
          <groups>profileServer</groups>
          <systemPropertyVariables>
            <jacoco-agent.destfile>${project.build.directory}/jacoco-it.exec</jacoco-agent.destfile>
          </systemPropertyVariables>
        </configuration>
        <executions>
          <execution>
            <id>integration-test</id>
            <phase>integration-test</phase>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

测试 class 如下所示:

@Cucumber
@Tag("profileServer")
public class CustomersCucumberIT
{
}

这里我使用 @Tag 注释替换 JUnit4 @Category,以便根据上面的故障安全 maven 插件中配置的元素有选择地执行测试。

features 文件位于 src/main/test/resources/features/it 中,其名称为 customers.features。

最后但同样重要的是,这里是步骤 class:

@Tag("profileServer")
public class CustomersCucumberSteps
{
  ...
}

运行 验证目标显示以下输出: ┌──────────────────────────────────────────────── ──────────────────────────────┐ │ 在 https://reports.cucumber.io 与您的团队分享您的黄瓜报告 │ │ 使用以下其中一项激活发布:│ │ │ │ src/test/resources/cucumber.properties: cucumber.publish.enabled=true │ │ 环境变量:CUCUMBER_PUBLISH_ENABLED=true │ │ JUnit: @CucumberOptions(publish = true) │ │ │ │ 更多信息请见 https://reports.cucumber.io/docs/cucumber-jvm │ │ │ │ 要禁用此消息,请将 cucumber.publish.quiet=true 添加到 │ │ src/test/resources/cucumber.属性 │ └────────────────────────────────────────────── ──────────────────────────────┘

但只是跳过测试执行,就好像没有任何测试一样。以前,在 JUnit 4 中,我使用 @CucumberOptions 来设置功能位置。但是对于 JUnit 5,这个注解不再受支持,我也找不到任何其他注解。看来应该是被发现了。

我看到一些帖子提到功能文件可能在 failsafe 或 surefire maven 插件中配置:

...
<options>
  <configurationParameters>
    ...
  </configueationParameters>
</options>
...

但是这种语法似乎不受支持,而且无论如何,我没有找到任何可以用来配置步骤位置的参数。

有人可以启发我并提供一个简单的例子吗?

非常感谢。

西摩

https://github.com/cucumber/cucumber-jvm/blob/main/junit-platform-engine/src/main/java/io/cucumber/junit/platform/engine/Cucumber.java

https://github.com/cucumber/cucumber-jvm/tree/main/junit-platform-engine

Maven Surefire and Gradle do not yet support discovery of non-class based tests (see: gradle/#4773, SUREFIRE-1724).

As a workaround you can use the @Cucumber annotation. Cucumber will scan the package of a class annotated with @Cucumber for feature files.

所以如果跑步者 class 是 src/test/java/com/example/RunCucumberIT 那么特征文件应该在 src/test/resources/com/example.

我正在回答我自己的问题。事实上,在我的案例中使用的 Cucumber 6.7.0 似乎无法正确解释 JUnit 5“@Tag”注释。在上面给出的当前测试用例中,注释掉故障安全插件配置中的“groups”语句(用于过滤测试执行)的行为符合预期,即所有测试都已执行。保持此语句未注释,无论“@Tag”注释定义什么,都会跳过所有测试。

但是,JUnit 4“@Category”注释按预期工作,即过滤没问题。因此,为了过滤基于例如 Maven 配置文件的 Cucumber 测试执行,只有 JUnit 4 样式有效。