maven install时自动施行单元测试
maven install时自动执行单元测试
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.动态指定要运行的测试用例
- 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 | 标注方法在所有测试方法之后运行 |
- <? 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 >
- @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 >
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 >
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 >
转自:http://www.verydemo.com/demo_c290_i17360.html
maven install时自动施行单元测试的更多相关文章
- 今天maven install时碰到的两个问题(堆溢出和编译错误)
问题1.maven install时出现,日志如下: 系统资源不足.有关详细信息,请参阅以下堆栈追踪. java.lang.OutOfMemoryError: Java heap space at c ...
- maven -- 问题解决(一)解决eclipse中maven项目配置过程和maven install时出现的问题
问题一: 配置项目时出现的错误: error: Cannot change version of project facet Dynamic Web Module to 2.5. error: One ...
- maven打包时跳过单元测试
运行mvn install时跳过Test <project> [...] <build> <plugins> <plugin> <groupId& ...
- Eclipse中Maven Install时发生错误
问题描述 要把一个本地包保存进本地maven库中, 所以对该project执行了run as => Maven Install, 结果报下面的错误. 解决办法 1. 通过命令窗口手动创建这两个文 ...
- maven install时跳过测试
xl_echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!! - ...
- maven编译时如何忽略单元测试
共有两种解决办法 1.通过在命令行设置:-Dmaven.test.skip=true 如:mvn clean install tomcat:run -Dmaven.test.skip=true 2.通 ...
- eclipse里maven install时,报错提示jdk为无效的目标版本:1.7
http://blog.csdn.net/wabiaozia/article/details/51733372 ************************************ 报错提示: [ ...
- maven install 时 pom中skip test
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-suref ...
- maven install 时提示“程序包 javax.crypto不存在”
但是javax.crypto是在jdk的jre\lib目录下的 解决方案: <compilerArguments> <bootclasspath>${java.home}/li ...
随机推荐
- Java项目结构总结
一.Struts2+Spring+Hibernate 项目的文档结构 二.Spring MVC+MyBatis项目的文档结构 三.Tomcat文档目录结构 转载至:https://www.cnblog ...
- Oracle---------coalesce的用法介绍
COALESCE (expression_1, expression_2, ...,expression_n)依次参考各参数表达式,遇到非null值即停止并返回该值.如果所有的表达式都是空值,最终将返 ...
- nginx2
Nginx的高可用是keeplived,keeplived是为lvs服务的. Nginx上分别安装keepalived,keepalived之间通过心跳交流,主节点宕机备节点起来.keepalived ...
- Spring 之高级装配
[环境与Profile] 暂略 [条件化的bean] 暂略 [处理自动装配歧义性] 暂略 [ bean 的作用域] 在 @Componen . @Bean 下以及 XML 中的声明方式如下所示, @C ...
- Windows:FTP命令大全
Windows:FTP命令大全 简介 1, open:与服务器相连接: 2, send(put):上传文件: 3,get:下载文件: 4,mget:下载多个文件: 用法: mget *:下载当前路径下 ...
- Percona-Server-5.7.16 启动错误
基于:percona-server-5.7.16 启动报错: [root@monitor mysql]# ./bin/mysqld_safe --defaults-file=/data/config ...
- Book Review of "The Practice of Programming" (Ⅰ)
The Practice of Programming In the preface, the author illustrates four basic principles of programm ...
- Python引用多个模块,调用模块中的函数时,要注意的地方
转自:http://blog.csdn.net/yjk13703623757/article/details/70237463 python模块是”从下到上”导入(import)的. 例如: a.py ...
- 统计mysql数据库中数据表/字段等数量的sql
1.查询一个表中有多少个字段: SELECT COUNT(*) FROM information_schema. COLUMNSWHERE table_schema = '数据库名'AND table ...
- XMLHTTP.readyState
XMLHTTP.readyState的五种就绪状态: 0:请求未初始化(还没有调用 open()). 1:请求已经建立,但是还没有发送(还没有调用 send()). 2:请求已发送,正在处理中(通常现 ...