如何在 Maven 安装目标中跳过测试,同时在 Maven 测试目标中运行它们? [英] How can I skip tests in maven install goal, while running them in maven test goal?

查看:49
本文介绍了如何在 Maven 安装目标中跳过测试,同时在 Maven 测试目标中运行它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多模块 maven 项目,在同一文件夹 (src/test/java) 中包含集成和单元测试.集成测试用 @Category(IntegrationTest.class) 标记.我想以以下设置结束:

I have a multi-module maven project with both integration and unit tests in the same folder (src/test/java). Integration tests are marked with @Category(IntegrationTest.class). I want to end up with the following setup:

  1. 如果我运行 mvn install,我希望编译所有测试,但我不想执行任何测试.
  2. 如果我运行 mvn test,我希望所有测试都能编译,但只执行单元测试.
  3. 如果我运行 mvn integration-test,我想编译并执行所有测试.
  1. If I run mvn install, I want all tests to compile, but I do not want to execute any.
  2. If I run mvn test, I want all tests to compile, but execute only unit tests.
  3. If I run mvn integration-test, I want to compile and execute all tests.

重要的一点是,我希望在 pom.xml 中配置它而没有任何额外的命令行参数.

The important point is, I want this configured in the pom.xml without any extra commandline arguments.

目前我在我的父 pom.xml 中提出了以下设置,其中唯一的问题是 #1,其中执行所有测试:

Currently I came up with the following setup in my parent pom.xml, where the only problem is #1, where all tests are executed:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${project.java.version}</source>
                    <target>${project.java.version}</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>
                <configuration>
                    <includes>
                        <include>**/*.class</include>
                    </includes>
                    <excludedGroups>cz.cuni.xrg.intlib.commons.IntegrationTest</excludedGroups>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-failsafe-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>
                <configuration>
                    <groups>cz.cuni.xrg.intlib.commons.IntegrationTest</groups>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>**/*.class</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

所有子模块在其 pom.xml 中都有以下插件配置,我认为应该从父 pom 继承:

All child modules have the following plugin configuration in their pom.xml, which I believe should inherit from the parent pom:

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

        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
        </plugin>
    </plugins>
</build>

我尝试使用 <skipTests>true</skipTests>,但它禁用了所有目标的测试执行,这不是我想要的(违反了 #2 和 #3).这也很奇怪,mvn test 尊重 skipTests=true 选项......为什么我首先要运行它??

I tried using <skipTests>true</skipTests>, but it disables test execution for all goals, which is not what I want (violates #2 and #3). It is also quite weird, that mvn test honors the skipTests=true option...why would I want to run it in the first place??

经过数小时的谷歌搜索和尝试不同的组合后,我很犹豫是否可以不在 mvn install 中运行测试,而同时在 mvn test.我希望有人证明这是错误的.;)

After hours of googling and trying different combinations, I am hesitant whether it is even possible to not run tests in mvn install, while at the same time run them in mvn test. I hope someone proves this wrong. ;)

我也愿意接受一个解决方案,其中 mvn install 将只执行单元测试,但我认为这没有太大区别.

I am also willing to accept a solution, where mvn install would execute only unit tests, but I don't think it makes much difference.

推荐答案

听起来你没有理解 在 Maven 中构建生命周期.如果您运行 mvn install 所有生命周期阶段(包括 install 阶段本身)在安装阶段之前运行.这意味着运行以下阶段:

It sounds like you didn't understand the concept of the build life-cycle in Maven. If you run mvn install all life-cycle phases (including the install phase itself) run before the install phase. This means running the following phases:

  1. 验证
  2. 初始化
  3. 生成源
  4. 流程来源
  5. 生成资源
  6. 流程资源
  7. 编译
  8. 流程类
  9. 生成测试源
  10. 过程测试源
  11. 生成测试资源
  12. 流程测试资源
  13. 测试编译
  14. 过程测试类
  15. 测试
  16. 准备包
  17. 包装
  18. 预集成测试
  19. 集成测试
  20. 集成后测试
  21. 验证
  22. 安装

换句话说,包括 testintegration-test 生命周期阶段.因此,如果没有任何补充信息,就不可能随心所欲地更改行为.

which means in other words the test as well as integration-test life-cycle phases are included. So without any supplemental information it's not possible to change the behaviour as you wish it.

可以通过在 Maven 中使用配置文件来实现:

It could be achieved by using a profile in Maven:

 <project>
  [...]
  <profiles>
    <profile>
      <id>no-unit-tests</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <skipTests>true</skipTests>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  [...]
</project>

所以你的第一个要求:

  1. 如果我运行 mvn install,我希望编译所有测试,但我不想执行任何测试.
  1. If I run mvn install, I want all tests to compile, but I do not want to execute any.

可以通过以下方式实现:

can be achieved by using the following:

mvn -Pno-unit-test test

  1. 如果我运行 mvn test,我希望所有测试都能编译,但只执行单元测试.
  1. If I run mvn test, I want all tests to compile, but execute only unit tests.

这可以通过简单的调用来实现:

This can simply achieved by using the plain call:

mvn test

因为未运行集成测试阶段(请参阅构建生命周期).

cause the integration tests phase is not run (see the build life cycle).

  1. 如果我运行 mvn integration-test,我想编译并执行所有测试.
  1. If I run mvn integration-test, I want to compile and execute all tests.

这意味着运行默认值,包括运行 test 阶段,该阶段将运行单元测试 (maven-surefire-plugin) 并进一步运行由 maven-failsafe-plugin 处理的集成测试.但是你应该知道,如果你想调用集成测试,你应该使用以下命令:

This means running the default which includes running the test phase which will run the unit tests (maven-surefire-plugin) and furthermore running the integration test which are handled by the maven-failsafe-plugin. But you should be aware that if you like to call the integration tests you should using the following command:

mvn verify

相反,因为您错过了之前调用中的 post-integration-test 阶段.

instead, cause you missed the post-integration-test phase in your previous call.

除了上述之外,您应该遵循单元和集成测试的命名约定,其中 单元测试 应该如下命名:

Apart from the above you should follow the naming conventions for unit and integration tests where unit tests should be named like the following:

<includes>
 <include>**/*Test*.java</include>
 <include>**/*Test.java</include>
 <include>**/*TestCase.java</include>
</includes>

集成测试命名如下:

<includes>
 <include>**/IT*.java</include>
 <include>**/*IT.java</include>
 <include>**/*ITCase.java</include>
</includes>

我希望你已经像下面这样配置了 maven-failsafe-plugin,这是将 maven-failsafe-plugin 绑定到正确的生命周期阶段所需的:

I hope you have configured the maven-failsafe-plugin like the following which is needed to bound the maven-failsafe-plugin to the correct life-cycle-phases:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.15</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

正如您所做的那样,但您应该知道 include 标记适用于源代码 (.java) 而不是编译名称 (.class).我不会使用 Category 注释,只是简单地使用命名约定使 pom 更简单和更短.

as you correctly did, but you should be aware that the include tags work on the source code (.java) and not on the compiled names (.class). I wouldn't use the Category annotation, just simply using the naming conventions makes the pom simpler and shorter.

这篇关于如何在 Maven 安装目标中跳过测试,同时在 Maven 测试目标中运行它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