欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

EMMA-代码覆盖率测试工具

程序员文章站 2022-05-11 12:21:13
...
最近在做代码覆盖率测试集成,看了一些文章,此处只做整理,方便以后查询使用。

ps:前人种树,后人乘凉,感谢!

查看的一些文章:

http://www.cnblogs.com/morebetter/archive/2013/06/26/3156756.html
http://www.ibm.com/developerworks/cn/opensource/os-cn-emma/
http://lishiguang.iteye.com/blog/1123641
http://colin2wang.iteye.com/blog/627618
http://m.blog.csdn.net/blog/dawnming/7943589
http://www.tuicool.com/articles/3YJRfuU
http://blog.sina.com.cn/s/blog_4692ea0a0101iusi.html
http://jingyan.baidu.com/article/49ad8bce6b392a5834d8fa0e.html
http://www.cnblogs.com/morebetter/archive/2013/06/26/3156756.html

以下为对上边文章的搜集和验证:

//0.插装准备
将emma.jar 放到 “../java/jre/lib/ext” 下面。
//1.插装
// 1.1对类文件插装命令
java emma instr -m overwrite -ip D:/Appium/UATProj/testUpload/WebRoot/WEB-INF/classes/org/yyx/upload -Dmetadata.out.file=D:/emma/testUpload/coverage.em
// 1.2对 JAR 包插装命令
java emma instr -m overwrite -cp D:/emma/emma.jar -Dmetadata.out.file=D:/emma/testUpload/coverage.em
// 1.3对 WAR/EAR 包插装命令
java emma instr -m overwrite -ip D:/emma/MyDWRSample.war -Dmetadata.out.file=D:/emma/MyDWRSample/coverage.em
// 1.4选择插装命令
java emma instr -m overwrite -cp TestWs.jar -ix +org.wstest.service.* -Dmetadata.out.file=/root/emma/Number_coverage.em
// 1.5合并元数据
java emma merge -input c:/coverage1.em,c:/coverage2.em -out c:/coverage.em
//2.运行测试用例
//3.收集覆盖率信息
// 3.1从本地收集
java emma ctl -connect localhost:47653 -command coverage.get,D:/emma/testUpload/coverage.ec
// 3.2从远程机器上收集
java emma ctl -connect 192.168.61.123:47653 -command coverage.dump,D:/emma/testUpload/coverage.ec
//4.生成覆盖率报告
java emma report -r html -in D:/emma/testUpload/coverage.em,D:/emma/testUpload/coverage.ec - Dreport.html.out.file=c:/emma/testUpload/coverage.html -Dreport.metrics=class:80,method:75
//5.清除内存中覆盖率信息命令
java emma ctl -connect localhost:47653-command coverage.reset
//6.合并覆盖率结果命令
java emma merge –input coverage1.ec,coverage2.ec,coverage3.ec –output coverage.ec

ps:
a.本次关注点
1.插装-对类文件插装命令 2.运行测试用例 3.收集覆盖率信息 4.生成覆盖率报告
b.使用emma时遇到几个问题
1、没法直接操作war包,如果对war包进行instr,提示说处理了0个文件,所以只好把war包解压以后再处理
2、在windows下tomcat不能自动得到coverage.ec文件,需要使用命令手动获取。


//插装  运行  收集
1.
cd D:\eclipse-standard-kepler-SR2-win32\apache-tomcat-6.0.35\webapps

java emma instr -m overwrite -ip testUploado\WEB-INF\classes\org\yyx\upload\ -Dmetadata.out.file=coverage.em
java emma ctl -connect localhost:47653 -command coverage.get,coverage.ec

ok!

java emma instr -m overwrite -ip testUploado\WEB-INF\classes -Dmetadata.out.file=coverage.em
java emma ctl -connect localhost:47653 -command coverage.get,coverage.ec

ok!

java emma instr -m overwrite -ip testUploado\WEB-INF\classes -Dmetadata.out.file=D:/emma/testUpload/coverage.em
java emma ctl -connect localhost:47653 -command coverage.get,D:/emma/testUpload/coverage.ec

ok!

2.
java emma instr -m overwrite -ip D:\eclipse-standard-kepler-SR2-win32\apache-tomcat-6.0.35\webapps\testUploado\WEB-INF\classes -Dmetadata.out.file=D:/emma/testUpload/coverage.em
java emma ctl -connect localhost:47653 -command coverage.get,D:/emma/testUpload/coverage.ec

ok!


3.
cd D:\eclipse-standard-kepler-SR2-win32\apache-tomcat-6.0.35\webapps\testUploado\WEB-INF

