用ant进行自动化测试,生成测试报告,在软件开发中是件很常见的事情,首先看测试代码:
/**
*
*/
package com.wolf.app.test;
/**
* @author gang.huang
*
*/
public class Calculation {
public int add(int a, int b) {
return a + b;
}
public int subtraction(int a, int b) {
return a - b;
}
public int multiplication(int a, int b) {
return a * b;
}
public double division(double a, double b) {
if (b == 0) {
return 0;
}
return a / b;
}
}
单元测试代码:
package com.wolf.app.test;
import org.junit.Assert;
import org.junit.Test;
/**
* @author gang.huang
*
*/
public class CalculationTest {
private Calculation calculation;
@Test
public void testAdd(){
this.calculation=new Calculation();
Assert.assertEquals(5, this.calculation.add(1, 4));
}
}
用ant编写的测试脚本为:
<?xml version="1.0" encoding="UTF-8"?>
<!-- ======================================================================
2009-12-31 上午11:47:24
单元自动化测试 (junit)
gang.huang
====================================================================== -->
<project name="AutoTest" default="report" basedir=".">
<description>
单元自动化测试
</description>
<!-- =================================
target: default
================================= -->
<property name="src" value="." />
<property name="build" value="build" />
<property name="classes" value="${build}/classes" />
<property name="report" value="report" />
<path id="jar-all">
<pathelement path="${classes}" />
<fileset dir="${src}/lib">
<include name="*.jar" />
</fileset>
</path>
<target name="init">
<delete dir="${build}" />
<delete dir="${classes}" />
<delete dir="${report}" />
<mkdir dir="${build}" />
<mkdir dir="${classes}" />
<mkdir dir="${report}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${classes}" debug="on">
<classpath refid="jar-all" />
</javac>
</target>
<target name="test" depends="compile">
<junit printsummary="yes" haltonfailure="no">
<formatter type="xml" />
<test name="com.wolf.app.test.CalculationTest" />
<classpath refid="jar-all">
</classpath>
<batchtest fork="yes" todir="${report}">
<fileset dir="${src}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
</target>
<target name="report" depends="test">
<junitreport todir="${report}">
<fileset dir="${report}">
<include name="test-*.xml" />
</fileset>
<report format="frames" todir="${report}/html" />
</junitreport>
</target>
</project>
简简单单测试报告就这样出来,下一步如何用maven来管理的项目以及测试的项目,完成过后与artifactory整合。这是一门有意思的技术。