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 ...
随机推荐
- Git学习笔记-完全版
注意本文参考廖雪博客: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 一:Git ...
- URAL - 1902 Neo-Venice
题目: Mars was the first planet colonized by humans. After a long terraforming process its appearance ...
- CodeForces - 894E Ralph and Mushrooms (强连通缩点+dp)
题意:一张有向图,每条边上都有wi个蘑菇,第i次经过这条边能够采到w-(i-1)*i/2个蘑菇,直到它为0.问最多能在这张图上采多少个蘑菇. 分析:在一个强连通分量内,边可以无限次地走直到该连通块内蘑 ...
- 对Java的接口和抽象类 的一些了解
学习并转载自: https://mp.weixin.qq.com/s?__biz=MzAxMzQ3NzQ3Nw==&mid=2654251476&idx=4&sn=e66ec4 ...
- Sybase:游标用法以及嵌套用法
Sybase:游标用法以及嵌套用法 游标示例一: --Sybase游标示例一: create PROCEDURE DBA.p_proc_test() ON EXCEPTION RESUME begin ...
- Zabbix linux agent 安装
系统:Linux Centos 7.3 x64 服务:Zabbix_agent 3.0.16 一.安装Zabbix_agent 服务 1.安装zabbix 3.0 yum源 rpm -ivh http ...
- 20145219 《Java程序设计》实验一 Java开发环境的熟悉(Linux + Eclipse)实验报告
20145219 <Java程序设计>实验一 Java开发环境的熟悉(Windws + IDEA)实验报告 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用IDEA 编辑. ...
- Oracle sql plus中常用的几个命令
1.set linesize 300(表示一行为300个字符) set linesize可以设置一行显示的字符数,默认情况下为80个字符 2.l(list) 可以显示缓冲区中的最后执行的内容 3.ru ...
- [mongodb] MMAP 和wiredTiger 的比较
mongodb 现在有两款存储引擎 MMAPv1 和 WireTiger,当然了除了这两款存储引擎还有其他的存储引擎了. 如: 内存引擎:现在的mongodb 版本中已经有了,主要的cache 服务 ...
- AtCoder Regular Contest 095
AtCoder Regular Contest 095 C - Many Medians 题意: 给出n个数,求出去掉第i个数之后所有数的中位数,保证n是偶数. \(n\le 200000\) 分析: ...