zoukankan      html  css  js  c++  java
  • XP项目配置管理(3)——分析研究篇 完

     

    1、代码覆盖率(Code Coverage

    代码覆盖率也称为测试覆盖率,是评价测试活动覆盖产品代码的指标。测试覆盖率评价的是测试代码的质量,并不是产品代码的质量。因此,测试覆盖率可以看作是产品代码质量的间接指标。

    ·衡量测试覆盖率的指标很多,常用的指标有:

    Statement coverage,也称作Line coverage,用于评价测试的代码语句覆盖率。

    Basic block coverage,是Statement coverage的一个变种,它把没有一个分支的代码区域作

    为一个计量单位,而不是简单的代码行,用于一个if-else分支代码行数远远大于另一个的情况,在这种情况下,statement coverage指标并不适用。

    Decision coverage(也称作Branch coverage),用于评价代码分支地测试覆盖率。

    Path coverage,和Decision coverage相似,用于评价代码从开始到结束所有路径的测试覆

    盖率。

    Function coverage,用于评价代码方法的测试覆盖率。

    ·实现测试服务覆盖率的技术通常分为两种:

    1Instrumentation

    Instrumentation技术在产品代码的关键位置插入统计代码。事实上,Instrumentation技术可以分为两种方式:Class InstrumentationSource Instrumentation。前者把统计代码插入编译好的.class文件,而后者则把统计代码插入源代码并编译成新的.class文件。大多数测试覆盖率工具采用这两种Instrumentation技术。

    2Custom JVM

    另一种方式是在JVM中把统计代码插入.class。测试覆盖率分析可以在JVM执行测试代码的过程中完成。

    ·测试覆盖率工具的典型特性:

    1、和Ant集成

    2、多种报告输出格式

    3、源代码链接

    4、覆盖率历史报告

     

    1.1  Emma

    1.1.1 EMMA的特点

    ·    Instrumentatiton方式:EMMA使用两种模式来实现覆盖率的统计,它称作“offline”“on-the-fly”EMMA使用Instrumentation .class文件的方式。EMMA通过byte instrumentation生成.class文件的增强版本,加入了统计测试覆盖率的代码Hook。对于“offline”模式,它从硬盘读入.class文件,然后输出经过Instrumented的增强版本;对于“on-the-fly”模式,这些操作发生在JVM内部(即增强版本的.class文件不写入硬盘)。前者是通用的模式,而后者用于简单的Java应用程序。

    ·    支持的覆盖率指标:EMMA支持classmethodlinebasic block coverage指标。

    ·    优越的性能和可伸缩性。

    ·    Java平台支持:EMMA支持Java 1.2或更高版本的JVM,不依赖于任何第三方类库。

    ·    CPL License

    1.1.2 EMMA目前支持四种Coverage类型:classmethodblockline。(如下图)


    EMMA
    不仅仅包括对一个类的总结(如上图),还对包有总结(如下图)。

    COVERAGE SUMMARY FOR PACKAGE 


    Emma
    可以关联覆盖率到代码,绿色标示的行表示已经覆盖,红色表示没有覆盖,黄色表示部分覆盖。

    对于黄色行的意义,EMMA Frequently Asked Questions描述如下:

    What are those yellow lines in the HTML report?

    They are Java source lines that EMMA determined to have been only partially covered. They usually mean that there is a branch in your code that was not exercised, although EMMA presents no direct way of seeing in the report which branch it is.

    In some rare cases, a line could be split across several methods or classes and could be partially covered because one of those methods or classes has not been covered.


    1.1.3 Emma
    的使用

    1.1.3.1Ant的集成

    可以建立两个targettarget emma用作使用emma的开关,target emmarun运行测试覆盖率,并产生覆盖率报告。

    需要注意的是,emma需要指定一个主程序类用于运行;这就需要定义测试组合包含所有的测试类。这就是为什么需要在每个包都建立一个testsuit包含所有的testcase,并在项目根目录下建立一个testsuit用于包含所有包下的testsuit

    <target name="emma" description="turns on EMMA's on-the-fly instrumentation mode" >

        <property name="emma.enabled" value="true" />

        <property name="emma.filter" value="" />

    </target>

    <target name="emmarun" depends="init,compile,test,emma" description="emmaruns the examples" > 

        <emmajava enabled="${emma.enabled}" libclasspathref="classpath" fullmetadata="yes" filter="${emma.filter}" sourcepath="${src.dir}" classname="AllTests" classpathref="emmarun.classpath">

          <arg value="somearg" />

          <txt outfile="${coverage.dir}/coverage.txt" />

          <html outfile="${coverage.dir}/coverage.html"  />

        </emmajava>

      </target>

    1.1.3.2 命令行

    Windows下,可以建立一个bat文件,包含如下两行:

    java -cp .\lib\junit.jar;.\lib\emma.jar emmarun -r html -sp .\test -cp .\bin com.hitachi.tdd.addnewemployer.AllTests

    -cp classpath

    -r, -report (txt|html|xml)...

    -sp, -sourcepath list of source directories...

    关于参数的意义,情参考emma的文档。

     

    1.2  cobertura

    Cobertura jcoverage 的分支(请参阅 参考资料)。GPL 版本的 jcoverage 已经有一年没有更新过了,并且有一些长期存在的 bugCobertura 修复了这些 bug。原来的那些 jcoverage 开发人员不再继续开发开放源码,他们转向开发 jcoverage 的商业版和 jcoverage+jcoverage+ 是一个从同一代码基础中发展出来的封闭源代码产品。

    Cobertura 是一种开源工具,它通过检测基本的代码,并观察在测试包运行时执行了哪些代码和没有执行哪些代码,来测量测试覆盖率。除了找出未测试到的代码并发现 bug 外,Cobertura 还可以通过标记无用的、执行不到的代码来优化代码,还可以提供 API 实际操作的内部信息。

    1.2.1 Cobertura目前支持两种Coverage类型:LineBranch,另外包括对类的个数的统计和复杂度的计算。(如下图)




    Cobertura
    能记录某行的运行次数。


    1.2.2
    运行 Cobertura

    Cobertura 被设计成为在 Ant 中运行。现在还没有这方面的 IDE 插件可用,不过一两年内也许就会有了。

    首先需要在 build.xml 文件中添加一个任务定义。以下这个顶级 taskdef 元素将 cobertura.jar 文件限定在当前工作目录中:

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

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

    <target name="instrument">
      <cobertura-instrument todir="target/instrumented-classes">
        <fileset dir="target/classes">
          <include name="**/*.class"/>
        </fileset>
      </cobertura-instrument>
    </target>

    用通常运行测试包的同一种类型的 Ant 任务运行测试。惟一的区别在于:被测量的类必须在原始类出现在类路径中之前出现在类路径中,而且需要将 Cobertura JAR 文件添加到类路径中:

    <target name="cover-test" depends="instrument">
      <mkdir dir="${testreportdir}" />
      <junit dir="./" failureproperty="test.failure" printSummary="yes" 
             fork="true" haltonerror="true">
        <!-- Normally you can create this task by copying your existing JUnit
             target, changing its name, and adding these next two lines.
             You may need to change the locations to point to wherever 
             you've put the cobertura.jar file and the instrumented classes. -->
        <classpath location="cobertura.jar"/>
        <classpath location="target/instrumented-classes"/>
        <classpath>
          <fileset dir="${libdir}">
            <include name="*.jar" />
          </fileset>
          <pathelement path="${testclassesdir}" />
          <pathelement path="${classesdir}" />
        </classpath>
        <batchtest todir="${testreportdir}">
          <fileset dir="src/java/test">
            <include name="**/*Test.java" />
            <include name="org/jaxen/javabean/*Test.java" />
          </fileset>
        </batchtest>
      </junit>
    </target>
    最后,cobertura-report 任务生成本文开始部分看到的那个 HTML 文件:
    <target name="coverage-report" depends="cover-test">
     <cobertura-report srcdir="src/java/main" destdir="cobertura"/>
    </target>
    srcdir 属性指定原始的 .java 源代码在什么地方。destdir 属性指定 Cobertura 放置输出 HTML 的那个目录的名称。
    在自己的 Ant 编译文件中加入了类似的任务后,就可以通过键入以下命令来生成一个覆盖报告:
    % ant instrument
    % ant cover-test
    % ant coverage-report

     

    1.3   clover

    大名鼎鼎的CloverClover功能强劲,理应是测试覆盖率的首选工具。但Clover用于商业开发是要收费的。

    1.3.1 clover目前支持三种Coverage类型:Conditionals Statements Methods


    Clover
    也可以计算代码行的覆盖次数。


    1.3.2
    使用clover

           Clover的使用有多种方式,与ant集成,eclipse插件等。由于clover是商业的,只能从clover主页上下载试用版,可以用18天。

           这次使用的是clover eclipse插件。装好插件后,打开clover view,就可以对每一个项目使用clover做覆盖率测试了。

    2、代码度量(Code Metrics

           Maven也有metrics,但只有区区10项。当然最好还是用togethermetrics,功能非常强大,也比较可信。可以参见《Metrics Item》文档。

     

    3、代码审查(Code Review

    Code Review是一种通过复查代码提高代码质量的过程,在XP方法中占有极为重要的地位,也已经成为软件工程中一个不可缺少的环节。出自《Code Review理论与实战

    CheckStyle Code ReviewCheckStyle

    Jupiter

           这两个软件没有用到,所以没有去研究。

     

    4CVS监控

    CVS的监控,主要是通过生成和分析cvschange log,和利用cvs的触发器来处理一些事务。

    4.1 cvs log

           Ant预定义了一个cvschangelog任务用于生成CVS的变更日志。

        <cvschangelog dir="TDD_WAMS" destfile="changelog_TDD.xml">

          </cvschangelog>

          <style in="changelog_TDD.xml" out="changelog_TDD.html"

    style="${ant.home}/etc/changelog.xsl">

              <param name="title" expression="TDD Ant ChangeLog"/>

              <param name="module" expression="ant"/>

              <param name="cvsweb" expression="http://cvs.apache.org/viewcvs/"/>

            </style>

     

    4.2 StatCVS

    StatCVS generates HTML reports from CVS repository logs.详见StatCVS review.doc.

    StatCVS的用法见StatCVS Manual.mht

     

    4.3 CVS触发器

           见《CVS触发器的使用

     5bugzilla扩展

           如何更方便的报告bugbugzilla,以及统计bugzillabug数据。

    51 bugzilla客户端插件

           bugzilla eclipse plug-in.doc,主要其中比较了JagzillaBuglistBugzilla Integration Plugin for EclipsmyEclipseeclipse平台下作为bugzilla客户端的使用。

    52 bugzilla统计

           自己编写的程序,用来自动统计bug的一些相关数据。见程序说明。

  • 相关阅读:
    Robotium源码分析之Instrumentation进阶
    路飞学城Python-Day115
    路飞学城Python-Day114
    路飞学城Python-Day113
    【逻辑回归的特征筛选方法】
    路飞学城Python-Day108
    路飞学城Python-Day107
    【算法学习】神经网络
    路飞学城Python-Day100
    路飞学城Python-Day101
  • 原文地址:https://www.cnblogs.com/markjiao/p/241348.html
Copyright © 2011-2022 走看看