我的项目中有JUnit4和JUnit5测试。问题是,当我运行mvn clean install时,JUnit4测试会运行两次(JUnit5测试运行良好,只运行一次)。
在我的父项目pom中,我有以下尽是火的插件配置(只显示相关的依赖项)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<threadCount>1</threadCount>
<properties>
<property>
<name>junit</name>
<value>false</value>
</property>
</properties>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>2.22.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit-platform</artifactId>
<version>2.22.2</version>
</dependency>
</dependencies>
</plugin>
...
...
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- needed for https://youtrack.jetbrains.com/issue/IDEA-231927?_ga=2.101965186.223349104.1602977709-1646014256.1600106493 -->
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>我还在子项目中复制了上面的尽是火插件,以确保它不会被任何东西覆盖。但是,JUnit4测试仍然会运行两次。
以下是有效pom中的保险插件部分-
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<threadCount>1</threadCount>
<properties>
<property>
<name>junit</name>
<value>false</value>
</property>
</properties>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>2.22.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit-platform</artifactId>
<version>2.22.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.19.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</plugin在使用-X选项进行一些调试时,我认为原因是因为surefire-junit47也被添加到了为surefire-junit47插件提供程序中,surefire-junit-platform只运行一次junit4测试,而它们又由surefire-junit47提供者运行。如果这是一个可能的原因,那么我如何才能防止它被添加到万无一失的插件依赖呢?我试过了<classpathDependencyExcludes>,但这没有帮助,有效的pom仍然包含可靠的弹药47。
此外,是否有任何方法可以避免JUnit4运行两次,即使同时有两个提供者(surefire-junit47和surefire-junit-platform)?
我还将junit属性设置为false,以防止testng提供程序运行junit测试(如建议的这里)。但是,我仍然得到了两次JUnit4测试。我的猜测是,不知怎么的,surefire-junit47(它被神秘地添加了)和surefire-junit-platform在一起很奇怪,会导致重复的运行。
实际的问题是,我的项目继承了一个父pom,该父pom已经声明了一个依赖关系,即在尽是火插件47中的依赖关系。因此,有效地,我的项目有两个肯定火-junit-平台和万无一失-军民47,这导致了双重运行的JUnit4测试。
发布于 2020-10-18 00:59:40
我能重现你的问题。您正遇到一个描述这里的情况
TestNG 6.5.1及更高版本提供了在当前Maven项目中运行TestNG和JUnit 4.x的支持。(...) 您可能希望运行两个提供程序,例如
surefire-junit47和surefire-testng,并通过设置属性junit=false避免在surefire-testng提供程序中运行JUnit测试。
因此,请将您的强制插件配置更改为:
<configuration>
<threadCount>1</threadCount>
<properties>
<!-- Avoid running JUnit 4 tests in TestNG engine -->
<property>
<name>junit</name>
<value>false</value>
</property>
</properties>
</configuration>更新:如何(伪)排除直接的Maven插件依赖关系:
好的,FWIW,我想出了一种“排除”依赖的方法,方法是用一个假人来覆盖它。
首先,向项目的根POM添加一个模块,但不指定根POM作为其父模块。确保模块建在同一个反应堆里。或者,您也可以为它创建一个单独的项目,并确保其产生的工件在您公司的存储库中。
模块POM如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>dummy</version>
</project>在您希望停用JUnit 4.7引擎以支持在JUnit 5平台上运行JUnit 4测试的子POM中,您可以这样做:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire.version}</version>
<configuration>
<threadCount>1</threadCount>
<properties>
<!-- Avoid running JUnit 4 tests in TestNG engine -->
<property>
<name>junit</name>
<value>false</value>
</property>
</properties>
</configuration>
<dependencies>
<dependency>
<!-- Deactivate JUnit 4.7 engine by overriding it with an empty dummy -->
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>dummy</version>
</dependency>
</dependencies>
</plugin>
<!-- (...) -->
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>dummy</version>
</dependency>
</dependencies>这是非常丑陋的,如果可能的话,我仍然建议重构父POM。不管怎么说,这很管用。现在,您的JUnit 4测试只运行一次,即在JUnit 5平台内与JUnit 5测试、JUnit 2.0测试或其他测试一起运行。
更新2:在您提供了一个基于GitHub的MCVE之后,我向您发送了一个实现上一次更新中提到的解决方案的拉请求。重要的提交是这一个。
https://stackoverflow.com/questions/64408418
复制相似问题