用法
要使用故障安全插件,您需要将以下配置添加到您的pom.xml
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.11</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins> </build> [...] </project>
可以通过调用构建生命周期的验证阶段来调用故障安全插件。
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关键字。有关更多信息,请参阅使用 POJO 测试。
所有提供程序都支持 Surefire 插件参数配置。但是,如果您正在运行 TestNG 测试(包括如果您使用 TestNG 来执行 JUnit 测试,如果两者都存在于 Surefire 中,则默认情况下会发生这种情况)。
有关更多信息,请参阅使用 TestNG。
使用码头和 maven-failsafe-plugin
您需要将jetty:run、jetty:run-exploded或jetty:run-war之一绑定到pre-integration-test阶段,并将deamon设置为 true,将failsafe:integration-test绑定到integration-test阶段,绑定jetty :stop到post-integration-test阶段,最后将failsafe:verify绑定到verify阶段。这是一个例子:
<project> [...] <build> [...] <plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.11</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-test、integration-test或post-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 插件使用与 Surefire 插件完全相同的格式,因此要生成报告,您只需使用 Failsafe 报告目录添加第二个 Surefire 报告插件报告集,例如
<project> [...] <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-report-plugin</artifactId> <version>2.11</version> <reportSets> <reportSet> <id>integration-tests</id> <reports> <report>failsafe-report-only</report> </reports> </reportSet> </plugin> </plugins> </reporting> [...] </project>
多次运行集成测试
如果您需要多次运行集成测试,只需使用集成测试目标的多次执行。您需要为每次执行指定不同的摘要文件,然后使用多个摘要文件配置验证目标,以便在任何一次执行失败时使构建失败,例如
<project> [...] <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.11</version> <executions> <execution> <id>integration-test-red-bevels</id> <goals> <goal>integration-test</goal> </goals> <configuration> <systemPropertyVariables> <bevels>red</bevels> </systemPropertyVariables> <summaryFile>target/failsafe-reports/failsafe-summary-red-bevels.xml</summaryFile> </configuration> </execution> <execution> <id>integration-test-no-bevels</id> <goals> <goal>integration-test</goal> </goals> <configuration> <systemPropertyVariables> <bevels>none</bevels> </systemPropertyVariables> <summaryFile>target/failsafe-reports/failsafe-summary-no-bevels.xml</summaryFile> </configuration> </execution> <execution> <id>verify</id> <goals> <goal>verify</goal> </goals> <configuration> <summaryFiles> <summaryFile>target/failsafe-reports/failsafe-summary-red-bevels.xml</summaryFile> <summaryFile>target/failsafe-reports/failsafe-summary-no-bevels.xml</summaryFile> </summaryFiles> </configuration> </execution> </executions> </plugin> </plugins> </reporting> [...] </project>
在多模块项目中使用
本页顶部列出的使用故障安全插件的建议适用于 95% 的用例。当您在父 pom 中定义故障安全插件的共享定义时,定义执行 ID 以允许子项目覆盖配置被认为是最佳实践。因此,您的父pom.xml中可能有以下内容
<project> [...] <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.11</version> <executions> <execution> <id>integration-test</id> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> </build> [...] </project>
然后子项目可以触发故障安全插件的使用
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.11</version> </plugin> </plugins> </build> [...] </project>
对于非常复杂的构建,最好将集成测试和验证目标的执行分开。
<project> [...] <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.11</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> </pluginManagement> </build> [...] </project>