Maven单元测试(转)
文章出自:http://www.360doc.com/content/13/0927/15/7304817_317455642.shtml
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.跳过测试
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>有时候可能不仅仅需要跳过测试运行,还要跳过测试代码的编译:
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用户能够在命令行指定要运行的测试用例。如:
mvn test -Dtest=RandomGeneratorTest
也可以使用通配符:
mvn test -Dtest=Random*Test
或者也可以使用“,”号指定多个测试类:
mvn test -Dtest=Random*Test,AccountCaptchaServiceTest
如果没有指定测试类,那么会报错并导致构建失败。
mvn test -Dtest
这时候可以添加-DfailIfNoTests=false参数告诉maven-surefire-plugin即使没有任何测试也不要报错。
mvn test -Dtest -DfailIfNoTests=false
由此可见,命令行参数-Dtest -DfailIfNoTests=false是另外一种路过测试的方法
4.包含与排除测试用例
<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.生成测试报告
5.1基本测试报告
- 简单文本格式——内容十分简单,可以看出哪个测试项出错。
- 与JUnit兼容的XML格式——XML格式已经成为了Java单元测试报告的事实标准,这个文件可以用其他的工具如IDE来查看。
5.2测试覆盖率报告
mvn cobertura:cobertura
6.运行TestNG测试
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.9</version>
<scope>test</scope>
<classifier>jdk15</classifier>
</dependency>| JUnit类 | TestNG类 | 作用 |
| org.junit.Test | org.testng.annotations.Test | 标注方法为测试方法 |
| org.junit.Assert | org.testng.Assert | 检查测试结果 |
| org.junit.Before | org.testng.annotations.BeforeMethod | 标注方法在每个测试方法之前运行 |
| org.junit.After | org.testng.annotations.AfterMethod | 标注方法在每个测试方法之后运行 |
| org.junit.BeforeClass | org.testng.annotations.BeforeClass | 标注方法在所有测试方法之前运行 |
| org.junit.AfterClass | org.testng.annotations.AfterClass | 标注方法在所有测试方法之后运行 |
TestNG允许用户使用一个名为testng.xml的文件来配置想要运行的测试集合。如在类路径上添加testng.xml文件,配置只运行RandomGeneratorTest
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite1" verbose="1">
<test name="Regression1">
<classes>
<class name="com.juvenxu.mvnbook.account.captcha.RandomGeneratorTest" />
</classes>
</test>
</suite>同时再配置maven-surefire-plugin使用该testng.xml,如:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>TestNG较JUnit的一大优势在于它支持测试组的概念。如可以在方法级别声明测试组:
@Test(groups={"util","medium"})然后可以在pom中配置运行一个或多个测试组:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<groups>util,medium</groups>
</configuration>
</plugin>7.重用测试代码
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>现在如要引用test-jar生成的测试代码包,可以如下配置:
<dependency>
<groupId>com.juvenxu.mvnbook.account</groupId>
<artifactId>account-captcha</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>