Maven运行JUnit测试之mvn test的默认行为
2015-05-25 14:42
513 查看
参照http://www.blogjava.net/sitinspring/archive/2007/06/20/125224.html,用Maven跑JUnit类。但是运行mvn
test,却报找不到任何测试类,即:There are no tests to run.
郁闷之际查了一下maven参考资料,发现原来是surefire插件的默认行为所致。maven是使用surefire插件执行测试的,它按照指定格式的类名来查找匹配的测试类,
默认包含的测试类:
**/*Test.java
**/Test*.java
**/*TestCase.java
默认排除的测试类:
**/Abstract*Test.java
**/Abstract*TestCase.java
因此默认情况下,诸如“add_exist_department.java”或者“add_exist_departmentTests.java”这种JUnit类是不会被Maven发现并执行的,可按如下修改surefire插件的配置以达到包含"**/*Tests.java"测试类的目的:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
运行指定的测试类:
>mvn test -Dtest=[ClassName]
运行测试类中指定的方法:(这个需要maven-surefire-plugin:2.7.3以上版本才能支持)
>mvn test -Dtest=[ClassName]#[MethodName]
[MethodName]为要运行的方法名,支持*通配符,范例:
1) >mvn test -Dtest=MyClassTest#test1
2) >mvn test -Dtest=MyClassTest#*test*
test,却报找不到任何测试类,即:There are no tests to run.
郁闷之际查了一下maven参考资料,发现原来是surefire插件的默认行为所致。maven是使用surefire插件执行测试的,它按照指定格式的类名来查找匹配的测试类,
默认包含的测试类:
**/*Test.java
**/Test*.java
**/*TestCase.java
默认排除的测试类:
**/Abstract*Test.java
**/Abstract*TestCase.java
因此默认情况下,诸如“add_exist_department.java”或者“add_exist_departmentTests.java”这种JUnit类是不会被Maven发现并执行的,可按如下修改surefire插件的配置以达到包含"**/*Tests.java"测试类的目的:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
运行指定的测试类:
>mvn test -Dtest=[ClassName]
运行测试类中指定的方法:(这个需要maven-surefire-plugin:2.7.3以上版本才能支持)
>mvn test -Dtest=[ClassName]#[MethodName]
[MethodName]为要运行的方法名,支持*通配符,范例:
1) >mvn test -Dtest=MyClassTest#test1
2) >mvn test -Dtest=MyClassTest#*test*
相关文章推荐
- Maven运行JUnit测试之mvn test的默认行为
- TekNote #1: 在Mac(Snow Leopard)下,用Maven运行测试(mvn test)时,DBUnit 读文件的编码问题(file.encoding)
- junit或maven test中无法输出被测试类中log4j的调试信息的解决方案
- JUnit测试是中文 maven test是乱码
- 第10章 从Maven2中运行JUnit测试(第三部分 JUnit与构建过程)
- [置顶] maven项目junit测试运行时提示java.lang.NoClassDefFoundError: xx
- Maven运行JUnit测试(http://www.360doc.com/content/13/0927/15/7304817_317455642.shtml)
- maven配置时,测试出现“'mvn' 不是内部或外部命令,也不是可运行的程序”错误
- Android JUnit Test——批量运行测试代码
- Python中unittest 用不同的参数组合,测试同样的行为;使test case根据参数多次运行
- junit 测试spring-test 代码!在项目中运行测试通过,留着以后直接使用
- mvn test 整工程测试解决方案(基于junit.framework)
- Maven中运行Junit测试Log4j的配置
- JUnit学习笔记5---从Maven中运行测试
- Maven项目Junit测试类运行报错
- JUnit学习笔记5---从Maven中运行测试
- JUnit中@Test的运行顺序
- 关于python测试的nose框架和doc test以及和junit的一点对比
- Junit核心——测试类(TestCase)、测试集(TestSuite)、测试运行器(TestRunner)
- iOS 给测试人员测试手机APP的四种方法:真机运行(略),打ipa包,(testFlighe)邮件,蒲公英(二)testflight