【问题标题】:maven surefire-junit47 is not running JUnit4 testsmaven surefire-junit47 没有运行 JUnit4 测试
【发布时间】:2019-11-20 02:49:56
【问题描述】:

在一个包含大量 JUnit 4 测试的 maven 项目中,surefire-junit47 没有执行测试。

本项目中没有testng测试,pom中也没有testng。但是这个项目依赖于另一个在 pom.xml 中有 testng 的项目。您可以在下面的 mvn -X 输出中看到它 import testng。

为了参考,这里是我正在使用的文档: https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

这里有几个显示问题的 pom 配置。

鉴于这个测试类组织:

- src/main/test/
   - com.mycomp.qc.core.account
     - CopyAccountTests.java
     - CreateAccountTests.java
     - DeleteAccountTests.java
     - ListAccountTests.java
     - ReadAccountTests.java
     - UpdateAccountTests.java
   - com.mycomp.qc.core.product
     - CopyProductTests.java
     - CreateProductTests.java
     - DeleteProductTests.java
     - ListProductTests.java
     - ReadProductTests.java
     - UpdateProductTests.java
   - ..... and 300 more packages .....

给定这个测试类结构:

package com.mycomp.qc.core.account;

import org.junit.Assert;
import org.junit.Test;
.... and more ....

public class CopyAccountTests {

    @Test
    public void copyAccount1() {
        Assert.assertTrue("pass", true);
    }

    @Test
    public void copyAccount2() {
        Assert.assertTrue("fail", false);
    }

.... and more ....

}

pom 配置 1:具体包括帐户测试,按模式

按照文档说明运行所有帐户测试。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${surefire.version}</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>${surefire.version}</version>
        </dependency>
    </dependencies>
    <configuration>
        <testFailureIgnore>true</testFailureIgnore>
        <includes>
            <include>*Account*</include>
        </includes>
        <threadCount>1</threadCount>
    </configuration>
    <executions>
        <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
</plugin>

pom 配置 2:具体包括帐户测试,按模式

按照文档说明运行所有帐户和产品测试。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${surefire.version}</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>${surefire.version}</version>
        </dependency>
    </dependencies>
    <configuration>
        <testFailureIgnore>true</testFailureIgnore>
        <includes>
            <include>*Account*</include>
            <include>*Product*</include>
        </includes>
        <threadCount>1</threadCount>
    </configuration>
    <executions>
        <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
</plugin>

pom config 3:包括所有测试,基于默认的surefire

查找并初始化测试类,但不执行任何@Test 方法。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${surefire.version}</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>${surefire.version}</version>
        </dependency>
    </dependencies>
    <configuration>
        <testFailureIgnore>true</testFailureIgnore>
        <threadCount>1</threadCount>
    </configuration>
    <executions>
        <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
</plugin>

pom 配置 4:包括所有测试,按模式

查找并初始化测试类,但不执行任何@Test 方法。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${surefire.version}</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>${surefire.version}</version>
        </dependency>
    </dependencies>
    <configuration>
        <testFailureIgnore>true</testFailureIgnore>
        <includes>
            <include>*Test*</include>
        </includes>
        <threadCount>1</threadCount>
    </configuration>
    <executions>
        <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
</plugin>

我的尝试:

  1. 显然,从示例中,我在 pom.xml 中尝试了不同的包含模式。查看上述结果。

  2. 配置了一个新项目,所有相同的导入和几个小测试。上述所有包含模式的行为都如文档中所述。

  3. 将surefire 提供程序切换到surefire-junit4。这实际上执行了所有测试,但我们遇到了其他问题。

  4. 跑mvn -X,主要是找testng问题,基于这个答案:Surefire is not picking up Junit 4 tests

  5. mvn -X 显示默认的 maven-resources-plugin 引入了 junit 3.8.x,文档称这可能会导致问题。将资源更新到 3.1.0,但没有解决我的问题。

mnv -X 输出

太大了,无法包含。如果您想要其中的一部分,请询问。

【问题讨论】:

  • 为什么你觉得有必要添加 surefire-junit47 依赖?通常不需要。
  • 我们将 testng 作为依赖包含在另一个项目中,而这又是这个项目的依赖。使用类路径上的 testng,surefire 会尝试通过 testng 执行测试。所以我们正在根据这个页面配置我们的测试执行:maven.apache.org/surefire/maven-surefire-plugin/examples/…
  • 如果当前项目构建不使用 testng 传递依赖项,它是否可以从其他依赖项中排除它?
  • 项目测试依赖应该相互独立。它们的范围是否都为test

标签: java maven junit junit4 maven-surefire-plugin


【解决方案1】:

事实证明,maven 实际上正在运行测试。我认为它没有运行测试的原因是因为 JunitCore() 在 4.7+ 中的工作方式发生了变化。

似乎 junit4 按类处理测试类静态(和静态 @Parameters 方法),其中 junit47 处理所有静态,然后运行所有测试。所以你会得到:

junit4
- ClassA
  - staticField1
  - staticMethod1
  - testMethod1
- ClassB
  - staticField2
  - staticMethod2
  - testMethod2


junit47
- Initialize:
  - ClassA
    - staticField1
    - staticMethod1
  - ClassB
    - staticField2
    - staticMethod2
- ClassA
  - testMethod1
- ClassB
  - testMethod2

关于这个的更多细节,来自比我更了解它的人,在这个线程中: https://issues.apache.org/jira/browse/SUREFIRE-1676?filter=-2

【讨论】:

    最近更新 更多