如何让Maven集成测试运行

时间:2023-01-14 12:18:17

I have a maven2 multi-module project and in each of my child modules I have JUnit tests that are named Test.java and Integration.java for unit tests and integration tests respectively. When I execute:

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

mvn test

mvn测试

all of the JUnit tests *Test.java within the child modules are executed. When I execute

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

mvn test -Dtest=**/*Integration

mvn -Dtest = * * / *集成测试

none of the Integration.java tests get execute within the child modules.

所有的集成。java测试在子模块中执行。

These seem like the exact same command to me but the one with the -Dtest=/*Integration** does not work it displays 0 tests being run at the parent level, which there are not any tests

这些对我来说似乎是完全相同的命令,但是带有-Dtest=/*Integration**的命令不能工作,它显示在父级运行0个测试,没有任何测试

8 个解决方案

#1


84  

You can set up Maven's Surefire to run unit tests and integration tests separately. In the standard unit test phase you run everything that does not pattern match an integration test. You then create a second test phase that runs just the integration tests.

您可以设置Maven的Surefire来分别运行单元测试和集成测试。在标准单元测试阶段,运行所有与集成测试不匹配的模式。然后创建第二个测试阶段,该阶段只运行集成测试。

Here is an example:

这是一个例子:

    <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>

#2


197  

The Maven build lifecycle now includes the "integration-test" phase for running integration tests, which are run separately from the unit tests run during the "test" phase. It runs after "package", so if you run "mvn verify", "mvn install", or "mvn deploy", integration tests will be run along the way.

Maven构建生命周期现在包括运行集成测试的“集成测试”阶段,这些集成测试与“测试”阶段运行的单元测试是分开运行的。它在“包”之后运行,因此如果您运行“mvn verify”、“mvn install”或“mvn deploy”,那么集成测试将在此过程中运行。

By default, integration-test runs test classes named **/IT*.java, **/*IT.java, and **/*ITCase.java, but this can be configured.

默认情况下,集成测试运行名为**/IT*的测试类。java,* * / *。java和* * / * ITCase。java,但是可以配置它。

For details on how to wire this all up, see the Failsafe plugin, the Failsafe usage page (not correctly linked from the previous page as I write this), and also check out this Sonatype blog post.

有关如何将这些连接起来的详细信息,请参阅Failsafe插件、Failsafe usage页面(在我写这篇文章时,不会正确地从上一页链接过来),并查看Sonatype博客文章。

#3


54  

I have done EXACTLY what you want to do and it works great. Unit tests "*Tests" always run, and "*IntegrationTests" only run when you do a mvn verify or mvn install. Here it the snippet from my POM. serg10 almost had it right....but not quite.

我已经做了你想做的,而且效果很好。单元测试“* tests”始终运行,“*IntegrationTests”只在进行mvn验证或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>

Good luck!

好运!

#4


23  

You can split them very easily using JUnit categories and Maven.
This is shown very, very briefly below by splitting unit and integration tests.

使用JUnit类别和Maven,可以很容易地将它们分开。下面将通过分解单元和集成测试非常、非常简短地展示这一点。

Define A Marker Interface

The first step in grouping a test using categories is to create a marker interface.
This interface will be used to mark all of the tests that you want to be run as integration tests.

public interface IntegrationTest {}

Mark your test classes

Add the category annotation to the top of your test class. It takes the name of your new interface.

将类别注释添加到测试类的顶部。它使用新接口的名称。

import org.junit.experimental.categories.Category;
@Category(IntegrationTest.class)
public class ExampleIntegrationTest{
@Test
public void longRunningServiceTest() throws Exception {
 }
}

Configure Maven Unit Tests

The beauty of this solution is that nothing really changes for the unit test side of things.
We simply add some configuration to the maven surefire plugin to make it to ignore any integration tests.

<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>

When you do a mvn clean test only your unmarked unit tests will run.

当您进行mvn清洁测试时,只会运行未标记的单元测试。

Configure Maven Integration Tests

Again the configuration for this is very simple.
We use the standard failsafe plugin and configure it to only run the integration tests.

<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>

The configuration uses a standard execution goal to run the failsafe plugin during the integration-test phase of the build.

