使用 Cobertura 报告代码覆盖率
现在已经编写了一些测试,如何确定所有这些测试执行什么
呢?幸运的是,此问题可由像 Cobertura 这样的代码覆盖工具来解答。代码覆盖工具可报告测试覆盖率 —— 以行覆盖或分支覆盖形式表示 —— 它表示测试运行时所涉及的代码量。
清单 8 展示了一个 Ant 脚本。该脚本使用 Cobertura 生成一份关于代码覆盖率的 HTML 报告,代码覆盖率通过运行一系列 JUnit 测试获得
Ant的 build.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project name="cobertura.examples.basic" default="coverage" basedir=".">
<description>
Cobertura - http://cobertura.sourceforge.net/
Copyright (C) 2003 jcoverage ltd.
Copyright (C) 2005 Mark Doliner <thekingant@users.sourceforge.net>
Copyright (C) 2006 Dan Godfrey
Cobertura is licensed under the GNU General Public License
Cobertura comes with ABSOLUTELY NO WARRANTY
</description>
<property file="build.properties" />
<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"/>
<target name="init">
<mkdir dir="${classes.dir}" />
<mkdir dir="${instrumented.dir}" />
<mkdir dir="${reports.xml.dir}" />
<mkdir dir="${reports.html.dir}" />
<mkdir dir="${coverage.xml.dir}" />
<mkdir dir="${coverage.summaryxml.dir}" />
<mkdir dir="${coverage.html.dir}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes">
<classpath refid="cobertura.classpath" />
</javac>
</target>
<target name="instrument" depends="init,compile">
<!--
Remove the coverage data file and any old instrumentation.
-->
<delete file="cobertura.ser"/>
<delete dir="${instrumented.dir}" />
<!--
Instrument the application classes, writing the
instrumented classes into ${build.instrumented.dir}.
-->
<cobertura-instrument todir="${instrumented.dir}">
<!--
The following line causes instrument to ignore any
source line containing a reference to log4j, for the
purposes of coverage reporting.
-->
<ignore regex="org.apache.log4j.*" />
<fileset dir="${classes.dir}">
<!--
Instrument all the application classes, but
don't instrument the test classes.
-->
<include name="**/*.class" />
<exclude name="**/*Test.class" />
</fileset>
</cobertura-instrument>
</target>
<target name="test" depends="init,compile">
<junit fork="yes" dir="${basedir}" failureProperty="test.failed">
<!--
Note the classpath order: instrumented classes are before the
original (uninstrumented) classes. This is important.
-->
<classpath location="${instrumented.dir}" />
<classpath location="${classes.dir}" />
<!--
The instrumented classes reference classes used by the
Cobertura runtime, so Cobertura and its dependencies
must be on your 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>
<junitreport todir="${reports.xml.dir}">
<fileset dir="${reports.xml.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${reports.html.dir}" />
</junitreport>
</target>
<target name="coverage-check">
<cobertura-check branchrate="34" totallinerate="100" />
</target>
<target name="coverage-report">
<!--
Generate an XML file containing the coverage data using
the "srcdir" attribute.
-->
<cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />
</target>
<target name="summary-coverage-report">
<!--
Generate an summary XML file containing the coverage data using
the "srcdir" attribute.
-->
<cobertura-report srcdir="${src.dir}" destdir="${coverage.summaryxml.dir}" format="summaryXml" />
</target>
<target name="alternate-coverage-report">
<!--
Generate a series of HTML files containing the coverage
data in a user-readable form using nested source filesets.
-->
<cobertura-report destdir="${coverage.html.dir}">
<fileset dir="${src.dir}">
<include name="**/*.java"/>
</fileset>
</cobertura-report>
</target>
<target name="clean" description="Remove all files created by the build/test process.">
<delete dir="${classes.dir}" />
<delete dir="${instrumented.dir}" />
<delete dir="${reports.dir}" />
<delete file="cobertura.log" />
<delete file="cobertura.ser" />
</target>
<target name="coverage" depends="compile,instrument,test,coverage-report,summary-coverage-report,
alternate-coverage-report" description="Compile,
instrument ourself, run the tests and generate JUnit and coverage reports."/>
</project>
Ant的 build.properties文件内容如下:
# The source code for the examples can be found in this directory
src.dir=src
# The path to cobertura.jar
cobertura.dir=../..
# Classes generated by the javac compiler are deposited in this directory
classes.dir=classes
# Instrumented classes are deposited into this directory
instrumented.dir=instrumented
# All reports go into this directory
reports.dir=reports
# Unit test reports from JUnit are deposited into this directory
reports.xml.dir=${reports.dir}/junit-xml
reports.html.dir=${reports.dir}/junit-html
# Coverage reports are deposited into these directories
coverage.xml.dir=${reports.dir}/cobertura-xml
coverage.summaryxml.dir=${reports.dir}/cobertura-summary-xml
coverage.html.dir=${reports.dir}/cobertura-html
Cobertura 产生了一个如图 1 中所示的 HTML 报告。请注意行覆盖和分支覆盖的百分比是以包计算的。可单击每一个包,获得类级别的行百分比和路径百分比,甚至能看到执行的源代码行和它们执行的次数。
- 大小: 48.3 KB
分享到:
相关推荐
NULL 博文链接:https://terrencexu.iteye.com/blog/718834
Cobertura代码覆盖率测试工具介绍文档,Cobertura代码覆盖率测试工具介绍
Cobertura 是一种代码覆盖测试工具,它通过检测基本的代码,并观察在测试包运行时执行了哪些代码和没有执行哪些代码,来测量测试覆盖率。
cobertura和weblogic整合使用-代码覆盖率测试 里面详细步骤和使用说明,并附加xml文件,不懂的可以QQ我,147806015,
NULL 博文链接:https://mwhgjava.iteye.com/blog/1297276
Cobertura是一个测试案例覆盖率统计工具,此资源下载后,解压,配置上环境变量即可使用
示例build文件 博文链接:https://zhupan.iteye.com/blog/200878
cobertura代码覆盖率工具,有工具包,并且付上一份自己给项目组写的手册一份。
Cobertura是一种开源工具,它通过检测基本的代码,并观察在测试包运行时执行了哪些代码和没有执行哪些代码,来测量测试覆盖率。除了找出未测试到的代码并发现bug外,Cobertura还可以通过标记无用的、执行不到的代码...
cobertura测试代码
cobertura junit java 单元测试 测试驱动
代码测试覆盖率工具,该工具与ant执行集成,快速简单有效的测试代码的覆盖率,内附测试用例
Cobertura可用于分析Java代码的测试覆盖率。Cobertura根据没有被测试覆盖的代码生成基于HTML的报告。...通过监控可测试的代码,Cobertura允许你使用你选择的测试框架,甚至不需要测试框架就可以运行程序。
JSCoverage工具的增强版本,增加了一些功能,包括分支覆盖率、 LCOV和用于 CI 集成的Cobertura XML报告、用于自动化测试的钩子和HTML 本地存储,以在不使用 iFrame 或 JavaScript 打开的窗口的情况下维护覆盖率数据...
三者结合,利用EMMA产生的测试覆盖度报考,可以一目了然的知道那部分代码已经被测试,哪部分没有,从而控制单元测试的质量。 EMMA,它是一个免费、开源的、用来衡量单元测试覆盖度的工具。 三者结合,利用EMMA...
它也旨在通过pycobertura diff命令来防止代码覆盖率降低:任何更改的行都应进行测试,并且未覆盖的更改应清晰可见,而不会让遗留的未覆盖代码成为pycobertura diff因此开发人员可以仅专注于其更改。 特征: 显示...
cobertura-2.1.1版本,主要用于统计单元测试的覆盖率。
在测试驱动开发中,单元测试非常重要,而测试代码的分支覆盖率和行覆盖率同样重要,目前比较流行的单元测试工具为junit,而覆盖率测试工具cobertura非常好用,现在将2者集成到一块,个人感觉在现实的开发中还是比较...