Maven本身并不是一个单元测试框架,它只是在构建执行到特定生命周期阶段的时候,通过插件来执行JUnit或者TestNG的测试用例。这个插件就是maven-surefire-plugin,也可以称为测试运行器(Test Runner),它能兼容JUnit 3、JUnit 4以及TestNG。
在默认情况下,maven-surefire-plugin的test目标会自动执行测试源码路径(默认为src/test/java/)下所有符合一组命名模式的测试类。这组模式为:
mvn package -DskipTests
<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
<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>
mvn test -Dtest=RandomGeneratorTest
mvn test -Dtest=Random*Test
mvn test -Dtest=Random*Test,AccountCaptchaServiceTest
mvn test -Dtest
mvn test -Dtest -DfailIfNoTests=false
<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>
mvn cobertura:cobertura
<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 | 标注方法在所有测试方法之后运行 |
<?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>
<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>
@Test(groups={"util","medium"})
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.5</version> <configuration> <groups>util,medium</groups> </configuration> </plugin>
<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>maven-jar-plugin有两个目标,分别为jar和test-jar。这两个目标都默认绑定到default生命周期的package阶段运行,只是test-jar并没有在超级POM中配置,因此需要我们另外在pom中配置。
<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>