首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >maven尽是火- JUnit4 47没有运行JUnit4测试

maven尽是火- JUnit4 47没有运行JUnit4测试
EN

Stack Overflow用户
提问于 2019-07-10 13:03:44
回答 1查看 1.8K关注 0票数 1

在一个包含大量JUnit 4测试的maven项目中,尽是火-JUnit 47没有执行这些测试。

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

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

下面是一些显示问题的pom配置。

给定此测试类组织:

代码语言:javascript
复制
- 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 .....

考虑到这个测试类的结构:

代码语言:javascript
复制
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:按模式具体包括帐户测试

运行所有帐户测试,正如文档所指示的那样。

代码语言:javascript
复制
<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:按模式具体包括帐户测试

如文档所示,运行所有帐户和产品测试。

代码语言:javascript
复制
<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配置3:包括所有测试,基于默认的

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

代码语言:javascript
复制
<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:按模式包括所有测试

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

代码语言:javascript
复制
<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中尝试了不同的包含模式。见上文概述的结果。
  2. 配置了一个新项目,具有所有相同的导入和一些小测试。上述所有包含模式的行为都与文档中概述的相同。
  3. 将保险供应商改为保险公司4。事实上,这执行了所有的测试,但是我们遇到了其他问题。
  4. 运行mvn -X,主要是为了查找测试问题,基于这个答案:Surefire is not picking up Junit 4 tests
  5. mvn -X显示,默认的maven-resources在junit 3.8.x中使用,这可能会导致问题。将资源更新到3.1.0,但它没有解决我的问题。

mnv -X输出

太大了包括不了。如果你想要其中的一部分,请问。

EN

回答 1

Stack Overflow用户

发布于 2019-07-18 20:01:16

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

看起来,junit4逐个类处理测试类静力学(和静态@参数方法),其中junit47处理所有静力学,然后运行所有测试。所以你会得到:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56971331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档