问题:当使用较新版本的 Cucumber 和 Junit5 以及 cucumber-junit-platform-engine 时,mvn test 同时运行单元测试和集成测试。我们不能再单独运行它们,我正在寻找一种方法来解决这个问题。
我们使用surefire和failsafe插件在mvn verify上运行了单元测试和cucumber集成测试。更新到 Junit5 和 Cucumber 7.15.0 后,我们现在使用 cucumber-junit-platform-engine for maven 来发现 Cucumber 测试。问题是,我们不再可以选择单独运行这些测试。 mvn test 和 mvn verify 一起运行所有测试(单元测试和黄瓜测试)。
我们如何区分在 mvn test 上运行单元测试和在 mvn verify 上运行 cucumber 集成测试?
项目结构:
- src
|-integration-test
|-resources with feature files, and application.yml for tests
|-java
|-com.project.package.structure
|- scenariosteps
|- CucumberIT.java with @Suite annotation
|- IntegrationTestConfig with @TestConfiguration annotation
|- SpringEntry.java
|-Spring boot 3 project
|-test
|-java
|-com.project.package.structure
|-our junit5 + mockito unit tests
依赖关系(省略所有Spring Boot 3项目依赖)
Maven surefire plugin 3.2.5
Maven failsafe plugin 3.2.1
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>1.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
我一直在浏览文档+ stackoverflow 了解如何做到这一点。当我们使用 org.apache.maven.surefire.junitplatform.JUnitPlatformProvider 时,这工作正常,但看起来 io.cucumber.junit.platform.engine.DiscoverySelectorResolver 覆盖了所有内容:
在执行单元测试和 Cucumber 测试之前运行 mvn test 时,我收到此警告:
io.cucumber.junit.platform.engine.DiscoverySelectorResolver warnWhenCucumberFeaturesPropertyIsUsed 警告:使用 cucumber.features 属性发现测试。其他发现选择器将被忽略! 请请求/投票/赞助/等更好地支持 JUnit 5 发现选择器。 请参阅:https://github.com/cucumber/cucumber-jvm/pull/2498
当我从依赖项中删除 cucumber-junit-platform-engine 时,将使用以下内容。但这无法进行黄瓜测试。看起来切换回 JUnitPlatformProvider 不是一个选择,因为这不再与 Cucumber 测试兼容。
使用自动检测到的提供程序org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
我的万无一失/故障安全配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<skipTests>${unit-tests.skip}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>${cucumber.version}</version>
</dependency>
</dependencies>
<configuration>
<properties>
<configurationParameters>
cucumber.junit-platform.naming-strategy=long
</configurationParameters>
</properties>
</configuration>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${integration-tests.skip}</skipTests>
<runOrder>alphabetical</runOrder>
<parallel>none</parallel>
</configuration>
</execution>
</executions>
</plugin>
这就是我最终要做的:
为了解决
mvn run
运行单元和黄瓜集成测试的问题,并且在运行 mvn test/verify 时我们收到 io.cucumber.junit.platform.engine.DiscoverySelectorResolver warnWhenCucumberFeaturesPropertyIsUsed
警告,我使用了@M.P。 Korstanje 的解决方案:
从 junit-platform.properties 中删除了 cucumber.features。 对于 CucumberIT.java:
@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
// Worked the same as the line above: @ConfigurationParameter(key = FEATURES_PROPERTY_NAME, value = "src/integration-test/resources/features")
public class CucumberIT {
}
依赖关系:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
我的万无一失:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<skipTests>${unit-tests.skip}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<properties>
<configurationParameters>
cucumber.junit-platform.naming-strategy=long
</configurationParameters>
</properties>
</configuration>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${integration-tests.skip}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
最后一个问题是 Failsafe 不断运行集成测试两次,结果是有点旧的配置
<runOrder>alphabetical</runOrder><parallel>none</parallel>
就在 Failsafe 插件中的 <skipTests>${integration-tests.skip}</skipTests>
下面。