Maven不同单元测试如何从集成测试?

bra*_*orm 14 integration-testing unit-testing maven

这是我的项目结构mvn:

在此输入图像描述

你可以注意到,我有两节课 src/test/java

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

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

我跑的时候

mvn package

我注意到只有unit-test (CardValidatorTest.java)运行

但是当我跑步的时候

mvn integration-test

我看到了两个unit-test并且Integration tests正在运行.

怎么mvn知道CardValidatorIT.java在我跑的时候不执行mvn package.也就是说,为什么它没有运行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>
Run Code Online (Sandbox Code Playgroud)

PS:我知道我integration-test在mvn中有一个目标.但我没有把目标放在集成测试期间应该运行哪个类

谢谢

小智 18

见Maven Surefire.

这个插件负责mvn testMaven.默认配置正在发挥作用.这意味着Test当你跑步mvn test和你的情况下,带有单词的班级将发挥作用mvn package

http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

当您运行mvn integration-test故障安全插件时使用.它的默认包含/排除规则不同 - 默认情况下,它会查找单词IT.

http://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html

注意:对我来说,CardValidatorTest运行时检测类是很奇怪的mvn integration-test.基于我如何阅读failsafe-plugin的默认包含和排除规则,我不希望这样.事实上,当我适应pom.xml我自己的示例项目时,我没有看到这种行为.相反,所有Test课程都用mvn testmvn package.所有IT课程都会被选中mvn integration-test.你确定你没有对这两个类有一些代码级依赖吗?除了更改的包含/排除规则之外,这是我能想到的唯一可能导致它们被提取mvn test或用于获取的内容mvn package.


Vid*_*dya 5

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".
Run Code Online (Sandbox Code Playgroud)

Failsafe不是Maven 默认生命周期绑定的一部分,因此集成测试CardValidatorIT(当然,它满足默认模式)不会作为生命周期的一部分运行.这得到了Maven的看法惯例,其中失败应该使构建失败以及哪些测试应该一直运行(具有宽代码覆盖的快速单元测试)与哪些测试应该较少运行(慢速集成测试).

当然,您可以根据需要覆盖约定.