Monday, September 01, 2008

用 Cobertura 测量测试覆盖率

单元测试相信大家都不陌生,测试先行编程(Test-First Programming)和测试驱动也不是什么新概念了。测试改进了代码质量,但这也只是针对实际测试到的那部分代码而言的。您需要有一个工具告诉您程序的哪些部分没有测试到,这样就可以针对这些部分编写测试代码并找出更多 bug。

现在,有 Cobertura (cobertura 在西班牙语是“覆盖”的意思)这个免费的 GPL 工具在 Java 领域来完成这个任务。Cobertura 通过在执行测试时,用额外的语句记录哪些行被测试到、哪些行没有被测试到,通过这种方式来度量字节码,以便对测试进行监视。然后它生成一个 HTML 或者 XML 格式的报告,指出代码中的哪些包、哪些类、哪些方法和哪些行没有测试到。可以针对这些特定的区域编写更多的测试代码,以发现所有隐藏的 bug。

先来看看生成报告的样子吧:

Cobertura 报告示例(点击放大)


可以看到报告列出了每个包的行覆盖率、分支覆盖率以及复杂度。点击进去,可以看到针对这个包里头每个类的报告,进而每行代码的测试情况,以及被测试到的次数。这里是 Cobertura 官方网站上给出的一个报告的 Sample。

在使用上,Cobertura 支持命令行操作,也可以结合到 Ant 任务。当然首先要定义任务:
<property name="cobertura.dir" value="C:/javastuff/cobertura" />

<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="cobertura.jar" />
<include name="lib/**/*.jar" />
</fileset>
</path>

<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />

然后,就可以用 cobertura-instrument 任务来统计测试目标:
<delete file="cobertura.ser" />

<cobertura-instrument todir="${instrumented.dir}">
<ignore regex="org.apache.log4j.*" />
<fileset dir="${classes.dir}">
<include name="**/*.class" />
<exclude name="**/*Test.class" />
</fileset>
<fileset dir="${guiclasses.dir}">
<include name="**/*.class" />
<exclude name="**/*Test.class" />
</fileset>
<fileset dir="${jars.dir}">
<include name="my-simple-plugin.jar" />
</fileset>
</cobertura-instrument>

之后,运行 Junit 测试(重要的部分用蓝色标出):
<junit fork="yes" dir="${basedir}" failureProperty="test.failed">
<!--
指定覆盖率统计的数据文件名,以下为默认值
-->
<sysproperty key="net.sourceforge.cobertura.datafile"
file="${basedir}/cobertura.ser" />


<!--
注意 classpath 的顺序:被处理过的 class 路径在为处理的
class 路径之前。这很重要!
-->
<classpath location="${instrumented.dir}" />
<classpath location="${classes.dir}" />

<!--
被处理过的 class 会在 Cobertura 运行时引用 Cobertura
的类库,因此这些类也必须在我们的 classpath 中。
-->
<classpath refid="cobertura_classpath" />

<formatter type="xml" />
<test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
<batchtest todir="${reports.xml.dir}" unless="testcase">
<fileset dir="${src.dir}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>

最后,我们可以用 cobertura-report 任务来输出 HTML 格式或者 XML 格式的报告:
<cobertura-report format="html" destdir="${coveragereport.dir}"
srcdir="${src.dir}" />

也可以有更复杂的要求:
<cobertura-report format="html" destdir="${coveragereport.dir}" >
<fileset dir="${src.dir}">
<include name="**/*.java" />
<exclude name="**/*Stub.java" />
</fileset>
<fileset dir="${guisrc.dir}">
<include name="**/*.java" />
<exclude name="**/*RB.java" />
</fileset>
</cobertura-report>

另外,还可以用 cobertura-check 任务来检查统计出来的覆盖率是否达到预先的期望,如果没有达到则使得整个 Ant 任务失败;以及 cobertura-merge 任务用来合并多个测试的统计数据文件。

要特别注意的是,在这个 Ant 任务中,一定要保证数据文件 cobertura.ser 每次都被更新,并且在 instrument 的任务和后面做 Junit 的过程中,两者的路径一致,否则可能导致在输出的报告中,所有的测试结果都是 100% 或者 0%。

参考资料

30 comments:

Coach Factory said...

coach factory, oakley sunglasses, polo ralph lauren outlet, kate spade outlet, michael kors, ray ban sunglasses, michael kors outlet, louboutin, ray ban, longchamp, louis vuitton handbags, air jordan, louboutin, prada handbags, gucci outlet, oakley sunglasses, prada outlet, coach outlet store, nike shoes, louis vuitton, polo ralph lauren, air max, michael kors outlet, louboutin, michael kors outlet, toms shoes, tory burch outlet, burberry outlet online, tiffany and co, air max, jordan shoes, nike air max, coach outlet, true religion jeans, oakley sunglasses, ray ban sunglasses, burberry outlet online, nike free, michael kors, true religion outlet, louis vuitton outlet online, true religion, chanel handbags, polo ralph lauren outlet, coach purses, nike air max, nike free, ralph lauren, longchamp outlet

Coach Factory said...

oakley outlet, moncler outlet, oakley vault, rayban, ray-ban, canada goose outlet, oakley vault, oakley sunglasses cheap, cheap sunglasses, cheap oakley, ugg boots, ugg, ray ban, ralph lauren, oakley vault, ray ban outlet, raybans, ray ban, cheap sunglasses, ray ban sunglasses outlet, louboutin, oakley, cheap sunglasses, canada goose, canada goose, canada goose, cheap oakley sunglasses, cheap oakley sunglasses, ray bans, oakley sunglasses outlet, oakley sunglasses cheap, oakley sunglasses outlet, discount oakley sunglasses, ray ban, ray bans, rayban, cheap oakley sunglasses, tn pas cher, ray ban outlet, oakley outlet, oakley outlet, moncler, oakley sunglasses outlet, canada goose, gucci, moncler, moncler, canada goose outlet, oakley sunglasses cheap

Coach Factory said...

ferragamo shoes, supra shoes, abercrombie and fitch, ray ban pas cher, wedding dresses, nfl jerseys, reebok outlet, hermes, juicy couture outlet, air max, lululemon, p90x, ghd, timberland, mulberry, lancel, hogan, new balance shoes, birkin bag, mont blanc, asics, nike air max, ugg boots, soccer jerseys, hollister, rolex watches, north face outlet, beats by dre, montre pas cher, nike free, louis vuitton, ugg boots, herve leger, chi flat iron, vans, ralph lauren, oakley pas cher, jimmy choo, insanity, converse shoes, louis vuitton, karen millen, celine handbags, bottega veneta, marc jacobs, soccer shoes, mcm handbags, yoga pants, burberry, valentino shoes

jim kill said...

louis vuitton handbags
burberry scarf
ray ban sunglasses outlet
michael kors
cheap uggs
cheap oakleys
abercrombie fitch
ugg boots
tory burch flats
lv outlet
tods outlet
michael kors handbags
michael kors outlet online
ghd flat iron
coach outlet
coach factory online
prada handbags
lebron 11
canada goose jacket
ray ban outlet
christian louboutin
chanel handbags
coach outlet
polo outlet
michael kors outlet online
ray ban canada
coach factory outlet
20150311huangqihang

mao yun said...

5
mlb jerseys
ralph lauren outlet
tods outlet
cheap oakley sunglasses
lululemon outlet online
converse all star
jordan 4 retro
ed hardy clothing
coach outlet store
salomon running shoes
air jordan 4
pandora charms
discount oakley sunglasses
toms shoes outlet online
lebron shoes
marc jacobs outlet
coach factory outlet
abercrombie and fitch
ralph lauren outlet online
kate spade handbags
burberry outlet
coach outlet
lacoste polo shirts
kate spade
michael kors outlet online
louis vuitton
coach outlet store
coach outlet store
mont blanc pens for sale
ferragamo belts
tiffany and co jewelry
hollister uk
abercrombie and fitch
nike air force 1
snapback hats wholesale
lebron james shoes
air jordan concord
foamposite gold
ralph lauren outlet
pandora jewelry
air force one shoes
20150330maoqiuyun

Guo Guo said...

air max 90
bottega veneta outlet
nike air max uk
nike huarache
custom mlb jerseys
surpa sneakers
coach outlet
seattle seahawks jerseys
the north face uk
kansas city chiefs jerseys
nike outlet store
atlanta falcons jersey
chanel handbags
green bay packers jerseys
michael kors uk
los angeles clippers jerseys
burberry outlet
dallas cowboys jersey
nike running shoes
arizona cardinals jerseys
oakley sunglasses canada
the north face outlet
oakley sunglasses
real madrid jersey
cleveland cavaliers jersey
pittsburgh steelers jersey
north face jackets
soccer shoes
mizuno running
san francisco 49ers jerseys
dansko outlet
new orleans saints jerseys
new york jets jerseys
air jordan shoes
los angeles lakers jerseys
north face outlet
mulberry uk
hermes belt
stuart weitzman boots
washington redskins jerseys
wei20150610

Xiaozhengmm 123 said...

2015-7-28 xiaozhengm
christian louboutin sale
polo ralph lauren outlet
longchamp outlet
michael kors handbags
coach factory outlet
replica watches cheap
ed hardy
pandora rings
tory burch shoes
ralph lauren outlet
soccer jerseys wholesale
michael kors handbag
prada handbags
cheap ray ban sunglasses
chanel outlet
hollister clothing store
burberry outlet online
tory burch outlet
cheap jerseys
tory burch outlet online
cheap jordans
ray ban outlet
coach outlet online
air max 90
ralph lauren uk
coach outlet
kate spade bags
coach outlet
nike air force
chaussure louboutin
nike air max
tory burch handbags
air max shoes
air jordan
nike blazer
nike blazer low
jordan homme
michael kors
abercrombie
gucci

oakleyses said...

oakley sunglasses, longchamp outlet, ugg boots, chanel handbags, nike air max, longchamp, christian louboutin outlet, michael kors outlet, oakley sunglasses, tiffany and co, replica watches, tory burch outlet, nike air max, ray ban sunglasses, michael kors outlet, louboutin shoes, jordan shoes, louis vuitton outlet, louis vuitton, ray ban sunglasses, longchamp outlet, polo ralph lauren outlet, replica watches, kate spade outlet, oakley sunglasses, polo ralph lauren outlet, cheap oakley sunglasses, ugg boots, nike free, tiffany jewelry, michael kors outlet, uggs on sale, louis vuitton outlet, michael kors, oakley sunglasses, louboutin, louis vuitton, michael kors outlet, ray ban sunglasses, prada handbags, louis vuitton, burberry, burberry outlet online, gucci outlet

oakleyses said...

nike blazer, michael kors, nike free run uk, nike air max, true religion jeans, true religion jeans, air jordan pas cher, nike air max, ralph lauren uk, timberland, air max, nike roshe, tn pas cher, louboutin pas cher, true religion outlet, vans pas cher, coach outlet, hermes, ralph lauren pas cher, nike free, coach purses, michael kors, oakley pas cher, mulberry, hollister pas cher, lacoste pas cher, new balance pas cher, ray ban pas cher, vanessa bruno, burberry, hollister, michael kors, kate spade handbags, coach outlet, abercrombie and fitch, lululemon, coach factory outlet, nike roshe run, north face, nike air max, ray ban uk, michael kors, true religion jeans, converse pas cher, north face, air force, hogan, sac longchamp, longchamp pas cher, sac guess

oakleyses said...

ferragamo shoes, iphone 6s plus cases, babyliss, longchamp, reebok shoes, nike trainers, asics running shoes, oakley, iphone 5s cases, celine handbags, hollister, nfl jerseys, jimmy choo shoes, birkin bag, iphone 6s cases, iphone cases, mcm handbags, bottega veneta, giuseppe zanotti, iphone 6 plus cases, abercrombie and fitch, p90x workout, beats by dre, herve leger, north face outlet, instyler, louboutin, soccer jerseys, lululemon, nike roshe, s5 cases, timberland boots, mac cosmetics, soccer shoes, valentino shoes, insanity workout, vans shoes, hollister, ralph lauren, wedding dresses, ipad cases, chi flat iron, north face outlet, iphone 6 cases, nike air max, ghd, mont blanc, baseball bats, new balance, nike huarache

oakleyses said...