配置使用一个标准的执行目标,在构建的集成测试阶段运行故障安全插件。

You can now do a mvn clean install
This time as well as the unit tests running, the integration tests are run during the integration-test phase.

现在,您可以执行一次mvn清理安装以及单元测试的运行,集成测试在集成测试阶段运行。

#5


15  

You should try using maven failsafe plugin. You can tell it to include a certain set of tests.

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

#6


12  

By default, Maven only runs tests that have Test somewhere in the class name.

默认情况下,Maven只运行类名中某处有测试的测试。

Rename to IntegrationTest and it'll probably work.

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

Alternatively you can change the Maven config to include that file but it's probably easier and better just to name your tests SomethingTest.

另外,您也可以更改Maven配置以包含该文件,但将测试命名为SomethingTest可能会更容易、更好。

From Inclusions and Exclusions of Tests:

从包含和排除测试:

By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:

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

  • "**/Test*.java" - includes all of its subdirectory and all java filenames that start with "Test".
  • “测试* * * /。包括所有子目录和所有以“Test”开头的java文件名。
  • "**/*Test.java" - includes all of its subdirectory and all java filenames that end with "Test".
  • “* * / *测试。包括所有子目录和以“Test”结尾的所有java文件名。
  • "**/*TestCase.java" - includes all of its subdirectory and all java filenames that end with "TestCase".
  • “* * / * TestCase。包括所有子目录和以“TestCase”结尾的所有java文件名。

If the test classes does not go with the naming convention, then configure Surefire Plugin and specify the tests you want to include.

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

#7


9  

Another way of running integration tests with Maven is to make use of the profile feature:

使用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>
...

Running 'mvn clean install' will run the default build. As specified above integration tests will be ignored. Running 'mvn clean install -P integration-tests' will include the integration tests (I also ignore my staging integration tests). Furthermore, I have a CI server that runs my integration tests every night and for that I issue the command 'mvn test -P integration-tests'.

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

#8


0  

You can follow the maven documentation to run the unit tests with the build and run the integration tests separately.

您可以遵循maven文档,使用构建运行单元测试,并分别运行集成测试。

<project>
    <properties>
        <skipTests>true</skipTests>
    </properties>
    [...]
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                    <skipITs>${skipTests}</skipITs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    [...]
</project>

This will allow you to run with all integration tests disabled by default. To run them, you use this command:

这将允许您在默认情况下运行所有的集成测试。要运行它们,可以使用以下命令:

mvn install -DskipTests=false

#1


84  

You can set up Maven's Surefire to run unit tests and integration tests separately. In the standard unit test phase you run everything that does not pattern match an integration test. You then create a second test phase that runs just the integration tests.

您可以设置Maven的Surefire来分别运行单元测试和集成测试。在标准单元测试阶段,运行所有与集成测试不匹配的模式。然后创建第二个测试阶段,该阶段只运行集成测试。

Here is an example:

这是一个例子:

    <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>

#2


197  

The Maven build lifecycle now includes the "integration-test" phase for running integration tests, which are run separately from the unit tests run during the "test" phase. It runs after "package", so if you run "mvn verify", "mvn install", or "mvn deploy", integration tests will be run along the way.

Maven构建生命周期现在包括运行集成测试的“集成测试”阶段,这些集成测试与“测试”阶段运行的单元测试是分开运行的。它在“包”之后运行,因此如果您运行“mvn verify”、“mvn install”或“mvn deploy”,那么集成测试将在此过程中运行。

By default, integration-test runs test classes named **/IT*.java, **/*IT.java, and **/*ITCase.java, but this can be configured.

默认情况下,集成测试运行名为**/IT*的测试类。java,* * / *。java和* * / * ITCase。java,但是可以配置它。

For details on how to wire this all up, see the Failsafe plugin, the Failsafe usage page (not correctly linked from the previous page as I write this), and also check out this Sonatype blog post.

有关如何将这些连接起来的详细信息,请参阅Failsafe插件、Failsafe usage页面(在我写这篇文章时,不会正确地从上一页链接过来),并查看Sonatype博客文章。

#3


54  

