【问题标题】:Maven fail-safe not executing testsMaven故障安全不执行测试
【发布时间】:2013-05-05 18:26:22
【问题描述】:

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

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

我正在跳过万无一失,因为此模块中没有单元测试:

<!-- 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 1/2 小时内进行了搜索,我们将不胜感激。唯一值得一提的是我有Cargo 设置和拆除一个Tomcat 容器。有人看到明显的问题吗?

【问题讨论】:

  • 为什么maven-failsafe-pluginintegration-test 阶段没有开箱即用?

标签: java maven maven-failsafe-plugin


【解决方案1】:

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

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

myModule/src/main/test/java/ClientAccessIT.java

应该是:

myModule/src/test/java/ClientAccessIT.java

您还可以更新您的 pom 文件(如果您真的希望测试存在于 main 中)以包括:

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

【讨论】:

  • 这应该是公认的答案。因此,给定的示例显然不起作用。
  • 您是指&lt;testResources&gt; 还是&lt;testSourceDirectory&gt;?我找不到元素&lt;testSources>
【解决方案2】:

我遇到了类似的问题。如果没有编译到目标/测试类的任何测试类,请检查您的 pom 文件并确保打包不是“pom”。

【讨论】:

  • 什么是“包装不是 pom”?有什么提示吗?
  • 在有效的 pom.xml 中查找标签“包装”。查看此链接以了解有关包装标签的更多信息。 maven.apache.org/pom.html。关键是要确保该标签的值不是“pom”。
【解决方案3】:

对于多模块项目,子项目需要插件声明如下,

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

version 继承自父 pom。如果没有上面的定义,单独在父 pom 中的插件将无法工作。

【讨论】:

  • 我必须在 parent 的 pom(pluginManagement 部分)中定义插件并在项目的 pom(plugins 部分)上使用它
【解决方案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,并删除了提供 TestEngine 以运行基于 JUnit 3 和 4 的测试的老式引擎。

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

【讨论】:

  • 感谢 neXus 的回答。它节省了很多时间,因为很难找到原因,因为我没有执行任何测试,因为所有测试都是用 Junit4 编写的。
  • 或者更新到junit 5:stackoverflow.com/questions/65073944/… :)
【解决方案5】:

您需要重命名您的测试类。

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

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

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

【讨论】:

  • 这似乎不是真的:maven.apache.org/surefire/maven-failsafe-plugin/examples/… - 它说默认模式是**/IT*.java**/*IT.java**/*ITCase.java
  • @acdcjunior 打败了我;这是相关文档页面的直接链接:maven.apache.org/surefire/maven-failsafe-plugin/…
  • "**/*IT.java" - 包括其所有子目录和所有以“IT”结尾的 java 文件名。 ClientAccessIT.java 不符合这个模式的条件吗?
  • 示例“ClientAccessIT.java”明显匹配*IT.java。所以这个答案只是无用且令人困惑。
  • 这个答案是错误的并且具有误导性。 @pro-tester 给出了正确答案
【解决方案6】:

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

我终于将最新版本(截至撰写本文时)更改为 2.22.0 和 viola。这是我添加到我的 pom.xml 的 &lt;build&gt;&lt;plugins&gt; 部分的配置。更好的是,我不需要添加定义您看到的大多数人包含的integration-testverify 目标的执行;故障安全默认执行这些。我也包含了我的surefire插件配置,因为我在maven生命周期的test阶段使用它来执行单元测试。

编辑:我还必须将 integration-testverify 执行添加到插件配置中。

<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$ -->
  <executions>
    <execution>
      <goals>
        <goal>integration-test</goal>
        <goal>verify</goal>
      </goals>
     </execution>
  </executions>
</plugin>

为了更好地衡量,为了告诉安全可靠和故障安全to use JUnit 5,我将其包含在我的&lt;dependencies&gt; 部分中:

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

Eclipse 在版本标记下显示警告(黄色波浪下划线),因此包含&lt;!--$NO-MVN-MAN-VER$ --&gt; 错误。我确信有更好的方法来处理这个问题,但在这种情况下它对我有用。

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

  • mvn failsafe:integration-test(仅运行集成测试)
  • mvn surefire:test(仅运行单元测试)
  • (我显然会运行整个生命周期)

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

【讨论】:

  • 对我来说,如果没有在测试模块中添加这个 junit-jupiter-engine 依赖项,什么都不会工作。感谢您的提示!虽然我还不完全理解它实际上是如何影响它并使它起作用的......谁能解释一下?当我通过 surfire-plugin(在默认测试阶段)开始这些测试时,不需要依赖并且一切正常。
  • 查看 Horatiu 的评论。他有正确的解决方案,正如 Maven 引用:integration-testverify
  • 有趣。我曾经不得不降级到 2.18.1。这仍然对我有用。除非您的测试是 junit 5,否则我必须使用 M-3.0.x stackoverflow.com/a/70991122/32453
【解决方案7】:

我也有类似的问题,但需要包装元素是“pom”。确保您的测试源文件正在编译并使用 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>

【讨论】:

  • 奇怪的是我的包装是 jar,但测试类没有编译。谢谢。
  • 这也是我的问题,只要我包含编译器插件集成测试就开始工作了!
  • 我的包装也是罐子,我也需要这样做
【解决方案8】:

对我来说,这是一个缺失的处决部分

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

添加上面显示的缺失执行部分解决了问题。

【讨论】:

  • 当然我们不会在文档中写出所有这些细节,因为插件目标相关的规范是阶段性的,关系到对Maven和插件文档和Maven基础的基本理解。查看目标maven.apache.org/surefire/maven-failsafe-plugin/…,什么是默认阶段(实际上故障安全没有默认阶段,但肯定有),然后我们希望用户对这些事情有一个共同的理解。我建议将目标 integration-testverify 分开执行 id.
【解决方案9】:

我正在使用 java 8,故障安全插件版本 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>

【讨论】:

  • 必须在插件的执行部分列出目标 integration-testverify。否则你不会看到它运行。
  • 我还添加了执行部分。
  • 我强烈建议使用一个版本 ${surefire.plugin.version} 而不是硬编码版本 2.22.2。
【解决方案10】:

我有一些类似的问题。我的集成测试没有被选中。我的项目是一个多模块 Spring Boot 项目。我的错误是我在父 POM 中添加了 maven-failsafe-plugin。在摆弄了许多选项之后,我将故障安全插件放在模块 pom 中,所有集成测试都在其中解决了我的问题。

【讨论】:

  • 目标 integration-testverify 必须列在插件的执行部分。否则你不会看到它运行。地点:父母或孩子不是你让它运行的答案。
【解决方案11】:

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

【讨论】:

【解决方案12】:

由于有很多部分可以导致这个问题,我将分享我的解决方法。

我的 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>

【讨论】:

    【解决方案13】:

    如果您使用JUnit5,请不要忘记将以下依赖项添加到您的测试类路径中:

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

    【讨论】:

      猜你喜欢
      • 2016-02-15
      • 1970-01-01
      • 2013-11-12
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多