简体   繁体   English

在多模块Maven项目中构建所有模块后,如何运行集成测试?

[英]How can I run integration tests after building all modules in a multi-module Maven project?

I am working on a large multi-module Maven project. 我正在研究一个大型的多模块Maven项目。 Each module has fast unit tests (using the Surefire plugin), many modules have slow integration tests (using the Failsafe plugin). 每个模块都有快速单元测试(使用Surefire插件),许多模块都有慢速集成测试(使用Failsafe插件)。

I would like to speed up the feedback for "simple" build failures (compilation errors and unit test failures) by running integration tests from all modules after all modules have been built and unit-tested. 我想通过在构建所有模块并进行单元测试后运行所有模块的集成测试来加快“简单”构建失败(编译错误和单元测试失败)的反馈。

Can you suggest a good way of achieving this? 你能提出一个实现这个目标的好方法吗?

running integration tests from all modules after all modules have been built and unit-tested 在构建所有模块并进行单元测试后,从所有模块运行集成测试

To meet this requirement a possible solution is to have an additional (optional) module providing integration tests, that is, all integration tests should be moved to this module, which could be added to the default build via a profile. 为了满足此要求,可能的解决方案是使用附加(可选)模块提供集成测试,即,应将所有集成测试移至此模块,该模块可通过配置文件添加到默认构建中。

<profiles>
   <profile>
      <id>it</id>
      <modules>
        ...
        <module>it-module</module>
      </modules>
   </profile>
</profiles>

In order to have it as last step of the build for the Maven Reactor , this module should also depend on all other modules (probably it would implicitly already be the case). 为了使它成为Maven Reactor构建的最后一步,该模块还应该依赖于所有其他模块(可能它已经隐含地已经是这种情况)。

This module would actually be the only one providing maven-failsafe-plugin configuration and settings. 该模块实际上是唯一一个提供maven-failsafe-plugin配置和设置的模块。 Moreover, it might become more meaningful in case of the usage of an embedded server to test against (ie Jetty): the server will be created and shutted down only during the build of this module and not in each and every module requiring it, speeding up this integration phase. 此外,在使用嵌入式服务器进行测试(即Jetty)的情况下,它可能会变得更有意义:服务器将仅在构建此模块期间创建和关闭,而不是在需要它的每个模块中创建和关闭,加速这个整合阶段。

Being the last module of the build, you will make sure that it would be reached only in case of no unit test failures on previous modules and only when the it profile would be active (eg during CI builds). 作为构建的最后一个模块,您将确保仅在以前模块上没有单元测试失败的情况下才会到达它,并且只有当it配置文件处于活动状态时才会到达(例如在CI构建期间)。

The short answer is probably to move the failsafe-plugin into a profile. 简短的回答可能是将failsafe-plugin移动到配置文件中。

Since maven itself does not come with a default mapping for the failsafe plugin I assume it is enabled in your parent pom or (worse?) the company parent pom. 由于maven本身并没有为failafe插件提供默认映射,我认为它是在你的父pom中启用的,或者(更糟糕的是)公司的父pom。

The way would be to create a profile to enable the integration tests and only enable that profile when needed. 方法是创建配置文件以启用集成测试,并仅在需要时启用该配置文件。 To avoid waste this profile could disable the surefire-tests to not re-execute them. 为避免浪费,此配置文件可能会禁用surefire-tests以不重新执行它们。 If they are fast anyway that might not be an issue. 如果它们很快就可能不是问题。 In that case re-execute them for starting. 在这种情况下,重新执行它们以便开始。

I think its ok to extract this into a profile. 我认为可以将其提取到配置文件中。 My general rule for maven profiles is that they are not allowed to change the resulting artifact (they can, but it usually gets messy). 我对maven配置文件的一般规则是,它们不允许更改生成的工件(它们可以,但通常会变得混乱)。 In this case you add another execution step. 在这种情况下,您添加另一个执行步骤。 So that should not hurt. 所以这不应该伤害。 Just make sure the profile is enabled when cutting a release :) 只需确保在切割版本时启用了配置文件:)

Here's a solution that does exactly what I want: using the Failsafe plugin to run just the integration tests without having to move tests out of their natural places or recompile in order to run the tests. 这是一个完全符合我想要的解决方案:使用Failsafe插件只运行集成测试,而不必将测试移出自然位置或重新编译以运行测试。

mvn install -DskipITs
mvn failsafe:integration-test

We get Maven to publish the artifacts for the project into a local directory. 我们让Maven将项目的工件发布到本地目录中。 We can then use that as a repository for the second step. 然后我们可以将其用作第二步的存储库。 In that second step we then invoke exactly the goal that we need and it grabs the artifacts from the local repository. 在第二步中,我们将完全调用我们需要的目标,并从本地存储库中获取工件。

The same thing works for Surefire ( mvn surefire:test ) if you need to run unit tests separately. 如果你需要单独运行单元测试,同样的事情适用于Surefire( mvn surefire:test )。

I have to admit that I don't fully understand the Maven model and hence why this works. 我不得不承认,我并不完全了解Maven模型,因此我认为这是有效的。 The internet seems unanimously of the view that this kind of thing can't be done (see other answers here), but it's definitely working for me. 互联网似乎一致认为这种事情无法完成(请参见此处的其他答案),但它绝对适合我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Maven:如何在多模块项目中构建其他模块之前运行模块? - Maven: How to run module before building other modules in multi-module project? 如何默认跳过集成测试但仍然在多模块 maven 项目中按需运行它们? - How to skip integration tests by default but still run them on-demand in multi-module maven project? 应该将性能/集成测试视为多模块Maven项目中的模块吗? - Should performance/integration tests be considered modules in a multi-module Maven project? 如何配置SonarQube以显示多模块Maven项目的所有模块? - How do I configure SonarQube to display all modules of a multi-module maven project? Maven无法识别多模块项目中的集成测试 - Maven not recognizing integration tests in multi-module project 在 Maven 多模块项目中,如何在一个孩子上运行完整构建并运行特定插件? - In a Maven multi-module project, how can I run a full build and run a specific plugin on one child? 如何引用多模块Maven项目? - How can I reference a multi-module maven project? 如果我将测试放在单独的模块中,如何在多模块 maven 设置中运行单元测试? - How to run unit tests in multi-module maven setup if i put the tests in a separate module? 如何在没有指定版本的情况下拥有多模块Maven项目? - How can I have a multi-module Maven project without specifying the version all over the place? 为 SonarCloud 构建多模块 maven 项目 - Building multi-module maven project for SonarCloud
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM