ITeye博客

ant中使用cobertura分析测试用例的代码覆盖率

danlley · 2007-07-17 21:48 · PM(Project Manage) · Markdown

这次还是配置问题,接上上次关于ant脚本模板的详细说明。对于一个完整的项目测试报告,一般来说我们会用JUnit生成的report来分析关于测试用例执行情况,但是,我们怎么样保证我们的测试用例的测试质量呢,我们如何知道我们的测试用例到底覆盖了多少我们的工程代码呢。Cobertura就是一个很好的开源免费插件,他不仅仅支持ant,而且对maven的支持也有很不错的表现。对于Cobertura对Maven的支持我会在下一个专题中专门阐述(官方提供的Maven plug-in有些小bug,别走开,下一专题告诉你,呵呵呵呵),我这篇文章只针对Cobertura在ant中的使用做一个说明。
在上一个关于ant脚本的专题中,我详细说明了如何用ant来完成mail→mkzip→report→junit→build→prepare→clean等一系列的工作,为了保证项目中能够使用Cobertura,我这次对上一个专题的ant模板进行适当的修改:mail→mkzip→coverage-report→cover-test→instrument→report→junit→build→prepare→clean
我增加了coverage-report(生成测试报告)、cover-test(进行覆盖率测试)和instrument(生成打过标签的二进制classes)等三个target。同时对mkzip和mail的target也进行了适当的修改,以便能够把进行覆盖测试的report进行打包发邮件。
首先定义一个顶级 taskdef 元素将 cobertura.jar 文件限定在当前工作目录中:

xml 代码

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

接下来需要定义一个instrument任务,该任务将在已经编译好的类文件中添加日志代码-打上标签。todir 属性指定将测量类放到什么地方。fileset 子元素指定测量哪些 .class 文件

js 代码

  1. <target name="instrument" depends="report">
  2. <cobertura-instrument todir="${instrumented.dir}">
  3. <fileset dir="${pro.build.path}">
  4. <include name="**/*.class" />
  5. <exclude name="**/*Test.class" />
  6. </fileset>
  7. </cobertura-instrument>
  8. </target>

 
 
接下来就可以进行覆盖测试了,定义一个cover-test任务,该任务依赖于instrument任务。要注意的一点就是被测量的类必须在原始类出现在类路径中之前出现在类路径中,而且需要将 Cobertura JAR 文件添加到类路径中。

js 代码

  1. <target name="cover-test" depends="instrument">
  2. <mkdir dir="${testreportdir}" />
  3. <junit dir="./" failureproperty="test.failure" printSummary="yes" fork="true" haltonerror="true">
  4. <classpath location="cobertura.jar" />
  5. <classpath location="instrumented" />
  6. <classpath>
  7. <fileset dir="lib">
  8. <include name="**/*.jar" />
  9. </fileset>
  10. <pathelement path="${pro.build.path}" />
  11. <pathelement path="${pro.build.path}" />
  12. </classpath>
  13. <batchtest todir="${pro.build.path}">
  14. <fileset dir="src/main/test">
  15. <include name="**/*Test.java" />
  16. </fileset>
  17. </batchtest>
  18. </junit>
  19. </target>

 
生成测试报告,为了能够使ant支持cobertura-report,同上个专题说到的一样,我们需要将Cobertura的相关资源文件在${ANT_HOME}/lib下放一份(cobertura-report不是ant内置标签):

js 代码

  1. <target name="coverage-report" depends="cover-test">
  2. <cobertura-report srcdir="src/main/java" destdir="instrumented" />
  3. </target>

修改打包target

js 代码

  1. <target name="mkzip" depends="coverage-report">
  2. <jar destfile="report/html/test-result${ant.project.name}.zip">
  3. <fileset dir="report/html">
  4. <include name="**/*.html" />
  5. <include name="**/*.css" />
  6. <include name="**/*.txt" />
  7. </fileset>
  8. </jar>
  9. <jar destfile="report/html/cover-test-result${ant.project.name}.zip">
  10. <fileset dir="instrumented">
  11. <include name="**/*.html" />
  12. <include name="**/*.css" />
  13. <include name="**/*.txt" />
  14. <include name="**/*.png" />
  15. <include name="**/*.js" />
  16. </fileset>
  17. </jar>
  18. </target>

 
 
