• 使用cobertura确定测试代码的覆盖率


    这是某个美国大学的副教授写的关于cobertura来确定测试代码覆盖率的文章讲的很清楚,我就不偷了~直接把链接放在这里

    http://www.ibm.com/developerworks/cn/java/j-cobertura/

    这个是一个网友写的,可以配置IDE开发环境的

    http://www.blogjava.net/xmatthew/archive/2008/11/25/242658.html

    这是网友写的,个人觉得对配置很有指导意义:

    build.XML

    1.  1 <?xml version="1.0" encoding="UTF-8"?>   
    2.   2    
    3.   3 <project name="cobertura.examples.basic" default="coverage" basedir=".">   
    4.   4        
    5.   5     <!-- 引用 build.properties文件 配置路径信息-->   
    6.   6     <property file="build.properties" />   
    7.   7    
    8.   8     <!-- 设置 cobertura路径 -->   
    9.   9     <path id="cobertura.classpath">   
    10.  10         <fileset dir="${cobertura.dir}">   
    11.  11             <include name="cobertura.jar" />   
    12.  12             <include name="lib/**/*.jar" />   
    13.  13         </fileset>   
    14.  14     </path>   
    15.  15    
    16.  16     <!-- 配置 cobatura ant 扩展任务 -->   
    17.  17     <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>   
    18.  18    
    19.  19     <target name="init">   
    20.  20         <mkdir dir="${classes.dir}" />   
    21.  21         <mkdir dir="${instrumented.dir}" />   
    22.  22         <mkdir dir="${reports.xml.dir}" />   
    23.  23         <mkdir dir="${reports.html.dir}" />   
    24.  24         <mkdir dir="${coverage.xml.dir}" />   
    25.  25         <mkdir dir="${coverage.html.dir}" />   
    26.  26     </target>   
    27.  27    
    28.  28     <!-- 编译源代码 -->   
    29.  29     <target name="compile" depends="init">   
    30.  30         <javac srcdir="${src.dir}:${test.dir}:${src.conf.dir}:${test.conf.dir}" destdir="${classes.dir}" debug="yes">   
    31.  31             <classpath refid="cobertura.classpath" />   
    32.  32         </javac>   
    33.  33     </target>   
    34.  34    
    35.  35     <target name="instrument" depends="init,compile">   
    36.  36         <!--   
    37.  37             Remove the coverage data file and any old instrumentation.   
    38.  38         -->   
    39.  39         <delete file="cobertura.ser"/>   
    40.  40         <delete dir="${instrumented.dir}" />   
    41.  41    
    42.  42         <!--   
    43.  43             Instrument the application classes, writing the   
    44.  44             instrumented classes into ${build.instrumented.dir}.   
    45.  45         -->   
    46.  46         <cobertura-instrument todir="${instrumented.dir}">   
    47.  47             <!--   
    48.  48                 The following line causes instrument to ignore any   
    49.  49                 source line containing a reference to log4j, for the   
    50.  50                 purposes of coverage reporting.   
    51.  51             -->   
    52.  52             <ignore regex="org.apache.log4j.*" />   
    53.  53    
    54.  54             <fileset dir="${classes.dir}">   
    55.  55                 <!--   
    56.  56                     Instrument all the application classes, but   
    57.  57                     don't instrument the test classes.   
    58.  58                 -->   
    59.  59                 <include name="**/*.class" />   
    60.  60                 <exclude name="**/*Test.class" />   
    61.  61             </fileset>   
    62.  62         </cobertura-instrument>   
    63.  63     </target>   
    64.  64    
    65.  65     <!-- 单元测试 -->   
    66.  66     <target name="test" depends="init,compile">   
    67.  67         <junit fork="yes" dir="${basedir}" failureProperty="test.failed">   
    68.  68             <!--   
    69.  69                 Note the classpath order: instrumented classes are before the   
    70.  70                 original (uninstrumented) classes.  This is important.   
    71.  71             -->   
    72.  72             <classpath location="${instrumented.dir}" />   
    73.  73             <classpath location="${classes.dir}" />   
    74.  74    
    75.  75             <!--   
    76.  76                 The instrumented classes reference classes used by the   
    77.  77                 Cobertura runtime, so Cobertura and its dependencies   
    78.  78                 must be on your classpath.   
    79.  79             -->   
    80.  80             <classpath refid="cobertura.classpath" />   
    81.  81    
    82.  82             <formatter type="xml" />   
    83.  83             <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />   
    84.  84             <batchtest todir="${reports.xml.dir}" unless="testcase">   
    85.  85                 <fileset dir="${test.dir}">   
    86.  86                     <include name="**/*Test.java" />   
    87.  87                 </fileset>   
    88.  88                 <fileset dir="${src.dir}">   
    89.  89                     <include name="**/*Test.java" />   
    90.  90                 </fileset>                   
    91.  91             </batchtest>   
    92.  92         </junit>   
    93.  93    
    94.  94         <junitreport todir="${reports.xml.dir}">   
    95.  95             <fileset dir="${reports.xml.dir}">   
    96.  96                 <include name="TEST-*.xml" />   
    97.  97             </fileset>   
    98.  98             <report format="frames" todir="${reports.html.dir}" />   
    99.  99         </junitreport>   
    100. 100     </target>   
    101. 101    
    102. 102     <target name="coverage-check">   
    103. 103         <cobertura-check branchrate="34" totallinerate="100" />   
    104. 104     </target>   
    105. 105    
    106. 106     <!-- 生成 coverage xml格式报告 -->   
    107. 107     <target name="coverage-report">   
    108. 108         <!--   
    109. 109             Generate an XML file containing the coverage data using   
    110. 110             the "srcdir" attribute.   
    111. 111         -->   
    112. 112         <cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />   
    113. 113     </target>   
    114. 114    
    115. 115     <!-- 生成 coverage html格式报告 -->   
    116. 116     <target name="alternate-coverage-report">   
    117. 117         <!--   
    118. 118             Generate a series of HTML files containing the coverage   
    119. 119             data in a user-readable form using nested source filesets.   
    120. 120         -->   
    121. 121         <cobertura-report destdir="${coverage.html.dir}">   
    122. 122             <fileset dir="${src.dir}">   
    123. 123                 <include name="**/*.java"/>   
    124. 124             </fileset>   
    125. 125         </cobertura-report>   
    126. 126     </target>   
    127. 127    
    128. 128     <target name="clean" description="Remove all files created by the build/test process.">   
    129. 129         <delete dir="${classes.dir}" />   
    130. 130         <delete dir="${instrumented.dir}" />   
    131. 131         <delete dir="${reports.dir}" />   
    132. 132         <delete file="cobertura.log" />   
    133. 133         <delete file="cobertura.ser" />   
    134. 134     </target>   
    135. 135    
    136. 136     <target name="coverage" depends="compile,instrument,test,coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>   
    137. 137    
    138. 138 </project>   
    139. 139   

    build.properties

    1. 1 # The source code for the examples can be found in this directory   
    2.  2 src.dir=../src/java   
    3.  3 src.conf.dir=../src/conf   
    4.  4 test.dir=../test/java   
    5.  5 test.conf.dir=../src/conf   
    6.  6    
    7.  7 # The path to cobertura.jar   
    8.  8 cobertura.dir=cobertura   
    9.  9    
    10. 10 # Classes generated by the javac compiler are deposited in this directory   
    11. 11 classes.dir=../bin   
    12. 12    
    13. 13 # Instrumented classes are deposited into this directory   
    14. 14 instrumented.dir=instrumented   
    15. 15    
    16. 16 # All reports go into this directory   
    17. 17 reports.dir=reports   
    18. 18    
    19. 19 # Unit test reports from JUnit are deposited into this directory   
    20. 20 reports.xml.dir=${reports.dir}/junit-xml   
    21. 21 reports.html.dir=${reports.dir}/junit-html   
    22. 22    
    23. 23 # Coverage reports are deposited into these directories   
    24. 24 coverage.xml.dir=${reports.dir}/cobertura-xml   
    25. 25 coverage.html.dir=${reports.dir}/cobertura-html   
    26. 26   

     把出处放在这里,还是看原版的好:http://huangtut.javaeye.com/blog/297464

  • 相关阅读:
    记录某次应急演练
    C++之分文件编写
    C++之冒泡排序实现
    cobaitstrike之修改特征
    cobaitstrike之DNS上线
    网盘搜索【不断更新ing】
    CVE-2021-2109 Weblogic Server远程代码执行
    C++之一维&二维数组
    CDH-5.12.2安装教程
    linux安装mysql教程
  • 原文地址:https://www.cnblogs.com/ITEagle/p/1665615.html
Copyright © 2020-2023  润新知