Maven故障安全不执行测试

时间:2013-05-05 18:26:23

标签: java maven maven-failsafe-plugin

我已经梳理了StackOverflow和许多其他网站,发现了许多其他相关帖子,并且已经遵循了所有建议,但最终, failsafe正在跳过我的测试。

我的JUnit测试位于: myModule/src/main/test/java/ClientAccessIT.java

跳过surefire ,因为此模块中没有单元测试:

<!-- POM snippet -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
  <skip>true</skip>
  </configuration>
</plugin>

我正在尝试使用 failsafe 运行集成测试:

<!-- POM snippet -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <executions>
        <execution>
            <id>run-tests</id>
            <phase>integration-test</phase>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>

但是,当我运行mvn verify时,我看到了这一点:

[INFO] --- maven-failsafe-plugin:2.14.1:integration-test (run-tests) @ rest-services-test ---

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

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

我花了最后4个半小时的冲刷,任何帮助将不胜感激。唯一可能值得一提的是我有Cargo设置并拆除Tomcat容器。有人看到了明显的问题吗?

12 个答案:

答案 0 :(得分:19)

您需要重命名测试类。

您可以在the documentation中找到默认情况下插件所查找的名称,如@acdcjunior所指出的那样:

  

默认情况下,Failsafe插件将自动包含具有以下通配符模式的所有测试类:

     
      
  • **/IT*.java” - 包括所有子目录和所有以“IT”开头的java文件名。
  •   
  • **/*IT.java” - 包括所有子目录和所有以“IT”结尾的java文件名。
  •   
  • **/*ITCase.java” - 包括所有子目录和所有以“ITCase”结尾的java文件名。
  •   

答案 1 :(得分:13)

我有类似的问题。如果没有任何测试类编译到目标/测试类,那么检查你的pom文件并确保包装不是'pom'。

答案 2 :(得分:12)

对于多模块项目,子项目需要将插件声明为

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
 </plugin>

版本继承自父pom。如果没有上面定义的,单独使用父pom中的插件将无效。

答案 3 :(得分:11)

您的测试不在默认测试源目录src / test / java中。见:

https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

Mymodule中/ SRC /主/测试/ JAVA / ClientAccessIT.java

应该是:

Mymodule中/ SRC /测试/ JAVA / ClientAccessIT.java

您还可以更新您的pom文件(如果您确实希望测试生活在main中)以包含:

<build>
    <testSources>
        <testSource>
            <directory>src/main/test</directory>
        </testSource>
    </testSources>
</build>

答案 4 :(得分:6)

我也有类似的问题,但需要包装元素是&#34; pom&#34;。确保使用maven-compiler-plugin编译测试源文件并将其添加到... / target / test-classes文件夹中:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <executions>
                <execution>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

答案 5 :(得分:4)

如果您碰巧使用 Spring Boot 2.4 版 或更高版本,并且您的测试仍然使用 JUnit 4,请记住放置此依赖项:

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
</dependency>

spring-boot-starter-test 的 2.4 版仅包含 JUnit 5,并删除了为运行基于 JUnit 3 和 4 的测试提供 TestEngine 的老式引擎。

如果没有那个老式引擎,所有 JUnit 4 测试都会被默默忽略。

答案 6 :(得分:1)

我遇到了同样的问题,并在此处尝试了一些建议的选项。我正在使用JUnit 5开发Spring Boot应用程序。不幸的是,无论进行了advertising of the plugin,我的集成测试(位于src/test/java中并带有*TestIT.java后缀)都没有得到使用。所以。我尝试了downgrading to 2.18.1, 2.19.1,并尝试同时删除了<executions><configuration>部分,以尝试使用甚至插件的默认功能,但没有运气。

我终于将版本更改为最新版本(截至撰写本文时)为2.22.0和中提琴。这是我添加到pom的<build><plugins>部分中的配置。甚至更好的是,我不需要添加定义大多数人都包括的integration-testverify目标的执行;故障安全默认情况下执行这些操作。我还包括了surefire插件配置,因为我在maven生命周期的test阶段使用它来执行单元测试。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.22.0</version><!--$NO-MVN-MAN-VER$-->
</plugin>

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.22.0</version><!--$NO-MVN-MAN-VER$ -->
</plugin>

仅出于良好的考虑,为了告诉surefire和故障安全to use JUnit 5,我将其包含在我的<dependencies>部分中:

<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-engine</artifactId>
  <scope>test</scope>
</dependency>

Eclipse在版本标记下显示警告(黄色波浪形下划线),因此包含了<!--$NO-MVN-MAN-VER$ -->错误。我确定有更好的方法来解决此问题,但在这种情况下它对我有用。

为加快测试此配置的速度,并避免在testintegration-testverify阶段之前必须经历所有早期生命周期阶段的所有阶段,我使用以下命令快速验证:

  • mvn failsafe:integration-test(仅运行集成测试)
  • mvn surefire:test(仅运行单元测试)
  • (很明显,我很看重整个生命周期)

希望这对某人有帮助。干杯!

答案 7 :(得分:1)

对我来说,这是一个死刑处决部分

            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>

TestNG文档中的故障安全 https://maven.apache.org/surefire/maven-failsafe-plugin/examples/testng.html  没有它就显示了pom,但是没有用。

<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>3.0.0-M3</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    [...]
</plugins>

添加上面显示的执行遗漏部分可以解决此问题。

答案 8 :(得分:1)

我正在使用Java 8,failsafe插件版本2.22.2 =>测试未运行问题,以解决此问题,我接下来添加了此内容:

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-failsafe-plugin</artifactId>
     <version>${surefire.plugin.version}</version>
     <dependencies>
         <dependency>
             <groupId>org.apache.maven.surefire</groupId>
             <artifactId>surefire-junit47</artifactId>
             <version>2.22.2</version>
         </dependency>
    </dependencies>
     <executions>
         <execution>
             <id>integration</id>
             <phase>integration-test</phase>
             <goals>
                 <goal>integration-test</goal>
            </goals>
        </execution>
        <execution>
            <id>verify</id>
            <phase>verify</phase>
            <goals>
                <goal>verify</goal>
            </goals>
        </execution>
     </executions>
</plugin>

答案 9 :(得分:0)

我有类似的问题。我的集成测试没有被执行。我的项目是一个多模块spring boot项目。我的错误是我在父POM中添加了maven-failsafe-plugin。摆弄了很多选择之后,我将failsafe-plugin放在了模块pom中,在那里我进行了所有集成测试,从而解决了我的问题。

答案 10 :(得分:0)

对我来说,surefire:verify没有运行任何测试。 我不得不使用surefire:integration-test

答案 11 :(得分:0)

由于导致此问题的部分太多,我将分享我的解决方法。

我的 Maven 设置文件 ( .m2/settings.xml ) 中有一个活动配置文件,它的 maven.test.skip 属性设置为 true,当我通过 Maven 运行测试时,它会跳过测试。旧应用程序需要它,我完全忘记了该配置文件。

</profiles>
   <profile>
      <id>god-damnit-profile</id>
      <properties>
        ...
        <maven.test.skip>true</maven.test.skip>
      </properties>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>god-damnit-profile</activeProfile>
  </activeProfiles>

我已将其注释掉,现在可以使用了。

<activeProfiles>
    <!-- <activeProfile>god-damnit-profile</activeProfile> -->     
</activeProfiles>