用法

要使用故障安全插件,您需要将以下配置添加到您的 pom.xml

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>integration-test</id>
            <goals>
              <goal>integration-test</goal>
            </goals>
          </execution>
          <execution>
            <id>verify</id>
            <goals>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

然后可以通过调用构建生命周期的验证阶段来调用 Failsafe Plugin。

mvn verify

使用不同的测试提供者

测试源目录中的测试可以是以下任意组合:

  • 测试NG
  • JUnit(3.8 或 4.x)
  • POJO

哪些提供程序可用仅通过包含适当的依赖项来控制(即,junit:junit 用于 JUnit,org.testng:testng 4.7+ 用于 TestNG)。由于无论如何编译测试类都需要这样做,因此不需要额外的配置。

请注意,无论使用哪个提供程序,任何正常的 Surefire 集成都可以正常工作 - 例如,您仍然可以在项目网站上为您的 TestNG 测试生成 Cobertura 报告和 Surefire 结果报告。

上面的 POJO 提供程序允许您编写不依赖于 JUnit 的测试。它们的行为方式相同,运行类中公开的所有test*方法,但不需要 API 依赖项。要执行断言,可以使用 JDK 1.4 assert关键字,或者您可以使用org.apache.maven.surefire.assertion.Assert

所有提供程序都支持 Surefire 插件参数配置。但是,如果您正在运行 TestNG 测试(包括是否使用 TestNG 执行 JUnit 测试,如果两者都存在于 Surefire 中,则默认情况下会出现这种情况),还有其他可用选项。

有关更多信息,请参阅使用 TestNG

使用码头和 maven-failsafe-plugin

您需要将jetty:runjetty:run-explodedjetty:run-war之一绑定到pre-integration-test阶段,并将deamon设置为 true,将failsafe:integration-test绑定到integration-test阶段,绑定jetty :stoppost-integration-test阶段,最后将failsafe:verify绑定到verify阶段。这是一个例子:

<project>
  [...]
  <build>
    [...]
    <plugins>
      [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>integration-test</id>
            <goals>
              <goal>integration-test</goal>
            </goals>
          </execution>
          <execution>
            <id>verify</id>
            <goals>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.16</version>
        [...]
        <configuration>
          [...]
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <stopPort>8005</stopPort>
          <stopKey>STOP</stopKey>
          <contextPath>/</contextPath>
          [...]
        </configuration>
        [...]
        <executions>
          [...]
          <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>run-exploded</goal>
            </goals>
            <configuration>
              <scanIntervalSeconds>0</scanIntervalSeconds>
              <daemon>true</daemon>
            </configuration>
          </execution>
          <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>stop</goal>
            </goals>
          </execution>
          [...]
        </executions>
        [...]
      </plugin>
      [...]
    </plugins>
    [...]
  </build>
  [...]
</project>

然后,您使用验证阶段或稍后调用 maven以运行集成测试。不要直接调用任何阶段:pre-integration-testintegration-testpost-integration-test,因为它们太长而无法输入,它们可能会使码头容器运行。

mvn verify

注意:在测试开发期间,您可能会在后台运行一个码头实例。为了帮助运行集成测试,在jetty:run之前将jetty:stop绑定到pre-integration-test阶段会很方便,以便在启动集成测试 jetty 实例之前清除任何正在运行的 jetty 实例,例如

<project>
  [...]
  <build>
    [...]
    <plugins>
      [...]
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.16</version>
        [...]
        <executions>
          [...]
          <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <goals>
              <!-- stop any previous instance to free up the port -->
              <goal>stop</goal>
              <goal>run-exploded</goal>
            </goals>
            [...]
          </execution>
          [...]
        </executions>
        [...]
      </plugin>
      [...]
    </plugins>
    [...]
  </build>
  [...]
</project>

报告集成测试结果

Failsafe Plugin 使用与 Surefire Plugin 完全相同的格式,因此要生成报告,您只需使用 Failsafe 报告目录添加第二个 Surefire Report Plugin 报告集,例如

<project>
  [...]
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.5</version>
        <reportSets>
          <reportSet>
            <id>integration-tests</id>
            <reports>
              <report>report-only</report>
            </reports>
            <configuration>
              <outputName>failsafe-report</outputName>
              <reportsDirectories>
                <reportsDirectory>/home/user/src/surefire/target/checkout/maven-failsafe-plugin/target/failsafe-reports</reportsDirectory>
              </reportsDirectories>
            </configuration>
        </reportSet>
      </plugin>
    </plugins>
  </reporting>
  [...]
</project>