简体   繁体   English

Maven surefire-junit47没有运行JUnit4测试

[英]maven surefire-junit47 is not running JUnit4 tests

In a maven project with lots of JUnit 4 tests, surefire-junit47 is not executing the tests. 在具有大量JUnit 4测试的Maven项目中,surefire-junit47不执行测试。

There are no testng tests in this project, and there is no testng in the pom. 此项目中没有testng测试,并且pom中没有testng。 But this project has a dependency on another project that does have testng in the pom. 但是该项目依赖于另一个在pom中包含testng的项目。 You can see it import testng in the mvn -X output below. 您可以在下面的mvn -X输出中看到它导入了testng。

For reference sake, here is the documentation I'm working from: https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html 作为参考,这是我正在使用的文档: https : //maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

Here are a couple of pom configurations that show the problem. 这是显示问题的几个pom配置。

Given this test class organization: 给定这个测试班级组织:

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

And given this test class structure: 并给出以下测试类结构:

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 config 1: Specifically include Account tests, by pattern pom config 1:按模式专门包括帐户测试

Runs all the Account tests, just as the documentation indicates. 按照文档指示运行所有“帐户”测试。

<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 config 2: Specifically include Account tests, by pattern pom config 2:按模式专门包含帐户测试

Runs all the Account and Product tests, just as the documentation indicates. 按照文档指示运行所有“帐户”和“产品”测试。

<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: Include all tests, based on default surefire pom config 3:根据默认的surefire包括所有测试

Finds and initializes test classes, but does not execute any @Test methods. 查找并初始化测试类,但不执行任何@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 config 4: Include all tests, by pattern pom config 4:按模式包含所有测试

Finds and initializes test classes, but does not execute any @Test methods. 查找并初始化测试类,但不执行任何@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>

What I've tried: 我尝试过的

  1. Obviously, from the examples, I've tried different include patterns in the pom. 显然,从示例中,我在pom中尝试了不同的包含模式。 See the results outlined above. 请参阅上面概述的结果。

  2. Configured a new project, with all the same imports and just a few small tests. 配置了一个新项目,所有导入都相同,并且进行了一些小测试。 All the include patterns above behaved as outlined in the documentation. 上面所有的包含模式都按照文档中的概述进行操作。

  3. Switched the surefire provider to surefire-junit4. 将surefire提供程序切换为surefire-junit4。 This, in fact, executed all tests, but we ran into other problems. 实际上,这执行了所有测试,但是我们遇到了其他问题。

  4. Ran mvn -X, mainly to look for testng problems, based on this answer: Surefire is not picking up Junit 4 tests . 根据此答案,主要运行mvn -X来查找testng问题: Surefire不接受Junit 4测试

  5. mvn -X showed that the default maven-resources-plugin pulls in junit 3.8.x, which the doc says might cause problems. mvn -X表明默认的maven-resources-plugin插入了junit 3.8.x,文档说这可能会引起问题。 Updated resources to 3.1.0, but it didn't fix my problem. 将资源更新为3.1.0,但没有解决我的问题。

mnv -X output mnv -X输出

Is way too big to include. 太大而无法包含。 If you want part of it, please ask. 如果您想要一部分,请询问。

Turns out that maven is, in fact, running the tests. 事实证明,maven实际上正在运行测试。 The reason I thought it was not running the tests is because of changes in how JunitCore() works in 4.7+. 我认为它没有运行测试的原因是因为JunitCore()在4.7+版本中的工作方式发生了变化。

It seems that junit4 processes test class statics (and static @Parameters methods) class by class, where junit47 processes all statics, then runs all tests. 似乎junit4逐级处理测试类的静态变量(和静态@Parameters方法),其中junit47处理所有的静态变量,然后运行所有测试。 So you'd get: 因此,您将获得:

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


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

More detail on this, from someone who understands it better than me, in this thread: https://issues.apache.org/jira/browse/SUREFIRE-1676?filter=-2 有关此内容的更多详细信息,来自比我更了解的人,位于以下主题中: https : //issues.apache.org/jira/browse/SUREFIRE-1676?filter=-2

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

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