如何运行Maven Integration测试

Pet*_*ney 160 java testing maven-2 surefire

我有一个maven2多模块项目,在我的每个子模块中,我都有JUnit测试,这些测试都是命名的Test.java,分别Integration.java用于单元测试和集成测试.当我执行:

mvn test

*Test.java执行子模块中的所有JUnit测试.当我执行

mvn test -Dtest=**/*Integration

没有任何Integration.java测试在子模块中执行.

这些看起来像是完全相同的命令,但是带有-Dtest =/*Integration**的命令不起作用它显示在父级别运行的0个测试,没有任何测试

Kie*_*ief 232

Maven构建的生命周期现在包括"集成测试"相对于运行集成测试,这是从在"测试"阶段运行单元测试单独运行.它在"package"之后运行,因此如果你运行"mvn verify","mvn install"或"mvn deploy",集成测试将一路运行.

默认情况下,集成测试运行在名为测试类**/IT*.java,**/*IT.java**/*ITCase.java,但可以配置.

有关如何连接所有内容的详细信息,请参阅Failsafe插件,Failsafe使用页面(在我编写此内容时未在上一页中正确链接),还可以查看此Sonatype博客文章.

  • 默认情况下,`mvn integration-test`也运行单元测试(使用via surefire)但是`mvn failsafe:integration-test`只运行故障安全集成测试. (31认同)
  • 令人难以置信的是,Failsafe插件文档,用法页面和FAQ没有提到它运行名为**/IT*.java,**/*IT.java和**/*ITCase.java的测试类...... (19认同)
  • @HennoVermeulen我对于命名测试的内容感到困惑.它在[包含和排除测试]中进行了描述(http://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html).很高兴可以覆盖默认值,但如果他们之前提到默认值,那就太好了. (5认同)
  • @BruceSun:两者都不是,将它们放在 `src/it/java` 下,另请参阅 https://khmarbaise.github.io/maui/it-example-1.html (2认同)

ser*_*g10 96

您可以设置Maven的Surefire来单独运行单元测试和集成测试.在标准单元测试阶段,您运行的所有模式都不符合集成测试.然后,您创建第二个仅运行集成测试的测试阶段.

这是一个例子:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <excludes>
          <exclude>**/*IntegrationTest.java</exclude>
        </excludes>
      </configuration>
      <executions>
        <execution>
          <id>integration-test</id>
          <goals>
            <goal>test</goal>
          </goals>
          <phase>integration-test</phase>
          <configuration>
            <excludes>
              <exclude>none</exclude>
            </excludes>
            <includes>
              <include>**/*IntegrationTest.java</include>
            </includes>
          </configuration>
        </execution>
      </executions>
    </plugin>
Run Code Online (Sandbox Code Playgroud)

  • 您应该使用Fail-safe插件进行集成测试,而不是使用sure-fire插件.在整合后阶段完成之后__之后,它不会失败.允许您在构建失败之前拆除测试资源(例如Web服务器).因此,故障安全. (59认同)
  • 此答案已过时,应予以更新或删除. (4认同)

HDa*_*ave 60

我已经完成了你想要做的事情并且效果很好.单元测试"*Tests"始终运行,"*IntegrationTests"仅在执行mvn verify或mvn安装时运行.这是我POM的片段.serg10几乎是正确的....但不完全.

  <plugin>
    <!-- Separates the unit tests from the integration tests. -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
       <!-- Skip the default running of this plug-in (or everything is run twice...see below) -->
       <skip>true</skip>
       <!-- Show 100% of the lines from the stack trace (doesn't work) -->
       <trimStackTrace>false</trimStackTrace>
    </configuration>
    <executions>
       <execution>
          <id>unit-tests</id>
          <phase>test</phase>
          <goals>
             <goal>test</goal>
          </goals>
          <configuration>
                <!-- Never skip running the tests when the test phase is invoked -->
                <skip>false</skip>
             <includes>
                   <!-- Include unit tests within integration-test phase. -->
                <include>**/*Tests.java</include>
             </includes>
             <excludes>
               <!-- Exclude integration tests within (unit) test phase. -->
                <exclude>**/*IntegrationTests.java</exclude>
            </excludes>
          </configuration>
       </execution>
       <execution>
          <id>integration-tests</id>
          <phase>integration-test</phase>
          <goals>
             <goal>test</goal>
          </goals>
          <configuration>
            <!-- Never skip running the tests when the integration-test phase is invoked -->
             <skip>false</skip>
             <includes>
               <!-- Include integration tests within integration-test phase. -->
               <include>**/*IntegrationTests.java</include>
             </includes>
          </configuration>
       </execution>
    </executions>
  </plugin>
Run Code Online (Sandbox Code Playgroud)

祝好运!

  • 然后随意检查这个响应的方框就是答案! (2认同)
  • 这应该是公认的答案。相关的 maven 目标是:`clean compile integration-test -Dmaven.test.failure.ignore=false` (2认同)

小智 29

您可以使用JUnit类别和Maven轻松拆分它们.
下面通过拆分单元和集成测试非常简单地展示了这一点.

定义标记接口

使用类别对测试进行分组的第一步是创建标记接口.
此接口将用于标记要作为集成测试运行的所有测试.

public interface IntegrationTest {}
Run Code Online (Sandbox Code Playgroud)

标记您的测试类

将类别注释添加到测试类的顶部.它采用新界面的名称.

import org.junit.experimental.categories.Category;

@Category(IntegrationTest.class)
public class ExampleIntegrationTest{

    @Test
    public void longRunningServiceTest() throws Exception {
    }

}
Run Code Online (Sandbox Code Playgroud)

配置Maven单元测试

这个解决方案的优点在于单元测试方面没有任何改变.
我们只是在maven surefire插件中添加一些配置,使其忽略任何集成测试.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.11</version>
    <configuration>
        <includes>
            <include>**/*.class</include>
        </includes>
        <excludedGroups>
            com.test.annotation.type.IntegrationTest
        </excludedGroups>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

当你进行mvn clean测试时,只会运行未标记的单元测试.

配置Maven集成测试

同样,这个配置非常简单.
我们使用标准的failsafe插件并将其配置为仅运行集成测试.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <includes>
            <include>**/*.class</include>
        </includes>
        <groups>
            com.test.annotation.type.IntegrationTest
        </groups>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

配置使用标准执行目标在构建的集成测试阶段运行failsafe插件.

您现在可以执行mvn clean install
这次以及运行的单元测试,集成测试在集成测试阶段运行.

  • 这仅适用于标记接口已存在于Maven可用的某个位置.如果您的标记接口存在于同一多模块构建的另一个模块中,则它不起作用. (4认同)

Jam*_*ery 15

你应该尝试使用maven failsafe插件.您可以告诉它包含一组特定的测试.

  • 墓地页面只是说```failsafe```插件已被移动到```maven-failsafe-plugin```.看起来```maven-failsafe-plugin```仍然活跃(文档最后推到2014年3月). (9认同)

Liq*_*pie 15

您应该使用maven surefire 插件来运行单元测试和maven failsafe 插件来运行集成测试。

如果您希望使用标志来切换这些测试的执行,请遵循以下步骤。

Maven 配置

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>${skipUnitTests}</skipTests>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <configuration>
                <includes>
                    <include>**/*IT.java</include>
                </includes>
                <skipTests>${skipIntegrationTests}</skipTests>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <properties>
            <skipTests>false</skipTests>
            <skipUnitTests>${skipTests}</skipUnitTests>
            <skipIntegrationTests>${skipTests}</skipIntegrationTests>
        </properties>
Run Code Online (Sandbox Code Playgroud)

因此,将根据以下标志规则跳过或切换测试:

可以通过以下标志跳过测试:

  • -DskipTests 跳过单元测试和集成测试
  • -DskipUnitTests 跳过单元测试但执行集成测试
  • -DskipIntegrationTests 跳过集成测试但执行单元测试

运行测试

在下面运行以执行单元测试

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>${skipUnitTests}</skipTests>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <configuration>
                <includes>
                    <include>**/*IT.java</include>
                </includes>
                <skipTests>${skipIntegrationTests}</skipTests>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <properties>
            <skipTests>false</skipTests>
            <skipUnitTests>${skipTests}</skipUnitTests>
            <skipIntegrationTests>${skipTests}</skipIntegrationTests>
        </properties>
Run Code Online (Sandbox Code Playgroud)

您可以执行以下命令来运行测试(单元和集成)

mvn clean test
Run Code Online (Sandbox Code Playgroud)

为了运行集成测试,请遵循

mvn clean verify
Run Code Online (Sandbox Code Playgroud)

或者跳过单元测试

mvn failsafe:integration-test
Run Code Online (Sandbox Code Playgroud)

此外,为了在 期间跳过集成测试mvn install,请遵循

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

您可以使用跳过所有测试

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


cle*_*tus 13

默认情况下,Maven仅运行在类名称中具有Test的测试.

重命名为IntegrationTest,它可能会工作.

或者,您可以更改Maven配置以包含该文件,但只是将测试命名为SomethingTest可能更容易也更好.

包含和排除测试:

默认情况下,Surefire插件将自动包含具有以下通配符模式的所有测试类:

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

如果测试类不符合命名约定,则配置Surefire插件并指定要包含的测试.


Jor*_*rge 9

使用Maven运行集成测试的另一种方法是使用配置文件功能:

...
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
                <excludes>
                    <exclude>**/*IntegrationTest.java</exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>integration-tests</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <includes>
                            <include>**/*IntegrationTest.java</include>
                        </includes>
                        <excludes>
                            <exclude>**/*StagingIntegrationTest.java</exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
...
Run Code Online (Sandbox Code Playgroud)

运行'mvn clean install'将运行默认构建.如上所述,集成测试将被忽略.运行'mvn clean install -P integration-tests'将包括集成测试(我也忽略了我的分段集成测试).此外,我有一个CI服务器,每晚运行我的集成测试,为此我发出命令'mvn test -P integration-tests'.