【问题标题】:Maven doesn't execute any unit testMaven 不执行任何单元测试
【发布时间】:2013-05-23 07:27:59
【问题描述】:

我正在使用带有多模块的 Maven。有3个项目。

foo(the parent project)
foo-core
foo-bar

我在foo的pom中配置了所有的依赖和插件:

<modules>
    <module>../foo-core</module>
    <module>../foo-bar</module>
</modules>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            ...
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.14.1</version>
                <dependencies>
                    <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.14.1</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

foo-core 中有几个用于单元测试的基类和实用程序类,所以我在foo-core 项目中添加了maven-jar-plugin 以使其对foo-bar 可用:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
               <execution>
                   <goals>
                       <goal>test-jar</goal>
                   </goals>
               </execution>
            </executions>
        </plugin>
    </plugins>
</build>

当我执行test 目标时,我得到如下结果:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
parallel='none', perCoreThreadCount=true, threadCount=2, useUnlimitedThreads=false

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

我确实在我的项目中进行了测试。但为什么它不运行其中的任何一个?

【问题讨论】:

标签: unit-testing maven maven-surefire-plugin


【解决方案1】:

将测试文件从**Tests.java重命名为**Test.java或将以下配置添加到pom.xml

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.14.1</version>
  <configuration>
    <includes>
      <include>**/*Tests.java</include>
    </includes>
  </configuration>
</plugin>

【讨论】:

  • @Kirin Yao 别担心!我知道答案只是因为我在每个项目开始时都犯了同样的错误。 :-)
  • 这对我不起作用。我有一个类“OptimizerTest.java”,它完美地工作,没有任何包含,但另一个类“OptimizerTest2.java”不起作用。如果我运行 mvn:test -Dtest=OptimizerTest 我收到 BUILD SUCCESS 并通过 3 个测试,如果我运行相同的命令但对于 OptimizerTest2 我收到 BUILD FAILURE,测试运行:0 其中 OptimizerTest2 只是第一个的副本°_°
  • @Aerox OptimizerTest2.java 不包括在内,因为名称以 Test2 而不是 Tests 结尾。
  • 你是对的。问题是错误的导入。我使用了org.junit.Test 打算使用org.testng.annotations.Test,所以我反复收到错误。无论如何,谢谢你;-)
  • 有什么原因它不能只扫描 src/test/java 中的所有类并默认查找@Test 注解的方法?
【解决方案2】:

此外,导致这种行为的另一个原因是,如果您在 pom.xml 依赖项中排除了 junit-vintage-engine

就我而言,我已将其从 pom.xmlspring-boot-starter-test 依赖项中排除

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
    </exclusion>
  </exclusions>
</dependency>

这导致了同样的问题。 Maven 无法识别/找到项目中的任何测试用例,即使它们存在。

所以只需删除排除部分并再次运行 maven 命令。

【讨论】:

    【解决方案3】:

    有类似的问题可以通过添加对 sunfire 插件的依赖来运行单元测试用例

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit4</artifactId>
                        <version>2.22.0</version>
                    </dependency>
                </dependencies>
            </plugin>
    

    通过在 mvn install 、 mvn test 和所有测试用例上执行测试用例,上述工作正常

    【讨论】:

      【解决方案4】:

      就我而言,有必要在所有带有测试的文件的名称末尾添加单词"Test"。 例如,如果您有"LoginTestNegative",那么这将不起作用。您需要将其重命名为LoginNegativeTest。现在它可以工作了。

      【讨论】:

        猜你喜欢
        • 2011-06-06
        • 2020-03-18
        • 2018-12-18
        • 1970-01-01
        • 2013-04-08
        • 1970-01-01
        • 2016-12-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多