如何在同一POM文件中使用Surefire和Failsafe插件实现JUnit5执行单元测试、TestNG执行集成测试
如何在同一POM文件中使用Surefire和Failsafe插件实现JUnit5执行单元测试、TestNG执行集成测试
嘿,我来帮你搞定这个配置问题!你的需求很清晰:mvn package只跑JUnit5单元测试,mvn verify先执行单元测试再用TestNG跑集成测试(指定XML文件)。现在问题出在Failsafe插件的配置、部分依赖冗余以及插件版本不匹配上,咱们一步步调整:
第一步:清理冗余依赖
你的POM里有些重复或不必要的依赖,先精简一下:
junit-jupiter已经包含junit-jupiter-api,可以删掉单独的junit-jupiter-api依赖testng-engine不是必须的,Failsafe会直接用TestNG provider执行测试junit-vintage-engine如果不需要兼容JUnit4测试,也可以删掉(按需保留即可)
第二步:调整Surefire插件配置
确保Surefire插件和它的JUnit Platform Provider版本一致(你之前插件用2.22.2但provider用3.2.1,版本不兼容会出问题),同时明确指定只使用JUnit Platform运行单元测试:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <!-- 使用和provider匹配的稳定版本 --> <version>3.2.1</version> <configuration> <!-- 明确指定使用JUnit Platform,确保只执行JUnit5单元测试 --> <provider>junit-platform</provider> </configuration> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit-platform</artifactId> <version>3.2.1</version> </dependency> </dependencies> </plugin>
这样mvn package时,Surefire就只会执行你的JUnit5单元测试了。
第三步:修复Failsafe插件配置
Failsafe负责集成测试,需要更新到稳定版本,指定TestNG provider,同时确保正确读取你的TestNG XML文件(注意原路径的换行要去掉,否则Maven找不到文件):
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <!-- 和Surefire版本保持一致,避免版本冲突 --> <version>3.2.1</version> <executions> <execution> <id>run-integration-tests</id> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> <configuration> <!-- 指定TestNG的XML suite文件,路径不要有换行 --> <suiteXmlFiles> <file>src/test/resources/testng-first-suite.xml</file> <file>src/test/resources/testng-second-suite.xml</file> </suiteXmlFiles> </configuration> <dependencies> <!-- 添加TestNG provider,确保Failsafe能识别并执行TestNG测试 --> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-testng</artifactId> <version>3.2.1</version> </dependency> </dependencies> </plugin>
第四步:完整的精简后POM依赖部分
<properties> <junit.jupiter.version>5.8.2</junit.jupiter.version> <testcontainers.version>1.17.2</testcontainers.version> <junit.platform.version>1.8.2</junit.jupiter.version> <testng.version>7.9.0</testng.version> <maven.surefire.version>3.2.1</maven.surefire.version> </properties> <dependencies> <!-- JUnit5核心依赖 --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <!-- TestContainers相关依赖 --> <dependency> <groupId>org.testcontainers</groupId> <artifactId>testcontainers</artifactId> <version>${testcontainers.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.testcontainers</groupId> <artifactId>junit-jupiter</artifactId> <version>${testcontainers.version}</version> <scope>test</scope> </dependency> <!-- JUnit Platform Suite(如果需要用Suite组织测试则保留) --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-suite-api</artifactId> <version>${junit.platform.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-suite-engine</artifactId> <version>${junit.platform.version}</version> <scope>test</scope> </dependency> <!-- TestNG依赖,用于集成测试 --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>${testng.version}</version> <scope>test</scope> </dependency> </dependencies>
验证执行效果
- 运行
mvn package:只会触发Surefire插件,执行JUnit5单元测试,不会运行集成测试 - 运行
mvn verify:会先执行Surefire的单元测试,再触发Failsafe插件,读取指定的TestNG XML文件执行集成测试
这样就完全符合你的需求啦!
备注:内容来源于stack exchange,提问作者DG82
相关产品推荐
相关产品推荐

