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



我在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>

测试类如下:

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

这里我使用了@Tag注释,它取代了JUnit4 @Category,以便有选择性地执行基于failsafe maven插件中配置的元素的测试。

特性文件位于src/main/test/resources/features/it目录下,文件名为customers.features.

最后但并非最不重要的是,这里是步骤类:

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

运行verify goal命令显示如下输出:┌─────────────────────────────────────────────────────────────────────────────┐│在https://reports.cucumber.io│与你的团队分享你的黄瓜报告│使用下列命令之一激活发布│ ││src/测试/资源/黄瓜。属性:cucumber.publish。启用= true││环境变量:CUCUMBER_PUBLISH_ENABLED=true│JUnit: @CucumberOptions(publish = true)││ ││更多信息请访问https://reports.cucumber.io/docs/cucumber-jvm││ │要禁用此消息,请添加cucumber.publish。安静=真实的││src/测试/资源/黄瓜。属性 │└─────────────────────────────────────────────────────────────────────────────┘

只是简单地跳过测试执行,就好像没有测试一样。以前,在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和Gradle还不支持发现非基于类的测试(参见:Gradle/#4773, Surefire -1724)。

作为一种解决方法,您可以使用@Cucumber注释。Cucumber将扫描带有@Cucumber注释的类的包以查找特征文件。

因此,如果运行器类是src/test/java/com/example/RunCucumberIT,那么功能文件应该在src/test/resources/com/example

我在回答自己的问题。看起来,事实上,在我的情况下使用的Cucumber 6.7.0无法正确解释JUnit 5 "@Tag"注释。在上面给出的当前测试用例中,注释掉"组";用于过滤测试执行的failsafe插件配置中的语句,其行为符合预期,即执行所有测试。保持这个语句不加注释,不管"@Tag"注释定义时,跳过所有测试。

然而,JUnit 4 "@Category"注释按预期工作,即过滤正常。因此,为了过滤基于Cucumber的测试执行,例如,maven配置文件,只有JUnit 4风格才能工作。

最新更新