1. 程式人生 > >孩子,你還得練,還得練! 記錄生活點點滴滴 讓程式更精彩

孩子,你還得練,還得練! 記錄生活點點滴滴 讓程式更精彩

官方:http://cobertura.sourceforge.net/


Cobertura是一個基於jcoverage的免費Java工具,它能夠顯示哪一部分程式碼被你的測試所覆蓋,並可生成HTML或XML 報告.



cobertura 的大概基本工作思路:
1.對已經編譯好的class 檔案新增標記
2. 對新增好標記的程式碼進行單元測試
3. 輸出覆蓋率統計報告

在ant 中使用cobertura 的基本步驟:
1. 編譯程式碼
2. 定義cobertura 的ant task
3. 用nstrument 命令為編譯好的程式碼新增標記
4. 用junit 命令對新增好標記的程式碼進行單元測試
5. 用cobertura-report 命令輸出報告


build.properties

#原始碼目錄
src.dir=src
#測試程式碼目錄
test.dir=test

# The path to cobertura.jar
cobertura.dir=C:\\cobertura-1.9

# Classes generated by the javac compiler are deposited in this directory
# Classes所產生的javac編譯器都存放在這個目錄
classes.dir=classes

# Instrumented classes are deposited into this directory
# 所裝入classes的存入這個目錄
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.html.dir=${reports.dir}/cobertura-html

junit3.dir=D:\\junit3.8.1

junit4.dir=D:\\junit4.4

#資料庫驅動的目錄
jdbc.dir=path


build.xml


<?xml version="1.0" encoding="UTF-8"?>

<project name="測試專案" default="coverage" basedir=".">

<property file="build.properties" />

<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="cobertura.jar" />
<include name="lib/**/*.jar" />
</fileset>
</path>

<path id="junit3.classpath">
<fileset dir="${junit3.dir}">
<include name="junit.jar" />
</fileset>
</path>

<path id="junit4.classpath">
<fileset dir="${junit4.dir}">
<include name="junit4.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.html.dir}" />
</target>

<target name="compile" depends="init">
//原始碼
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes">
<classpath refid="cobertura.classpath" />
<classpath refid="junit3.classpath" />
<classpath refid="junit4.classpath" />

</javac>
//測試程式碼
<javac srcdir="${test.dir}" destdir="${classes.dir}" debug="yes">
<classpath refid="cobertura.classpath" />
<classpath refid="junit3.classpath" />
<classpath refid="junit4.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" />
<exclude name="**/Test.class"/>
<exclude name="com/pattern/**/*.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}" />
<classpath refid="junit3.classpath"></classpath>
<classpath refid="junit4.classpath"></classpath>
<classpath location="${jdbc.dir}"></classpath>

<!--
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="${test.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="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="clean,compile,instrument,test,coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports." />

</project>



具體看官方文件