1.maven-surefire-plugin简介
Maven本身并不是一个单元测试框架,它只是在构建执行到特定生命周期阶段的时候,通过插件来执行JUnit或者TestNG的测试用例。这个插件就是maven-surefire-plugin,也可以称为测试运行器(Test Runner),它能兼容JUnit 3、JUnit 4以及TestNG。
在默认情况下,maven-surefire-plugin的test目标会自动执行测试源码路径(默认为src/test/java/)下所有符合一组命名模式的测试类。这组模式为:
- **/Test*.java:任何子目录下所有命名以Test开关的Java类。
- **/*Test.java:任何子目录下所有命名以Test结尾的Java类。
- **/*TestCase.java:任何子目录下所有命名以TestCase结尾的Java类。
2.跳过测试
要想跳过测试,在命令行加入参数skipTests就可以了。如:
[plain] view plaincopyprint?
- mvn package -DskipTests
也可以在pom配置中提供该属性。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
有时候可能不仅仅需要跳过测试运行,还要跳过测试代码的编译:
- mvn package -Dmaven.test.skip=true
也可以在pom中配置maven.test.skip:
<plugin>
<groupId>org.apache.maven.plugin</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>
3.动态指定要运行的测试用例
maven-surefire-plugin提供了一个test参数让Maven用户能够在命令行指定要运行的测试用例。如:
[plain] view plaincopyprint?
- mvn test -Dtest=RandomGeneratorTest
也可以使用通配符:
[plain] view plaincopyprint?
- mvn test -Dtest=Random*Test
或者也可以使用“,”号指定多个测试类:
[plain] view plaincopyprint?
- mvn test -Dtest=Random*Test,AccountCaptchaServiceTest
如果没有指定测试类,那么会报错并导致构建失败。
[plain] view plaincopyprint?
- mvn test -Dtest
这时候可以添加-DfailIfNoTests=false参数告诉maven-surefire-plugin即使没有任何测试也不要报错。
[plain] view plaincopyprint?
- mvn test -Dtest -DfailIfNoTests=false
由此可见,命令行参数-Dtest -DfailIfNoTests=false是另外一种路过测试的方法
4.包含与排除测试用例
如果由于历史原因,测试类不符合默认的三种命名模式,可以通过pom.xml设置maven-surefire-plugin插件添加命名模式或排除一些命名模式。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
<excludes>
<exclude>**/*ServiceTest.java</exclude>
<exclude>**/TempDaoTest.java</exclude>
</excludes>
</configuration>
</plugin>
5.生成测试覆盖率
cobertura-maven-plugin提供单元测试整体覆盖率以及分支覆盖率的统计工具,给编写单元测试的开发者提供一个参考,在pom.xml的配置如下:
<build>
<plugins>
<!-- 单元测试覆盖率插件 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<encoding>UTF-8</encoding>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
</plugins>
</build>
在项目目录下运行mvn cobertura:cobertura即可生成target目录下生成site\cobertura目录,里面存放了所有的单元测试报告,组织形式如javadoc.其中index.html对所有包的覆盖率做了统计
cobertura-maven-plugin还提供了mvn cobertura:check命令来在verify阶段查看代码覆盖率是否达到预设要求,给项目管理者提供了一个检查代码覆盖率的工具。
maven给管理者一套监控项目过程中代码质量的统计工具,也给开发者提供了衡量并改进代码测试的工具,提供了开发效率。