ugg pas cher, vans, moncler, moncler, canada goose, louis vuitton, ugg boots uk, canada goose, toms shoes, doudoune canada goose, moncler, louis vuitton, louis vuitton, ugg australia, juicy couture outlet, moncler, supra shoes, doke gabbana outlet, gucci, karen millen, moncler outlet, hollister, moncler, hollister, canada goose, louis vuitton, ray ban, marc jacobs, pandora jewelry, michael kors outlet online, moncler, swarovski crystal, pandora jewelry, wedding dresses, thomas sabo, moncler, bottes ugg, sac louis vuitton pas cher, nike air max, barbour, barbour jackets, pandora charms, canada goose outlet, canada goose uk, michael kors outlet, converse outlet, links of london, canada goose outlet, pandora charms, canada goose

mmjiaxin said...

black friday deals
discount oakley sunglasses
oakley sunglasses wholesale
designer handbags,handbags outlet,cheap handbags,purses and handbags,wholesale handbags,designer bags,bags outlet,handbags wholesale,cheap purses,discount handbags,handbags sale,wholesale purses,handbags and purses,designer purses
mont blanc pens
swarovski outlet
camisetas futbol baratas
mulberry uk
valentino outlet
michael kors online outlet
fitflops sale
cheap wedding dresses
mlb jerseys
giuseppe zanotti outlet
converse shoes
soccer jerseys wholesale
tiffany and co
ferragamo outlet
true religion jeans
oakley sunglasses wholesale
mm1205

方松腾 said...

michael kors handbags,michael kors outlet,michael kors,kors outlet,michael kors outlet online,michael kors outlet online sale,michael kors handbags clearance,michael kors purses,michaelkors.com,michael kors bags,michael kors shoes,michaelkors,cheap michael kors
mlb jerseys wholesale
cheap mlb jerseys
cheap nfl jerseys
nfl jerseys wholesale
nike shoes
cheap oakley sunglasses
snapback hats
soccer jerseys,cheap soccer jerseys,cheap soccer jerseys for sale,soccer jersey,usa soccer jersey,football jerseys
cheap uggs
cheap ugg boots
ugg boots
uggs outlet
wedding dresses
chicago blackhawks

方松腾 said...

michael kors handbags,michael kors outlet,michael kors,kors outlet,michael kors outlet online,michael kors outlet online sale,michael kors handbags clearance,michael kors purses,michaelkors.com,michael kors bags,michael kors shoes,michaelkors,cheap michael kors
mlb jerseys wholesale
cheap mlb jerseys
cheap nfl jerseys
nfl jerseys wholesale
nike shoes
cheap oakley sunglasses
snapback hats
soccer jerseys,cheap soccer jerseys,cheap soccer jerseys for sale,soccer jersey,usa soccer jersey,football jerseys
cheap uggs
cheap ugg boots
ugg boots
uggs outlet
wedding dresses
chicago blackhawks

Chenzhen 20160303chenzhen said...

michael kors outlet
michael kors canada
coach factory outlet
louis vuitton borse
lululemon outlet store
longchamp uk
prada handbags
cartier
adidas shoes
cheap oakley sunglasses
adidas pure boost black
oakley sunglasses
rolex daytona
stephen curry basketball shoes
coach factory outlet
ed hardy uk
converse outlet
coach outlet online
new balance shoes
valentino bags
true religion
fitflops sale clearance
gucci handbags outlet
coach outlet online
ferragamo outlet
cheap jordan shoes
under armour outlet store
cheap basketball shoes
kate spade outlet
coach factory outlet
michael kors outlet online
versace sunglasses
louboutin shoes
ugg boots
canada goose outlet
cheap nfl jerseys
michael kors handbgas
cheap oakleys
michael kors outlet
dior sunglasses 2016
chenzhen20160429

chenlili said...

chenlili20160629
michael kors outlet
coach factory outlet
air jordans
oakley outlet
kobe 11
nike running shoes for women
cheap air jordans
michael kors outlet clearance
ralph lauren
gucci outlet
nike air max 90
ralph lauren outlet
air jordans
retro jordans 13
montblanc
vans shoes
jordan retro 11
hollister outlet
toms shoes
ralph lauren
michael kors handbags
true religion sale
ray ban sunglasses
tory burch outlet online
gucci belts
marc jacobs handbags
coach outlet store online
louis vuitton outlet online
timberlands
oakley sunglasses
hollister clothing
kobe shoes
ray bans
ray ban sunglasses
michael kors outlet online
replica rolex watches
true religion outlet

