Java 如何让我的 Maven 集成测试运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1399240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:39:57  来源:igfitidea点击:

How do I get my Maven Integration tests to run

javamaven-2testingsurefire

提问by Peter Delaney

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

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

mvn test

mvn test

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

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

mvn test -Dtest=**/*Integration

mvn test -Dtest=**/*Integration

none of the Integration.javatests get execute within the child modules.

没有任何Integration.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 个测试,其中没有任何测试

采纳答案by serg10

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

回答by cletus

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

默认情况下,Maven 只运行在类名中包含 Test 的测试。

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:

  • "**/Test*.java" - includes all of its subdirectory and all java filenames that start with "Test".
  • "**/*Test.java" - includes all of its subdirectory and all java filenames that end with "Test".
  • "**/*TestCase.java" - includes all of its subdirectory and all java filenames that end with "TestCase".

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

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

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

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

回答by James Kingsbery

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

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

回答by HDave

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 verify 或 mvn install 时运行。这是我的 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!

祝你好运!

回答by Kief

The Maven build lifecyclenow 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构建的生命周期现在包括“集成测试”相对于运行集成测试,这是从在“测试”阶段运行单元测试单独运行。它在“package”之后运行,因此如果您运行“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**/*IT.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 使用页面(在我撰写本文时未从上一页正确链接),并查看此 Sonatype 博客文章

回答by Jorge

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 install'将运行默认构建。如上所述,集成测试将被忽略。运行'mvn clean install -P integration-tests'将包括集成测试(我也忽略了我的登台集成测试)。此外,我有一个 CI 服务器,它每晚运行我的集成测试,为此我发出命令'mvn test -P integration-tests'

回答by John Dobie

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

定义一个标记接口

使用类别对测试进行分组的第一步是创建标记界面。


此接口将用于标记您希望作为集成测试运行的所有测试。


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

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

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

当您执行 a 时mvn clean test,只会运行您未标记的单元测试。

Configure Maven Integration Tests

配置 Maven 集成测试

同样,此配置非常简单。


我们使用标准的故障安全插件并将其配置为仅运行集成测试。


<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 clean install.
这次以及单元测试运行,集成测试在集成测试阶段运行。

回答by Dherik

You can follow the maven documentationto 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