繁体   English   中英

JUnit5:Surefire 插件运行 JUnit4 测试两次

[英]JUnit5: Surefire plugin runs JUnit4 tests twice

我的项目中有 JUnit4 和 JUnit5 测试。 问题是当我运行mvn clean install ,JUnit4 测试运行了两次(JUnit5 测试运行良好,并且只运行一次)。

我的父项目 pom 中有以下 surefire-plugin 配置(仅显示相关的依赖项)

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

我也在子项目中复制了上面的 surefire-plugin 以确保它不会被任何东西覆盖。 但是,JUnit4 测试仍然运行两次。

以下是有效 pom 中的 surefire-plugin 部分 -

<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 -plugin 的提供者中,surefire surefire-junit-platform运行一次junit4 测试,然后它们由surefire-junit47提供者再次运行。 如果这是一个可能的原因,那么如何防止将其添加到 surefire-plugin 依赖项中? 我尝试了<classpathDependencyExcludes>但这没有帮助,有效的 pom 仍然包含 surefire-junit47。

即使有两个提供程序( surefire-junit47surefire-junit47 surefire-junit-platform ),还有什么方法可以避免 JUnit4 运行两次?

- - - - - 更新 - - - - - -

我还在surefire的配置中将junit属性设置为false ,以防止testng提供程序运行junit测试(如建议here )。 但是,我仍然运行了两次 JUnit4 测试。 我的猜测是以某种方式surefire-junit47 (它被神秘地添加)和surefire-junit-platform一起表现surefire-junit47奇怪,导致重复运行。

- - - - - 更新 - - - - - -

实际问题是我的项目继承了一个父 pom,它在surefire-plugin 中声明了一个依赖项surefire-junit47。 如此有效地我的项目同时具有 surefire-junit-platform 和 surefire-junit47 这导致 JUnit4 测试的双重运行。

我能够重现您的问题。 您遇到了此处描述的情况:

TestNG 6.5.1 及更高版本支持在当前 Maven 项目中运行 TestNG 和 JUnit 4.x。 (……)

您可能希望运行两个提供程序,例如surefire-junit47surefire-junit47 surefire-testng ,并通过设置属性junit=false避免在surefire-testng提供程序中运行 JUnit 测试。

因此,请将您的 Surefire 插件配置更改为:

<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 我想出了一种通过用虚拟对象覆盖它来“排除”依赖项的hacky 方法。

首先,您将一个模块添加到项目的根 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 测试、Spock 2.0 测试或其他测试一起运行。


更新 2:在您在 GitHub 上提供MCVE后,我向您发送了一个拉取请求,请求完全实现了我在上一次更新中提到的解决方法。 重要的提交是这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM