用JCoverage生成测试覆盖率报告(二 build文件)
下面的那一大段build.xml文件内容主要也是参考下载过来的JCoverage中的一个例子中的build文件,只是稍稍作了点修改,用下面这个build文件前,需要把JCoverage用到的 jar (下载的JCoverage中都有) 包拷到工程目录下lib子目录中,请注意理解其中的注解,以后必要时会加上更详细的中文注释。
下面也许有必要粘贴一些用JCoverage生成的覆盖率报告界面图来。 永久链接 https://yanbin.blog/jcoverage-report-build-file/, 来自 隔叶黄莺 Yanbin's Blog
[版权声明]
本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。
1<?xml version="1.0" encoding="gb2312"?>
2
3<project name="jcoverage.examples" default="main" basedir=".">
4
5 <!--
6 all build artefacts are deposited under this directory.
7 -->
8 <property name="build.dir" value="${basedir}/build"/>
9
10 <!--
11 classes generated by the javac compiler are deposited in this
12 directory.
13 -->
14 <property name="build.classes.dir" value="${build.dir}/classes"/>
15
16 <!--
17 instrumented classes are deposited into this directory.
18 -->
19 <property name="build.instrumented.dir" value="${build.dir}/instrumented-classes"/>
20
21 <!--
22 coverage reports are deposited into this directory. For the
23 HTML report, look at ${build.coverage.dir}/index.html. For the XML
24 report look at ${build.coverage.dir}/coverage.xml.
25 -->
26 <property name="build.coverage.dir" value="${build.dir}/coverage"/>
27
28 <!--
29 unit test reports from junit are deposited in this directory.
30 -->
31 <property name="build.reports.dir" value="${build.dir}/reports"/>
32
33 <!--
34 third party libraries that are also shipped with jcoverage can be
35 found in this directory.
36 JCoverageLib directory contains bcel.jar, jakarta-oro-2.0.7.jar
37 java-getopt-1.0.9.jar, jcoverage.jar, junit,log4j-1.2.8.jar
38 -->
39 <property name="jcoverage.lib.dir" value="${basedir}/JCoverageLib"/>
40
41 <!--
42 the source code for the examples can be found in this directory.
43 -->
44 <property name="src.dir" value="${basedir}/src"/>
45
46 <target name="main" depends="clean,init,compile,instrument,test,coverage" description="clean build, instrument and unit test"/>
47
48 <!-- libraries path, can define a universal classpath as below
49 <path id="junit">
50 <fileset dir="${lib.dir}">
51 <include name="junit/3.8.1/*.jar"/>
52 </fileset>
53 </path>
54
55 <path id="log4j">
56 <fileset dir="${lib.dir}">
57 <include name="log4j/1.2.8/*.jar"/>
58 </fileset>
59 </path>
60
61 <path id="jcoverage">
62 <fileset dir="${dist.dir}">
63 <include name="jcoverage.jar"/>
64 </fileset>
65 </path>
66 -->
67
68 <!--
69 define class path for all the library needed
70 -->
71 <path id="jcoverage.class.path">
72 <fileset dir="${jcoverage.lib.dir}">
73 <include name="*.jar"/>
74 </fileset>
75 </path>
76
77 <!-- 现在还不知道resource指定的tasks.properties(不存在)是做什么用的 -->
78 <taskdef classpathref="jcoverage.class.path" resource="tasks.properties"/>
79
80 <target name="clean" description="clean up build artefacts">
81 <delete quiet="true">
82 <fileset dir="${build.dir}"/>
83 <fileset dir="${basedir}">
84 <include name="jcoverage.ser"/>
85 <include name="jcoverage.log"/>
86 </fileset>
87 </delete>
88 </target>
89
90 <target name="init" description="create build directories">
91 <mkdir dir="${build.dir}"/>
92 <mkdir dir="${build.classes.dir}"/>
93 <mkdir dir="${build.coverage.dir}"/>
94 <mkdir dir="${build.instrumented.dir}"/>
95 <mkdir dir="${build.reports.dir}"/>
96 </target>
97
98 <target name="compile" description="compile all classes">
99 <javac srcdir="${src.dir}" destdir="${build.classes.di*}" **ilonerror="yes" debug="yes">
100 <!--
101 <classpath refid="junit"/>
102 <classpath refid="log4j"/>
103 -->
104 <classpath refid="jcoverage.class.path"/>
105 </javac>
106 </target>
107
108 <target name="instrument" description="Add jcoverage instrumentation">
109 <!--
110 instrument the application classes, writing the instrumented
111 classes into ${build.instrumented.dir}.
112 -->
113 <instrument todir="${build.instrumented.dir}">
114 <!--
115 Note that the following line causes instrument to ignore any
116 source line containing a reference to log4j, for the purposes
117 of coverage reporting.
118 -->
119 <ignore regex="org.apache.log4j.*"/>
120
121 <fileset dir="${build.classes.dir}">
122 <!--
123 instrument all the application classes, but don't instrument
124 the test classes.
125 -->
126 <include name="**/*.clas*"/>
127 <**clude name="**/*Test.class"/>
128 </fileset>
129 </instrument>
130 </target>
131
132 <target name="test" description="Unit test the application">
133 <junit fork="yes" dir="${basedir}" errorProperty="test.failed" failureProperty="test.failed">
134 <!--
135 note the classpath order, instrumented classes are before the
136 original (uninstrumented) classes.
137 -->
138 <classpath location="${build.instrumented.dir}"/>
139 <classpath location="${build.classes.dir}"/>
140
141 <!--
142 the instrumented classes reference classes used by the
143 jcoverage runtime.
144 -->
145 <!--
146 <classpath refid="jcoverage"/>
147 -->
148 <classpath refid="jcoverage.class.path"/>
149
150 <formatter type="xml"/>
151
152 <batchtest todir="${build.reports.dir}">
153 <fileset dir="${src.dir}">
154 <include name="**/*Test.java"/>
155 </fileset>
156 </batchtest>
157 </junit>
158 </target>
159
160 <target name="coverage" description="HTML and XML coverage reports can be found in build/coverage">
161 <report srcdir="${src.dir}" destdir="${build.coverage.dir}"/>
162 <report srcdir="${src.dir}" destdir="${build.coverage.dir}" format="xml"/>
163 </target>
164</project>下面也许有必要粘贴一些用JCoverage生成的覆盖率报告界面图来。 永久链接 https://yanbin.blog/jcoverage-report-build-file/, 来自 隔叶黄莺 Yanbin's Blog
[版权声明]
本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。