修改发邮件target

js 代码

  1. <target name="mail" depends="mkzip">
  2. <mail mailhost="smtp.126.com" mailport="25" subject="The Build Test" user="用户名" password="邮箱密码">
  3. <from address="你的发信地址" name="Danlley Wei" />
  4. <fileset dir="report/html">
  5. <include name="**/test-result${ant.project.name}.zip" />
  6. <include name="**/cover-test-result${ant.project.name}.zip" />
  7. </fileset>
  8. <to address="收信人地址" name="Danlley Wei" />
  9. <message>The ${pro.name}--${pro.author} has been tested ! </message>
  10. </mail>
  11. </target>

最后执行一下看看结果。
 
本专题完整模板

js 代码

  1. <?xml version="1.0"?>
  2. <project name="springproj" basedir="." default="mail">
  3. <!--<property file="build.properties" /> -->
  4. <property name="pro.name" value="springproj" />
  5. <property name="pro.author" value="Danlley Wei" />
  6. <property name="src.dir" value="src/main/java" />
  7. <property name="pro.web.root" value="war" />
  8. <property name="pro.web.source" value="${pro.web.root}/WEB-INF" />
  9. <property name="pro.build.path" value="${pro.web.source}/classes" />
  10. <property name="user.dir" value="${pro.build.path}" />
  11. <property name="instrumented.dir" value="instrumented" />
  12. <taskdef classpathref="master-classpath" resource="tasks.properties" />
  13. <taskdef classpath="cobertura.jar" resource="tasks.properties" />
  14. <target name="mail" depends="mkzip">
  15. <mail mailhost="smtp.126.com" mailport="25" subject="The Build Test" user="邮箱用户名" password="邮箱密码">
  16. <from address="发信地址" name="Danlley Wei" />
  17. <fileset dir="report/html">
  18. <include name="**/test-result${ant.project.name}.zip" />
  19. <include name="**/cover-test-result${ant.project.name}.zip" />
  20. </fileset>
  21. <to address="收信地址" name="Danlley Wei" />
  22. <message>The ${pro.name}--${pro.author} has been tested ! </message>
  23. </mail>
  24. </target>
  25. <target name="mkzip" depends="coverage-report">
  26. <jar destfile="report/html/test-result${ant.project.name}.zip">
  27. <fileset dir="report/html">
  28. <include name="**/*.html" />
  29. <include name="**/*.css" />
  30. <include name="**/*.txt" />
  31. </fileset>
  32. </jar>
  33. <jar destfile="report/html/cover-test-result${ant.project.name}.zip">
  34. <fileset dir="instrumented">
  35. <include name="**/*.html" />
  36. <include name="**/*.css" />
  37. <include name="**/*.txt" />
  38. <include name="**/*.png" />
  39. <include name="**/*.js" />
  40. </fileset>
  41. </jar>
  42. </target>
  43. <target name="coverage-report" depends="cover-test">
  44. <cobertura-report srcdir="src/main/java" destdir="instrumented" />
  45. </target>
  46. <target name="cover-test" depends="instrument">
  47. <mkdir dir="${testreportdir}" />
  48. <junit dir="./" failureproperty="test.failure" printSummary="yes" fork="true" haltonerror="true">
  49. <classpath location="cobertura.jar" />
  50. <classpath location="instrumented" />
  51. <classpath>
  52. <fileset dir="lib">
  53. <include name="**/*.jar" />
  54. </fileset>
  55. <pathelement path="${pro.build.path}" />
  56. <pathelement path="${pro.build.path}" />
  57. </classpath>
  58. <batchtest todir="${pro.build.path}">
  59. <fileset dir="src/main/test">
  60. <include name="**/*Test.java" />
  61. </fileset>
  62. </batchtest>
  63. </junit>
  64. </target>
  65. <target name="instrument" depends="report">
  66. <cobertura-instrument todir="${instrumented.dir}">
  67. <fileset dir="${pro.build.path}">
  68. <include name="**/*.class" />
  69. <exclude name="**/*Test.class" />
  70. </fileset>
  71. </cobertura-instrument>
  72. </target>
  73. <target name="report" depends="junit">
  74. <junitreport id="myJUnitReport" taskname="reported" todir="report" description="Junit Report">
  75. <fileset dir="report">
  76. <include name="TEST-*.xml" />
  77. </fileset>
  78. <report todir="report/html" />
  79. </junitreport>
  80. </target>
  81. <target name="junit" depends="build">
  82. <mkdir dir="report/html" />
  83. <junit printsummary="yes" haltonerror="yes" haltonfailure="yes" fork="yes">
  84. <classpath location="${build.instrumented.dir}" />
  85. <formatter type="plain" usefile="false" />
  86. <formatter type="xml" />
  87. <test name="org.danlley.hibernate.dao.DeptDAOImplTest" todir="report" />
  88. <classpath refid="master-classpath" />
  89. </junit>
  90. </target>
  91. <target name="build" depends="prepare">
  92. <javac destdir="${pro.build.path}" target="1.5" debug="true">
  93. <src path="src/main/java" />
  94. <classpath refid="master-classpath" />
  95. </javac>
  96. <javac destdir="${pro.build.path}" target="1.5">
  97. <src path="src/main/test" />
  98. <classpath refid="master-classpath" />
  99. </javac>
  100. </target>
  101. <target name="prepare" depends="clean">
  102. <copy todir="${pro.build.path}">
  103. <fileset dir="${src.dir}">
  104. <include name="**/*.properties" />
  105. <include name="**/*.xml" />
  106. </fileset>
  107. </copy>
  108. </target>
  109. <target name="clean">
  110. <delete>
  111. <fileset dir="${pro.build.path}">
  112. <include name="**/*.*" />
  113. </fileset>
  114. <fileset dir="report">
  115. <include name="**/*.*" />
  116. </fileset>
  117. <fileset dir="instrumented">
  118. <include name="**/*.*" />
  119. </fileset>
  120. </delete>
  121. </target>
  122. <path id="master-classpath">
  123. <fileset dir="lib">
  124. <include name="*.jar" />
  125. </fileset>
  126. <pathelement path="${pro.build.path}" />
  127. </path>
  128. </project>

 

Cobertura 是敏捷程序员工具箱中新增的一个重要工具。通过生成代码覆盖率的具体数值,Cobertura 将单元测试从一种艺术转变为一门科学。它可以寻找测试覆盖中的空隙,直接找到 bug。测量代码覆盖率使您可以获得寻找并修复 bug 所需的信息,从而开发出对每个人来说都更健壮的软件。

尽管测试先行编程(test-first programming)和单元测试已不能算是新概念,但测试驱动的开发仍然是过去 10 年中最重要的编程创新。最好的一些编程人员在过去半个世纪中一直在使用这些技术,不过,只是在最近几年,这些技术才被广泛地视为在时间及成本预算内开发健壮的无缺陷软件的关键所在。但是,测试驱动的开发不能超过测试所能达到的程度。测试改进了代码质量,但这也只是针对实际测试到的那部分代码而言的。您需要有一个工具告诉您程序的哪些部分没有测试到,这样就可以针对这些部分编写测试代码并找出更多 bug。 

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

评论(1)

  1. #1 楼 · linuxcoffee · 2007-12-25 15:03

    我这里遇到一个编码问题,是gbk的java文件。在报表中看到就是乱码了,要是uff-8的就没有这个问题

评论功能维护中,当前仅展示历史评论。