一、插件简介
- 查看surefire插件的详细参数
mvn surefire:help -Ddetail=true
- 默认的测试类
"**/Test*.java", "**/*Test.java", "**/*Tests.java", "**/*TestCase.java"
- 测试报告默认的路径
${basedir}/target/surefire-reports
二、常用场景
- 命令行指定执行单个testCase
可以直接省略包路径
mvn test -Dtest=com.wsl.my.maven.tests.SuffixTestCase
mvn test -Dtest=SuffixTestCase
- 命令行指定执行单个testCase
mvn test -Dtest=SuffixTestCase,SuffixTest
- 直接跑测试,不跑其他前置的mvn执行周期(compile/resouce/)
mvn surefire:test -Dtest=SuffixTestCase
- 命令执行指定方法
mvn test -Dtest=SuffixTestCase#base
- 命令执行多个方法
mvn test -Dtest=SuffixTestCase#base,base2
- 跳过test
仅跳过测试执行,会执行testResource和testCompile
mvn install -DskipTests
对应xml形式
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
全部跳过测试相关阶段,包括testResource和testCompile和test
mvn install -Dmaven.test.skip=true
- 某个Test测试未通过后,忽略后续的测试不再执行
默认全部会执行,最后统计执行情况。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<skipAfterFailureCount>1</skipAfterFailureCount>
</configuration>
</plugin>
- 执行和排除表达式对应的测试
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<includes>
<include>Sample.java</include>
<include>**/Test*.java</include>
</includes>
<excludes>
<exclude>**/TestCircle.java</exclude>
<exclude>**/TestSquare.java</exclude>
</excludes>
</configuration>
</plugin>
通过正则表达式
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<includes>
<include>%regex[.*(Cat|Dog).*Test.*]</include>
</includes>
</configuration>
</plugin>
- 执行依赖包的测试
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<dependenciesToScan>
<dependency>org.acme:project-a</dependency>
<dependency>org.acme:project-b:test-jar</dependency>
<dependency>org.acme:project-c:*:tests-jdk15</dependency>
</dependenciesToScan>
</configuration>
</plugin>
三、结合junit4
- 和category一起使用
在测试类上标明category分组,然后配置只有指定的group才需要测试
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<groups>com.mycompany.SlowTests</groups>
</configuration>
</plugin>
[...]
</plugins>
public interface SlowTests{}
public interface SlowerTests extends SlowTests{}
public class AppTest {
@Test
@Category(com.mycompany.SlowTests.class)
public void testSlow() {
System.out.println("slow");
}
@Test
@Category(com.mycompany.SlowerTests.class)
public void testSlower() {
System.out.println("slower");
}
@Test
@Category(com.mycompany.FastTests.class)
public void testSlow() {
System.out.println("fast");
}
}