I have done EXACTLY what you want to do and it works great. Unit tests "*Tests" always run, and "*IntegrationTests" only run when you do a mvn verify or mvn install. Here it the snippet from my POM. serg10 almost had it right....but not quite.

我已经做了你想做的,而且效果很好。单元测试“* tests”始终运行,“*IntegrationTests”只在进行mvn验证或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>

Good luck!

好运!

#4


23  

You can split them very easily using JUnit categories and Maven.
This is shown very, very briefly below by splitting unit and integration tests.

使用JUnit类别和Maven,可以很容易地将它们分开。下面将通过分解单元和集成测试非常、非常简短地展示这一点。

Define A Marker Interface

The first step in grouping a test using categories is to create a marker interface.
This interface will be used to mark all of the tests that you want to be run as integration tests.

public interface IntegrationTest {}

Mark your test classes

Add the category annotation to the top of your test class. It takes the name of your new interface.

将类别注释添加到测试类的顶部。它使用新接口的名称。

import org.junit.experimental.categories.Category;
@Category(IntegrationTest.class)
public class ExampleIntegrationTest{
@Test
public void longRunningServiceTest() throws Exception {
 }
}

Configure Maven Unit Tests

The beauty of this solution is that nothing really changes for the unit test side of things.
We simply add some configuration to the maven surefire plugin to make it to ignore any integration tests.

<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>

When you do a mvn clean test only your unmarked unit tests will run.

当您进行mvn清洁测试时,只会运行未标记的单元测试。

Configure Maven Integration Tests

Again the configuration for this is very simple.
We use the standard failsafe plugin and configure it to only run the integration tests.

<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>

The configuration uses a standard execution goal to run the failsafe plugin during the integration-test phase of the build.

配置使用一个标准的执行目标,在构建的集成测试阶段运行故障安全插件。

You can now do a mvn clean install
This time as well as the unit tests running, the integration tests are run during the integration-test phase.

现在,您可以执行一次mvn清理安装以及单元测试的运行,集成测试在集成测试阶段运行。

#5


15  

You should try using maven failsafe plugin. You can tell it to include a certain set of tests.

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

#6


12  

By default, Maven only runs tests that have Test somewhere in the class name.

默认情况下,Maven只运行类名中某处有测试的测试。

Rename to IntegrationTest and it'll probably work.

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

Alternatively you can change the Maven config to include that file but it's probably easier and better just to name your tests SomethingTest.

另外,您也可以更改Maven配置以包含该文件,但将测试命名为SomethingTest可能会更容易、更好。

From Inclusions and Exclusions of Tests:

从包含和排除测试:

By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:

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

  • "**/Test*.java" - includes all of its subdirectory and all java filenames that start with "Test".
  • “测试* * * /。包括所有子目录和所有以“Test”开头的java文件名。
  • "**/*Test.java" - includes all of its subdirectory and all java filenames that end with "Test".
  • “* * / *测试。包括所有子目录和以“Test”结尾的所有java文件名。
  • "**/*TestCase.java" - includes all of its subdirectory and all java filenames that end with "TestCase".
  • “* * / * TestCase。包括所有子目录和以“TestCase”结尾的所有java文件名。

If the test classes does not go with the naming convention, then configure Surefire Plugin and specify the tests you want to include.

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

#7


9  

Another way of running integration tests with Maven is to make use of the profile feature:

使用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>
...

Running 'mvn clean install' will run the default build. As specified above integration tests will be ignored. Running 'mvn clean install -P integration-tests' will include the integration tests (I also ignore my staging integration tests). Furthermore, I have a CI server that runs my integration tests every night and for that I issue the command 'mvn test -P integration-tests'.

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

#8


0  

You can follow the maven documentation to run the unit tests with the build and run the integration tests separately.

您可以遵循maven文档,使用构建运行单元测试,并分别运行集成测试。

<project>
    <properties>
        <skipTests>true</skipTests>
    </properties>
    [...]
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                    <skipITs>${skipTests}</skipITs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    [...]
</project>

This will allow you to run with all integration tests disabled by default. To run them, you use this command:

这将允许您在默认情况下运行所有的集成测试。要运行它们,可以使用以下命令:

mvn install -DskipTests=false