Maven故障安全不执行测试



我已经梳理了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

我花了四个半小时搜寻,任何帮助都会很感激。唯一值得一提的另一件事是,我让Cargo设置和拆除Tomcat容器。有人看到这个突出的问题了吗?

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

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

myModule里/src/main/测试/java/ClientAccessIT.java

应:

myModule里/src/测试/java/ClientAccessIT.java

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

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

如果您碰巧使用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测试都会被静默地忽略。

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

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

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

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

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

您可以在文档中找到插件默认查找的名称,如@acdcjunior:

所指出的:

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

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

我有同样的问题,并尝试了一些建议的选项在这里。我正在使用JUnit 5开发一个Spring Boot应用程序。不幸的是,我的所有集成测试(位于src/test/java,后缀为*TestIT.java)都没有被选中,不管插件的广告是这样做的。我试着降级到2.18.1和2.19.1,并尝试删除<executions><configuration>部分,试图使用甚至插件的默认功能,但没有运气。

我最终将版本更改为最新的(截至撰写本文时)2.22.0,并且viola。下面是我添加到pom的<build><plugins>部分的配置。更好的是,我不需要添加定义integration-testverify目标的执行,你看到大多数人都包括这些目标;Failsafe默认执行这些操作。我还包括了我的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>

为了更好地衡量,为了告诉使用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(仅运行单元测试)
  • (当它计数时,我显然运行整个生命周期)

希望这能帮助到别人。干杯!

我也有一个类似的问题,但需要包装元素是"pom"。确保您的测试源文件正在被编译并添加到…/target/test-classes文件夹,使用maven-compiler-plugin:

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

对于我来说,这是一个缺失的执行部分

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

带有TestNG文档的failsafe在https://maven.apache.org/surefire/maven-failsafe-plugin/examples/testng.html我展示了一个没有它的诗歌,但它不起作用。

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

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

我使用的是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>

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

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

因为有这么多的片段可以导致这个问题,我将分享我是如何解决的。

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

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

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

最新更新