整合步骤
1.创建属性
2.编译源文件
3.编译测试文件
4.运行单元测试
5.生成单元测试报告
代码总览
Calculator.java
package com.itlwc;
public class Calculator {
public int addition(int a, int b) {
return a + b;
}
}
CalculatorTest.java
package com.itlwc;
import junit.framework.TestCase;
public class CalculatorTest extends TestCase {
private Calculator c = null;
@Override
public void setUp() throws Exception {
c = new Calculator();
}
public void testAddition() {
int actual = c.addition(5, 3);
int expected = 8;
assertEquals(expected, actual);
}
@Override
public void tearDown() throws Exception {
c = null;
}
}
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="end">
<property name="bulid.dir" location="bulid" />
<property name="bulid.classes" location="${bulid.dir}/classes" />
<property name="bulid.test.dir" location="${bulid.dir}/test" />
<property name="bulid.test.classes" location="${bulid.test.dir}/classes" />
<property name="bulid.test.report" location="${bulid.test.dir}/report" />
<!--项目清理工作-->
<target name="clean">
<delete dir="${build.dir}" />
</target>
<!--创建文件夹-->
<target name="createFolders" depends="clean">
<!--创建bulid文件夹-->
<mkdir dir="${bulid.dir}" />
<!--创建bulid/classes文件夹-->
<mkdir dir="${bulid.classes}" />
<!--创建bulid/test文件夹-->
<mkdir dir="${bulid.test.dir}" />
<!--创建bulid/test/classes文件夹-->
<mkdir dir="${bulid.test.classes}" />
<!--测试报告bulid/test/report文件夹-->
<mkdir dir="${bulid.test.report}" />
</target>
<!--编译源文件-->
<property name="src.dir" location="src" />
<target name="compile" depends="createFolders">
<javac includeantruntime="true"
srcdir="${src.dir}"
destdir="${bulid.classes}" />
</target>
<!--指定架包路径-->
<property name="lib.dir" location="lib" />
<path id="compile-path">
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
<!-- 指定架包路径和源文件编译后的路径 -->
<path id="compile-test-path">
<path refid="compile-path" />
<pathelement location="${bulid.classes}" />
</path>
<!--编译测试文件需要指定架包路径和源文件编译后的路径-->
<property name="test.src.dir" location="test" />
<target name="compile-test" depends="compile">
<javac
includeantruntime="true"
srcdir="${test.src.dir}"
destdir="${bulid.test.classes}"
classpathref="compile-test-path" />
</target>
<!--指定架包路径,源文件编译后的路径和测试类编译后的路径-->
<path id="run-test-path">
<path refid="compile-test-path" />
<pathelement location="${bulid.test.classes}" />
</path>
<!---运行单元测试1-->
<property name="run-test.class" value="**/*Test.class"></property>
<target name="run-test" depends="compile-test">
<!--
printsummary 测试完整信息
haltonfailure 测试异常不继续执行
classpath 运行单元测试的路径
formatter 测试完整信息
usefile 是否使用文件
test 测试哪个类
batchtest 批量测试
-->
<junit printsummary="true" haltonfailure="true">
<!--运行单元测试需要指定架包路径,源文件编译后的路径和测试类编译后的路径-->
<classpath refid="run-test-path"/>
<formatter type="brief" usefile="false"/>
<!-- 测试一个类
<test name="${run-test.class}"/>
-->
<batchtest>
<fileset dir="${bulid.test.classes}" includes="${run-test.class}"/>
</batchtest>
</junit>
</target>
<!---运行单元测试2-->
<target name="run-test-2" depends="compile-test">
<!--
type="xml" 测试报告输出到xml中
todir 测试报告写在哪个文件夹中
-->
<junit printsummary="true" haltonfailure="true">
<classpath refid="run-test-path"/>
<formatter type="xml"/>
<batchtest todir="${bulid.test.report}">
<fileset dir="${bulid.test.classes}" includes="${run-test.class}"/>
</batchtest>
</junit>
<!--
junitreport 设置junit任务报告
todir 哪个文件夹中找任务报告
dir 对哪个任务报告打包
includes 那些文件打包
report 格式化
-->
<junitreport todir="${bulid.test.report}">
<fileset dir="${bulid.test.report}" includes="TEST-*.xml"/>
<report format="frames" todir="${bulid.test.report}/html"/>
</junitreport>
</target>
<target name="end" depends="run-test-2">
<echo>执行完毕</echo>
</target>
</project>