java emma instr -m overwrite -ip classes -Dmetadata.out.file=D:/emma/testUpload/coverage.em
java emma ctl -connect localhost:47653 -command coverage.get,D:/emma/testUpload/coverage.ec

ok!

4.
cd D:\eclipse-standard-kepler-SR2-win32\apache-tomcat-6.0.35\webapps\testUploado

java emma instr -m overwrite -ip WEB-INF\classes -Dmetadata.out.file=D:/emma/testUpload/coverage.em
java emma ctl -connect localhost:47653 -command coverage.get,D:/emma/testUpload/coverage.ec

ok!

5.
java emma instr -m overwrite -ip testUploado.war -Dmetadata.out.file=c:\coverage.em

fall!

本次测试说明:
对于war包插装,emma搞不定,没有说得那没牛

6.
(Tomcat Project)
java emma instr -m overwrite -ip D:\eclipse-standard-kepler-SR2-win32\apache-tomcat-6.0.35\webapps\testUpload\WEB-INF\classes -Dmetadata.out.file=D:/emma/testUpload/coverage.em
java emma ctl -connect localhost:47653 -command coverage.get,D:/emma/testUpload/coverage.ec

fall!

(Dynamic Web Projec)
java emma instr -m overwrite -ip D:\eclipse-standard-kepler-SR2-win32\apache-tomcat-6.0.35\webapps\testUploadz\WEB-INF\classes -Dmetadata.out.file=D:/emma/testUpload/coverage.em
java emma ctl -connect localhost:47653 -command coverage.get,D:/emma/testUpload/coverage.ec

ok!

本次测试说明:
eclipse新建Web项目时,必须建立为Dynamic Web Project项目,
而不能使用tomcat插件建立Tomcat Project,
否则收集覆盖率信息时报错:
"emma ctl:coverage.get: RPC failure while executing [coverage.get]
Exception in thread "main" com.vladium.emma.EMMARuntimeException: coverage.get:
RPC failure while executing [coverage.get]
at com.vladium.emma.ctl.CtlProcessor._run(CtlProcessor.java:242)"

没有尝试直接通过ant或maven打包是否有问题,有待检验

//生成
java emma report -r html -sp D:\eclipse-standard-kepler-SR2-win32\apache-tomcat-6.0.35\webapps\testUploadz\src -in D:\emma\testUpload\bak6\coverage.em,D:\emma\testUpload\bak6\coverage.ec -Dreport.html.out.encoding=utf-8 -Dreport.html.out.file=D:\emma\testUpload\bak6\coverage.html -Dreport.metrics=class:80,method:75

ps:
1.emma在jdk1.7以上版本运行问题及解决方案 :
-XX:+UseSplitVerifier
语法:
java -XX:+UseSplitVerifier emma report -r html -sp D:\eclipse-standard-kepler-SR2-win32\apache-tomcat-6.0.35\webapps\testUploadz\src -in D:\emma\testUpload\bak6\coverage.em,D:\emma\testUpload\bak6\coverage.ec -Dreport.html.out.encoding=utf-8 -Dreport.html.out.file=D:\emma\testUpload\bak6\coverage.html -Dreport.metrics=class:80,method:75

2.内存不够解决方案:
-Xmx500m -Xms256m
语法:
java -Xmx500m -Xms256m emma report -r html -sp D:\eclipse-standard-kepler-SR2-win32\apache-tomcat-6.0.35\webapps\testUploadz\src -in D:\emma\testUpload\bak6\coverage.em,D:\emma\testUpload\bak6\coverage.ec -Dreport.html.out.encoding=utf-8 -Dreport.html.out.file=D:\emma\testUpload\bak6\coverage.html -Dreport.metrics=class:80,method:75

3.解决emma的coverage report 代码文件乱码的问题
下载emma-2.0.5312-src.zip   http://pkgs.fedoraproject.org/repo/pkgs/emma/
修改core\java12\com\vladium\emma\report\html\ReportGenerator.java的embedSrcFile方法
修改前
in = new BufferedReader (new FileReader (srcFile), IO_BUF_SIZE);
修改后
in = new BufferedReader (new java.io.InputStreamReader(new java.io.FileInputStream(srcFile), m_settings.getOutEncoding ()), IO_BUF_SIZE);
修改后编译,解压emma.jar并替换其中相应的文件重新打包即可。可直接替换emma-2.1.5320版本jar文件,已测试。
采用此jar后,可在java emma report 命令中传入-Dreport.html.out.encoding=utf-8 ,也可直接修改emma.jar中emma_default.properties文件的report.html.out.encoding: ISO-8859-1,改为你程序所使用的编码格式即可。
相关标签: EMMA