Maven执行指定的测试类或方法,需要使用test属性。
运行测试类com.mytest.TestCircle
mvn -Dtest=com.mytest.TestCircle test 运行所有以Test开头已cle结尾的测试类 mvn -Dtest=com.mytest.Test*cle test 使用逗号分割要测试的类 mvn -Dtest=com.mytest.TestSquare,com.mytest.Test*cle test 使用#指定测试方法,使用*通配测试方法 mvn -Dtest=com.mytest.TestSquare#testABC,com.mytest.Test*cle test mvn -Dtest=com.mytest.TestSquare#testA*,com.mytest.TestCircle test 使用+号指定一个类中的多个测试方法 mvn -Dtest=com.mytest.TestSquare#testABC+testEFG,com.mytest.TestCircle test注意:写成如下方式是不行的,必须用加号来分割同一个类中测试方法。
mvn -Dtest=com.mytest.TestSquare#testABC,com.mytest.TestSquare#testABCtestEFG test (JUnit测试报表中只有一个测试结果,另外一个被覆盖了)在实际项目中遇到一个问题,就是使用下面的参数的时候
-Dtest=com.mytest.TestSquare#testABC+testEFG,com.mytest.TestCircle 找不到测试对象,把dependence surefire-junit47去掉后,就可以识别了。下面是Maven build配置。<plugins> <!-- unit tests: read more @ https://maven.apache.org/surefire/maven-failsafe-plugin/ --> <!-- default test case pattern: **/Test*.Java, **/*Test.java, **/*TestCase.java --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.16</version> <configuration> <argLine>-Dfile.encoding=UTF-8</argLine> <skipTests>${skip.tests}</skipTests> <!-- systemPropertyVariables> <propertyName>username</propertyName> <buildDirectory>${username}</buildDirectory> <propertyName>env</propertyName> <buildDirectory>${env}</buildDirectory> <propertyName>catalog</propertyName> <buildDirectory>${catalog}</buildDirectory> </systemPropertyVariables--> </configuration> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.16</version> </dependency> </dependencies> </plugin> <!-- integration tests: read more @ http://maven.apache.org/surefire/maven-failsafe-plugin/ --> <!-- default test case pattern: **/IT*.java, **/*IT.java, **/*ITCase.java --> </plugins>
参考:http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html