Maven如何区分单元测试和集成测试?

17

这是我在mvn中的项目结构:

enter image description here

你可以注意到,我在src/test/java中有两个类:

  1. CardValidtorIT.java(这是集成测试)

  2. CardValidatorTest.java(这是单元测试)

当我运行

mvn package

我发现只运行了unit-test (CardValidatorTest.java)

但是当我运行

mvn integration-test

我看到同时运行了unit-testIntegration tests

mvn如何知道在运行mvn package时不执行CardValidatorIT.java?也就是说,为什么它没有运行CardValidatoryIT.java

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>chapter14</artifactId>
    <groupId>org.agoncal.book.javaee7</groupId>
    <version>1.0</version>
</parent>
<groupId>org.agoncal.book.javaee7.chapter14</groupId> <artifactId>chapter14-service</artifactId> <version>1.0</version>
<packaging>war</packaging>
  <dependencies>
    <dependency>
<groupId>org.glassfish.main.extras</groupId> <artifactId>glassfish-embedded-all</artifactId> <version>4.0</version>
<scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.12.4</version>
<executions>
          <execution>
            <id>integration-test</id>
            <goals>
<goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

P.S:我知道在mvn中有一个integration-test目标,但我没有将该目标与应在集成测试期间运行的类绑定。

谢谢

3个回答

25
请参阅Maven Surefire。此插件负责Maven中的“mvn test”。默认配置将生效。这意味着在运行“mvn test”和您的情况下“mvn package”时,带有单词“Test”的类将生效。
当您运行“mvn integration-test”时,使用failsafe插件。它的默认包含/排除规则不同-例如,默认情况下它会查找“IT”一词。
请注意:当您运行“mvn integration-test”时,我觉得很奇怪测试类CardValidatorTest被捡起来了。根据我对failsafe插件的默认包含和排除规则的理解,我不会期望这种情况发生。实际上,当我将您的pom.xml适应我的示例项目时,我没有看到这种行为。相反,所有"Test"类都会被“mvn test”和“mvn package”捡起来,所有"IT"类都会被“mvn integration-test”捡起来。您确定您没有某些代码级别依赖于这两个类吗?除了更改包含/排除规则之外,我想不到其他可能会导致它们都被“mvn test”或“mvn package”捡起来的事情。
链接:http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.htmlhttp://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html

我确定我的代码没有代码级别的依赖。mvn test 只运行了一个测试(cardValidatorTest), mvn package 仅运行了一个测试(CardValidatorTest),但是 mvn integration-test 运行了两个测试(CardValidatorTest 和 CardValidatorIT)。但你的答案和链接都帮助了我!谢谢 - brain storm
Maven如何在pom文件中没有failsafe插件的情况下知道哪些测试是集成测试?即使项目的pom文件中没有failsafe插件,这些测试也会被执行。项目的pom文件只有一个spring-boot-starter-parent作为其父项目,并且集成测试使用了*IntegrationTest后缀(这不是默认的后缀)。 - undefined

6
在Maven中,Failsafe插件处理集成测试。默认情况下,这些是用于集成测试的包含模式
"**/IT*.java" - includes all of its subdirectories and all java filenames that start with "IT".
"**/*IT.java" - includes all of its subdirectories and all java filenames that end with "IT".
"**/*ITCase.java" - includes all of its subdirectories and all java filenames that end with "ITCase".

Failsafe不是Maven的默认生命周期绑定的一部分,因此像CardValidatorIT这样的集成测试,当然符合默认模式,不会作为生命周期的一部分运行。这涉及到Maven对哪些失败应该导致构建失败以及哪些测试应该始终运行(具有广泛代码覆盖率的快速单元测试)的看法和约定,以及哪些测试应该较少运行(慢速集成测试)。

当然,您可以根据需要覆盖惯例。


这些文件需要放在 src/test/java/IT**.java 中吗?这样行吗?src/main/java/IT**.java。这可能会与我的类名称模式为 IT**.java 的类发生命名冲突。 - brain storm
我从未尝试过,但我认为您可以通过向插件添加“配置”元素来控制在生命周期的不同部分包含/排除哪些内容。话虽如此,这样做会给自己带来很多麻烦,我认为这并不值得。 - Vidya
我更喜欢使用src/test/java/下的surefire-plugin和src/integration-test/java/下的failsafe-plugin来进行单元测试和集成测试。我们该怎么做? - fjjiaboming

2

集成测试构建阶段在打包构建阶段之后。因此,“mvn package”将无法到达“integration-test”构建阶段。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接