zoukankan      html  css  js  c++  java
  • Java測试覆盖率工具----Cobertura,EclEmma


    Cobertura 是一个与Junit集成的代码覆盖率測量工具

    免费、开源的

    它能够与Ant和Maven集成。也能够通过命令行调用

    能够生成HTML或XML格式的报告

    能够依照不同的标准对HTML结果进行排序

    为每一个类、包以及整个项目计算所覆盖的代码行与代码分支的百分比例

    原创文章。版权全部,同意转载,标明出处:http://blog.csdn.net/wanghantong

    Eclipse插件地址: http://ecobertura.johoop.de/update/ (requires Eclipse 3.5+)


    使用Ant来运行Cobertura

    操作步骤:

    1.加入核心依赖jar包---

    2.在 build.xml 文件里加入一个任务定义。下面这个顶级taskdef 元素将 cobertura.jar 文件限定在当前工作文件夹中:

    <taskdef classpath="cobertura.jar" resource="tasks.properties" />
    3.被測量的类必须在原始类出如今类路径中之前出如今类路径中,并且须要将 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>>

    4.cobertura-report 任务生成測试报告 HTML 文件:

    <target name="coverage-report" depends="cover-test">
     <cobertura-report srcdir="src/java/main" destdir="cobertura"/>
    </target>
    5.srcdir 属性指定原始的 .java 源码在什么地方。destdir 属性指定 Cobertura 放置输出 HTML 的那个文件夹的名称。

    在自己的 Ant 编译文件里增加了类似的任务后,就能够通过键入下面命令来生成一个覆盖报告:

    % ant instrument
    % ant cover-test
    % ant coverage-report


    在Java測试覆盖率工具上。另一个更加简单的工具:EclEmma(推荐) ,笔者眼下也在使用EclEmma,它能够非常方便的与Eclipse集成,然后能够直接run,显示出代码覆盖率,其地址是:http://www.eclemma.org/

    我们能够在Eclipse的MarketPlace中直接搜索并下载安装

    在这里我就只是多介绍了,有兴趣的同学能够自己尝试。

    原创文章,版权全部。同意转载。标明出处:http://blog.csdn.net/wanghantong

    ——不要太高估自己在集体中的力量。由于当你选择离开时,就会发现即使没有你,太阳照常升起!

  • 相关阅读:
    <<一线架构师实践指南>>读书笔记之二PA阶段
    【读书笔记】简约至上交互设计四策略第4章 删除
    大数据量简单数据查询设计思考
    识别项目干系人
    【读书笔记】简约至上交互设计四策略第3章 简约四策略
    【读书笔记】简约至上交互设计四策略第2章 明确认识
    【读书笔记】简约至上交互设计四策略第1章 话说简单
    采购管理计划
    项目管理整体的一些基本概念1
    【读书笔记】简约至上交互设计四策略第5章 组织
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5203135.html
Copyright © 2011-2022 走看看