如何在默认情况下跳过集成测试,但仍然 运行 在多模块 Maven 项目中按需进行?
How to skip integration tests by default but still run them on-demand in multi-module maven project?
我有一个具有以下结构的多模块项目:
Project:
- module1
- module2
- integration-test
- parent pom
实现以下目标的正确方法是什么:
- 运行 所有模块的单元测试(集成测试除外)使用
mvn clean install
- 运行 按需集成测试(可以使用 maven-failsafe 插件或通过 maven 配置文件?)
- 集成测试失败时构建失败。
- 默认情况下,集成测试不应 运行 使用
mvn clean install
- 集成测试模块只有集成测试。
我已经尝试使用 maven-failsafe 插件和 maven-sunfire-plugin(用于单元测试)进行多次破解,但无法以标准方式实现上述目标。
以下是集成测试 pom 的相关部分:
<dependencies>
<!-- dependencies required for this module-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>add-integration-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-integration-test-resources</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/test/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>run-its</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
目前,当我 运行 mvn clean install
时,它也 运行 进行了集成测试。当我 运行 mvn -Prun-its clean verify
时,它也是来自其他模块的 运行ning 单元测试。我错过了什么?
我不知道这是否是最佳解决方案,但您可以使用配置文件实现。例如,在主要的 pom.xml 中,您只需在 <modules>
部分中添加其他模块,然后添加另一个配置文件:
<modules>
... standard modules ...
</modules>
<profiles>
<profile>
<id>tests</id>
<modules>
<module>module1</module>
... standard modules repeated (it might not be needed>...
<module>module2</module>
<module>module-integration-test</module>
</modules>
</profile>
</profiles>
然后 运行 如果您想 运行 进行测试,您可以使用该配置文件进行 maven。
maven -P tests clean install
如果您想 运行 集成测试和其他模块,那将是可行的。如果你只想 运行 集成测试,你可以这样做:
<modules>
<!-- EMPTY -->
</modules>
<profiles>
<profile>
<id>defaultModule</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>module1</module>
... standard modules repeated (it might not be needed>...
<module>module2</module>
</modules>
</profile>
<profile>
<id>tests</id>
<modules>
<module>module-integration-test</module>
</modules>
</profile>
</profiles>
使用 mvn clean install 的这种方式,您将 运行 使用 defaultModule(即 activeByDefault),如果您指定 -P 测试,您将 运行 仅测试
当 运行 构建时,您可以简单地通过设置 -DskipITs=true
来跳过集成测试的执行:
mvn clean install -DskipITs=true
这将 运行 除您的 IT 之外的所有其他测试(参见 here 文档)。
如果你只想运行
mvn clean install
您可以在 pom.xml
中设置 skipITs 的默认值
<properties>
<skipITs>true</skipITs>
</properties>
这样你就可以根据需要覆盖它
mvn clean install -DskipITs=false
对于 运行 只有没有单元测试的 IT,您可以像这样配置 maven-surefire-plugin
的 -属性
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skip>${skipUnitTests}</skip>
</configuration>
</plugin>
所以如果你 运行
mvn clean install -DskipITs=false -DskipUnitTests=true
请注意,skipUnitTests
默认为 false
,因此无需为此声明 属性。
如果您更愿意使用配置文件,它应该像这样工作
<profile>
<id>ITs</id>
<properties>
<skipUnitTests>true</skipUnitTests>
<skipITs>false</skipITs>
</properties>
</profile>
和运行这样的构建
mvn clean install -PITs
当然,您也可以直接在配置文件中将 plugin-configuration 用于 maven-surefire-plugin
和 true,这样就不需要额外的 属性,例如
<profile>
<id>ITs</id>
<properties>
<skipITs>false</skipITs>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
最好是制作一个单独的模块,其中包含看起来已经在您自己的问题中显示的集成测试。现在如何处理集成测试 运行ning 与否...
integration-test 模块如下所示:
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>groupOfYourParant</groupId>
<artifactId>integration-test</artifactId>
<version>0.1.0-SNAPSHOT</version>
</parent>
<artifactId>integration-test</artifactId>
<packaging>jar</packaging>
<name>Mod-IT</name>
<dependencies>
<dependency>
<groupId>groupId</groupId>
<artifactId>TheArtifact</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Supplemental deps only needed in this module -->
<dependency>
<groupId>....</groupId>
<artifactId>....</artifactId>
<version>...</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>run-its</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
通过使用它,您可以通过以下方式简单地控制 运行 集成测试:
mvn -Prun-its clean verify
重要的是您已经定义了集成测试模块所需的其他模块的依赖关系,或者确保必须在此集成测试模块之前构建这些模块。
此外,您现在可以在此处配置集成测试所需的补充内容,例如在 post-integration-test
阶段的 pre-integration-test
and/or 中应该是 运行。
如果您将测试代码放入 src/test/java
并将可选资源放入 src/test/resources
,您现在可以在集成测试模块中独立于任何其他模块定义依赖项。
您当然应该在父 pom 的 pluginManagement 中定义插件的版本,以定义在构建过程中使用的所有插件。
我有一个具有以下结构的多模块项目:
Project:
- module1
- module2
- integration-test
- parent pom
实现以下目标的正确方法是什么:
- 运行 所有模块的单元测试(集成测试除外)使用
mvn clean install
- 运行 按需集成测试(可以使用 maven-failsafe 插件或通过 maven 配置文件?)
- 集成测试失败时构建失败。
- 默认情况下,集成测试不应 运行 使用
mvn clean install
- 集成测试模块只有集成测试。
我已经尝试使用 maven-failsafe 插件和 maven-sunfire-plugin(用于单元测试)进行多次破解,但无法以标准方式实现上述目标。
以下是集成测试 pom 的相关部分:
<dependencies>
<!-- dependencies required for this module-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>add-integration-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-integration-test-resources</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/test/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>run-its</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
目前,当我 运行 mvn clean install
时,它也 运行 进行了集成测试。当我 运行 mvn -Prun-its clean verify
时,它也是来自其他模块的 运行ning 单元测试。我错过了什么?
我不知道这是否是最佳解决方案,但您可以使用配置文件实现。例如,在主要的 pom.xml 中,您只需在 <modules>
部分中添加其他模块,然后添加另一个配置文件:
<modules>
... standard modules ...
</modules>
<profiles>
<profile>
<id>tests</id>
<modules>
<module>module1</module>
... standard modules repeated (it might not be needed>...
<module>module2</module>
<module>module-integration-test</module>
</modules>
</profile>
</profiles>
然后 运行 如果您想 运行 进行测试,您可以使用该配置文件进行 maven。
maven -P tests clean install
如果您想 运行 集成测试和其他模块,那将是可行的。如果你只想 运行 集成测试,你可以这样做:
<modules>
<!-- EMPTY -->
</modules>
<profiles>
<profile>
<id>defaultModule</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>module1</module>
... standard modules repeated (it might not be needed>...
<module>module2</module>
</modules>
</profile>
<profile>
<id>tests</id>
<modules>
<module>module-integration-test</module>
</modules>
</profile>
</profiles>
使用 mvn clean install 的这种方式,您将 运行 使用 defaultModule(即 activeByDefault),如果您指定 -P 测试,您将 运行 仅测试
当 运行 构建时,您可以简单地通过设置 -DskipITs=true
来跳过集成测试的执行:
mvn clean install -DskipITs=true
这将 运行 除您的 IT 之外的所有其他测试(参见 here 文档)。
如果你只想运行
mvn clean install
您可以在 pom.xml
中设置 skipITs 的默认值<properties>
<skipITs>true</skipITs>
</properties>
这样你就可以根据需要覆盖它
mvn clean install -DskipITs=false
对于 运行 只有没有单元测试的 IT,您可以像这样配置 maven-surefire-plugin
的 -属性
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skip>${skipUnitTests}</skip>
</configuration>
</plugin>
所以如果你 运行
mvn clean install -DskipITs=false -DskipUnitTests=true
请注意,skipUnitTests
默认为 false
,因此无需为此声明 属性。
如果您更愿意使用配置文件,它应该像这样工作
<profile>
<id>ITs</id>
<properties>
<skipUnitTests>true</skipUnitTests>
<skipITs>false</skipITs>
</properties>
</profile>
和运行这样的构建
mvn clean install -PITs
当然,您也可以直接在配置文件中将 plugin-configuration 用于 maven-surefire-plugin
和 true,这样就不需要额外的 属性,例如
<profile>
<id>ITs</id>
<properties>
<skipITs>false</skipITs>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
最好是制作一个单独的模块,其中包含看起来已经在您自己的问题中显示的集成测试。现在如何处理集成测试 运行ning 与否...
integration-test 模块如下所示:
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>groupOfYourParant</groupId>
<artifactId>integration-test</artifactId>
<version>0.1.0-SNAPSHOT</version>
</parent>
<artifactId>integration-test</artifactId>
<packaging>jar</packaging>
<name>Mod-IT</name>
<dependencies>
<dependency>
<groupId>groupId</groupId>
<artifactId>TheArtifact</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Supplemental deps only needed in this module -->
<dependency>
<groupId>....</groupId>
<artifactId>....</artifactId>
<version>...</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>run-its</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
通过使用它,您可以通过以下方式简单地控制 运行 集成测试:
mvn -Prun-its clean verify
重要的是您已经定义了集成测试模块所需的其他模块的依赖关系,或者确保必须在此集成测试模块之前构建这些模块。
此外,您现在可以在此处配置集成测试所需的补充内容,例如在 post-integration-test
阶段的 pre-integration-test
and/or 中应该是 运行。
如果您将测试代码放入 src/test/java
并将可选资源放入 src/test/resources
,您现在可以在集成测试模块中独立于任何其他模块定义依赖项。
您当然应该在父 pom 的 pluginManagement 中定义插件的版本,以定义在构建过程中使用的所有插件。