如何在多模块Maven项目中运行集成测试

vik*_*dey 2 java maven-3 maven maven-failsafe-plugin

我有一个具有以下结构的多模块项目:

Project:    
 - module1
 - module2
 - integration-test
 - parent pom
Run Code Online (Sandbox Code Playgroud)

实现以下目标的正确方法是什么:

  • 使用以下命令从所有模块运行单元测试(集成测试除外) 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>
Run Code Online (Sandbox Code Playgroud)

目前,当我运行mvn clean install它时,它也会运行集成测试。当我运行时mvn -Prun-its clean verify,它也正在运行其他模块的单元测试。我想念什么?

tom*_*tom 5

您可以通过-DskipITs=true在运行构建时进行设置来跳过集成测试的执行,如下所示:

mvn clean install -DskipITs=true
Run Code Online (Sandbox Code Playgroud)

这将运行除您的IT之外的所有其他测试(有关文档,请参见此处)。

如果只想跑步

mvn clean install
Run Code Online (Sandbox Code Playgroud)

您可以在pom.xml中为skipIT设置默认值

<properties>
    <skipITs>true</skipITs>
</properties>
Run Code Online (Sandbox Code Playgroud)

这样,您可以按需覆盖它

mvn clean install -DskipITs=false
Run Code Online (Sandbox Code Playgroud)

要仅运行没有单元测试的IT,您可以配置-Property之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>
Run Code Online (Sandbox Code Playgroud)

所以如果你跑

mvn clean install -DskipITs=false -DskipUnitTests=true
Run Code Online (Sandbox Code Playgroud)

请注意,默认情况下skipUnitTests将是false默认设置,因此无需为此声明属性。

如果您想使用个人资料,它应该像这样工作

    <profile>
        <id>ITs</id>
        <properties>
            <skipUnitTests>true</skipUnitTests>
            <skipITs>false</skipITs> 
        </properties>
    </profile>
Run Code Online (Sandbox Code Playgroud)

然后像这样运行构建

mvn clean install -PITs
Run Code Online (Sandbox Code Playgroud)

当然,您也可以在配置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>
Run Code Online (Sandbox Code Playgroud)