下面通过一个完整的脚本样例来说明如何编写Ant脚本执行一个项目的单元测试用例和输出单元测试报告。
一、定义源代码目录和编译输出目录和classpath等变量定义 | Variable Definitions
2、业务源代码编译后输出目录(target.java.dir),单元测试源代码编译后输出目录(target.unit-test.dir),Cobertura打点后的输出目录(target.cover-test.dir)。
3、第三方依赖库目录。
4、单元测试执行报告输出目录(target.unit-test.dir),单元测试覆盖率报告输出目录(target.cover-test-report.dir)。
5、业务源代码编译所需的classpath(app.classpath),单元测试源代码编译所需的classpath(app.test.classpath)。
二、编译业务源代码和单元测试源代码 | Compile Code
三、执行单元测试用例并生成单元测试报告 | Unit Test And Generate Report
<junit>标签配置属性说明:
- printsummary - 为每一个测试用例通过System.out输出一行统计信息,如果出错或测试失败则通过System.err输出信息。
- haltonerror - 执行单元测试出错时停止执行的后续用例。
- haltonfailure - 执行单元测试失败时停止执行的后续用例。
- fork - 在独立的VM中执行单元测试用例。
- type - 单元测试用例执行结果输出格式。可选项有:plain, xml, brief 或 failure
- usefile - 单元测试用例执行结果是否输出至文件
- todir - 单元测试用例执行结果输出目录。
2、根据每个用例的执行结果文件 Test-*.xml输出单元测试报告。
<junitreport>标签配置属性说明:
- todir - 单元测试报告输出目录。
四、执行脚本并查看单元测试报告 | Execute Script And View Report
在命令行中输入:
ant unit-test-report ,最终输出的单元测试报告类似如下:
附录:完整的Ant脚本build.xml样例
<?xml version="1.0" encoding="UTF-8" ?>
<project name="busimonitor" basedir=".">
<property name="src.java.dir" location="${basedir}/app" />
<property name="src.test.dir" location="${basedir}/test" />
<property name="target.dir" location="${basedir}/classes" />
<property name="target.java.dir" location="${target.dir}/java" />
<property name="target.unit-test.dir" location="${target.dir}/unit-test" />
<property name="target.cover-test.dir" location="${target.dir}/cover-test" />
<property name="src.extend.lib.dir" location="${basedir}/lib"/>
<property name="play.lib.dir" location="/devdata/projects/game/play-1.2.5/framework" />
<property name="target.report.dir" location="${basedir}/report" />
<property name="target.unit-test-report.dir" location="${target.report.dir}/unit-test" />
<property name="target.cover-test-report.dir" location="${target.report.dir}/cover-test" />
<path id="app.classpath">
<fileset dir="${play.lib.dir}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${src.extend.lib.dir}">
<include name="*.jar" />
</fileset>
<path location="${target.java.dir}" />
<path location="${basedir}/conf" />
</path>
<path id="app.test.classpath">
<path location="${target.unit-test.dir}" />
</path>
<target name="compile.java">
<mkdir dir="${target.java.dir}" />
<javac srcdir="${src.java.dir}" destdir="${target.java.dir}"
debug="on" source="1.6" includeantruntime="on">
<classpath refid="app.classpath"></classpath>
</javac>
</target>
<target name="compile.test" depends="compile.java">
<mkdir dir="${target.unit-test.dir}" />
<javac srcdir="${src.test.dir}" destdir="${target.unit-test.dir}"
debug="on" source="1.6" includeantruntime="on">
<classpath refid="app.classpath"></classpath>
<classpath refid="app.test.classpath"></classpath>
</javac>
<copy todir="${target.unit-test.dir}">
<fileset dir="${src.test.dir}">
<include name="**/*.properties" />
<include name="**/*.xml" />
</fileset>
</copy>
</target>
<target name="compile" depends="compile.java, compile.test"></target>
<target name="unit-test" depends="compile">
<mkdir dir="${target.unit-test-report.dir}" />
<junit printsummary="on" haltonerror="off"
haltonfailure="off" fork="on">
<formatter type="plain" usefile="off"/>
<formatter type="xml" usefile="on" />
<batchtest todir="${target.unit-test-report.dir}">
<fileset dir="${target.unit-test.dir}">
<include name="**/*Test.class"/>
</fileset>
</batchtest>
<classpath refid="app.classpath"></classpath>
<classpath refid="app.test.classpath"></classpath>
</junit>
</target>
<target name="unit-test-report" depends="unit-test">
<mkdir dir="${target.unit-test-report.dir}/html" />
<junitreport todir="${target.unit-test-report.dir}">
<fileset dir="${target.unit-test-report.dir}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${target.unit-test-report.dir}/html" />
</junitreport>
</target>
</project>
参考资料 | References
附其他功能:
<!-- ==================== "make file to zip" Target ================================ -->
<target name="make_data_zip" depends="coverage-report">
<tstamp>
<format property="date" pattern="yyyy-MM-dd HH-mm" />
</tstamp>
<jar jarfile="dist/lib/cobertura${date}.zip" basedir="cobertura" />
<jar jarfile="dist/lib/report${date}.zip" basedir="report" excludes="*.xml"/>
</target>
<!-- ==================== "mail" Target ================================ -->
<target name="mail" depends="make_data_zip">
<!-- <taskdef name="mail" classname="org.apache.tools.ant.taskdefs.optional.mail.MimeMail"/> -->
<tstamp/>
<mail messageMimeType="text/html"
messageFile="message.txt"
tolist="bpcjy@hotmail.com"
mailhost="mailsvr or mail IPAddress"
subject="JUnit Test Results: ${TODAY}"
from="bpcjy@hotmail.com">
<fileset dir=".">
<include name="dist/lib/*.zip"/>
</fileset>
</mail>
</target>
ant build文件详细信息
关键Ant的build文件如下(已加上比较详细的说明)
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
<?
xml
version
=
"1.0"
encoding
=
"GBK"
?>
<
project
basedir
=
"."
default
=
"main"
name
=
"excute TestCase and build test report"
>
<!-- 测试报告存放目录 -->
<
property
name
=
"build.reports.dir"
value
=
"${basedir}/report"
/>
<
target
name
=
"main"
>
<!-- 删除测试报告数据,重新生成 -->
<
SPAN
id
=
more
-600></
SPAN
>
<
delete
>
<
fileset
dir
=
"${basedir}/report"
>
<
include
name
=
"*.*"
/>
</
fileset
>
</
delete
>
<
junit
fork
=
"yes"
printsummary
=
"true"
>
<!-- 生成的class目录以及执行TestCase所依赖的库
无论是用<test>还是<batchtest>都要这个配置 -->
<
classpath
location
=
"${basedir}/bin"
/>
<!-- 生成报告数据的格式,可能多个,支持xml/brief/plain -->
<
formatter
type
=
"xml"
/>
<
formatter
type
=
"brief"
usefile
=
"false"
/>
<!-- 可以用<test>也可以用<batchtest>,但两种的设置有一些区别
以下<test>和<batchtest>三种形式用某一种就可以的 -->
<!-- name指定Class的名称,如CatTest或com.unmi.CatTest -->
<
test
name
=
"CatTest"
todir
=
"${build.reports.dir}"
/>
<!-- 注意其中<fileset>的dir属性及<include>的name属性指代的意义 -->
<
batchtest
todir
=
"${build.reports.dir}"
>
<!-- dir属性指定TestCase类的源代码的路径 -->
<
fileset
dir
=
"${basedir}/src"
>
<!-- name属性指定TestCase源文件规则 -->
<
include
name
=
"**/*Test.java"
/>
</
fileset
>
</
batchtest
>
<!-- 上面的<batchtest>还可以写成如下形式,<fileset>按指定为class
注意其中<fileset>的dir属性及<include>的name属性指代的意义 -->
<
batchtest
todir
=
"${build.reports.dir}"
>
<!-- dir属性指定TestCase类的路径 -->
<
fileset
dir
=
"${basedir}/bin"
>
<!-- name属性指定TestCase类文件规则 -->
<
include
name
=
"**/*Test.class"
/>
</
fileset
>
</
batchtest
>
</
junit
>
<!-- 用执行以上TestCase生成的报告数据生成测试报告 -->
<
junitreport
todir
=
"${build.reports.dir}"
>
<
fileset
dir
=
"${build.reports.dir}"
>
<
include
name
=
"TEST-*.xml"
/>
</
fileset
>
<!-- 指定生成测试报告的格式frames/noframes,和报告存放目录 -->
<
report
format
=
"frames"
todir
=
"${build.reports.dir}"
/>
</
junitreport
>
</
target
>
</
project
>
|
本文介绍如何使用JUnit和Ant进行Java项目的自动化单元测试,包括定义目录结构、编译代码、执行测试并生成测试报告。
4091

被折叠的 条评论
为什么被折叠?