Gege Dai said...

jianbin0701
michael kors outlet
mcm outlet
michael kors outlet online
hermes outlet
nike air max
reebok shoes
michael kors outlet
replica watches
rolex watches
fitflops sale clearance
coach outlet online
coach outlet online
ferragamo outlet
camisetas futbol baratas
nike uk store
mulberry bags
louis vuitton neverfull sale
oakley sunglasses
mulberry handbags sale
michael kors uk outlet
tory burch outlet
louis vuitton outlet online
michael kors outlet
cheap mlb jerseys
soccer jerseys wholesale
michael kors uk
coach handbags outlet
louboutin pas cher
adidas uk store
toms outlet
lacoste shirts
oakley sunglasses uk
tory burch outlet
chrome hearts outlet

Qinan Chen said...

valentino shoes on sale
fitflops
true religion outlet
pandora charms
pandora charms
nike air max
michael kors purses
rolex submariner
nike air force white
versace shoes
tiffany and co
ray bans
cheap ugg sale
oakley sunglasses
coach outlet
kate spade handbags
adidas superstar
hollister sale
polo ralph lauren uk
moncler outlet store
mizuno running shoes
discount oakley sunglasses
prada outlet
fitflop uk
polo ralph lauren
zhenwei20160721

dong dong23 said...

fitflops uk
cheap uggs
nike tn pas cher
louis vuitton outlet
air jordan pas cher
adidas trainers
kate spade handbags
louis vuitton
louis vuitton outlet
michael kors outlet
201610.19wengdongdong

abcya said...

I enjoyed over read your blog post. Your blog have nice information, I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again..
starfall | barney | minecraft games

琐事 said...

michael kors handbags wholesale
cheap nfl jerseys wholesale
instyler max 2
nike huarache trainers
ed hardy outlet
pandora charms
michael kors handbags sale
tiffany and co outlet
lebron shoes
longchamp le pliage

chenmeinv0 said...

cheap ray ban sunglasses
ray ban wayfarer
uggs uk
louis vuitton handbags
uggs on sale
ralph lauren outlet
louis vuitton
toms shoes uk
coach outlet store online clearances
pandora charms
2016.11.14xukaimin

xumeiqing said...

20161117meiqing
nike store
christian louboutin
canada goose outlet
uggs outlet
polo outlet
levis outlet online
moncler
canada goose jackets
michael kors handbags
nike huarache

chenmeinv0 said...

michael kors outlet
cheap toms shoes
christian louboutin pas cher
ugg australia
red bottom shoes
ray ban sunglasses
canada goose
coach handbags
uggs pas cher
cheap ugg boots
hzx20170206

Gege Dai said...

air max 90
ray-ban sunglasses
rolex watches
oakley sunglasses wholesale
lululemon pants
instyler ionic styler
cartier watches for women
cheap oakley sunglasses
coach factory outlet
oakley sunglasses
20170215lck

Meiqing Xu said...

phillies jerseys
coach factory outlet online
vans outlet
coach outlet store
mlb jerseys
tods outlet
michael kors outlet online
michael kors bags
air max 90
ralph lauren uk
20170310huazhen

Unknown said...

mulberry handbags
fitflops sale clearance
cheap oakley sunglasses
oakley sunglasses
ralph lauren polo
nike shoes uk
fitflops outlet
pandora charms sale clearance
cartier watches
nike outlet store
chanyuan2017.04.01

xjd7410@gmail.com said...

20170513 junda
yeezy boost
burberry canada
cheap jordan shoes
rolex watches for sale
gucci outlet online
ralph lauren outlet
coach factory outlet
fitflops sale clearance
polo outlet
michael kors handbags outlet

shengda xu said...

michael kors outlet
cheap nfl jerseys
louis vuitton outlet
coach outlet
nike outlet online
birkenstock sandals
louboutin
michael kors outlet
coach factory outlet
kate spade outlet
xushengda0609

ASO Services said...

Hi dear, I like your post so much, and it is very good. Hope that we can communicate with each other. I want to introduce an aso services, I try it and I feel it is so good to us beginners.