1. maven-surefire-plugin 简介
default生命周期test阶段内置绑定maven-surefire-plugin的test目标。
maven-surefire-plugin的test目标会自动执行测试源代码路径,测试类命名模式:
**/Test*.java: 子目录下所有命名以Test开头的Java类。
**/*Test.java: ....
**/*TestCase.java: ....
2. 跳过测试
2.1 命令行模式
$mvnpackage -DskipTests
2.2 POM文件中配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
3 跳过测试和测试代码编译
3.1 命令行模式
$mvnpackage -Dmaven.test.skip=tue
3.2 POM文件中配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
3. 动态指定要运行的测试用例
maven-surefire-plugin:test的参数test指定
$mvntest -Dtest=RandonGeneratorTest
$mvntest -Dtest=Randon*Test
$mvntest -Dtest=RandonGeneratorTest, RandonGeneratorTestCase
$mvntest -Dtest=Randon*Test, RandonGeneratorTestCase
test参数的值必须匹配一个或者多个测试类。
$mvntest -Dtest 会显示构建失败
使用 -DfailIfNoTests=false 即时没指定测试类也不要报错(另外一种跳过测试方式)
$mvntest -Dtest -DfailIfNoTests=false
5. 生成测试报告
cobertura: cobertura
绑定到test
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
<executions>
<execution>
<id>cobertura-report</id>
<phase>test</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
4. 包含与排除测试用例
自动运行以Tests结尾的测试类:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
排除测试类:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<excludes>
<exclude>**/*ServiceTest.java</exclude>
<exclude>**/TmpDapTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
学习资料:《MAVEN实战》