<pre name="code" class="java">package com.widetrust;
public class CountService {
private int summary;
private int count;
public boolean stat(int visitor){
count++;
summary += visitor;
if(summary>1000 && count>2){
return true;
}else{
return false;
}
}
}
在test文件夾寫個測試該類的類:
<pre name="code" class="java">package com.widetrust.test;
import com.widetrust.CountService;
import junit.framework.TestCase;
public class TestCountService extends TestCase {
CountService cs;
protected void setUp() throws Exception {
cs = new CountService();
}
protected void tearDown() throws Exception {
}
public void testStat(){
assertEquals(true, cs.stat(4000));
}
public void testStat2(){
cs.stat(2000);
cs.stat(2000);
assertEquals(true, cs.stat(3000));
}
}
<pre name="code" class="html"><?xml version="1.0"?>
<project name="ant and junit" default="test auot junit and report" basedir=".">
<!-- 定義工程依賴的jar包存放的位置 -->
<property name="lib.dir" value="lib"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<property name="output folder" value="classes"/>
<property name="src folder" value="src"/>
<property name="test folder" value="test"/>
<property name="report folder" value="report"/>
<target name="clean">
<delete dir="report"/>
<echo>清除測試報告文件 成功!</echo>
</target>
<target name="compile init">
<mkdir dir="${output folder}"/>
<echo>創建編譯文件夾 成功!</echo>
</target>
<target name="report init" depends="clean">
<mkdir dir="${report folder}"/>
<echo>創建測試報告文件夾 成功!</echo>
</target>
<target name="compile" depends="compile init">
<javac srcdir="${src folder}" destdir="${output folder}" classpathref="classpath"/>
<echo>項目源文件編譯 成功!</echo>
</target>
<target name="test compile" depends="report init">
<javac srcdir="${test folder}" destdir="${output folder}" classpathref="classpath"/>
<echo>項目測試文件編譯 成功!</echo>
</target>
<target name="all compile" depends="compile, test compile">
</target>
<target name="test auot junit and report" depends="all compile">
<junit printsummary="on" fork="true" showoutput="true">
<classpath>
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<pathelement path="${output folder}"/>
</classpath>
<formatter type="xml"/>
<batchtest todir="${report folder}">
<fileset dir="${output folder}">
<include name="**/Test*.*"/>
</fileset>
</batchtest>
</junit>
<junitreport todir="${report folder}">
<fileset dir="${report folder}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${report folder}"/>
</junitreport>
</target>
</project>
我們在eclipse中利用windows -> show View -> Ant 打開ant工作窗口,點擊”Add Buildfiles” 將項目的根目錄下的build.xml添加進去,然後在ant工作窗口點運行圖標,可以看到控制檯上的輸出:(當然,用命令行方式運行也可以)
<span style="background-color: rgb(255, 255, 255);">現在我們已經可以做到測試代碼和開發代碼分開,並利用ant的自動編譯功能爲我們執行測試計劃和生成測試報告。雖然在編寫測試案例的時候確實比較麻煩,我們需要詳細的模擬實際應用中的環境,並尋找可能出現錯誤的邊界值幫助我們在編寫實際代碼時儘量減少隱患的發生,往往編寫測試案例的時間比我們編寫實際代碼的時間還長,但這樣做是非常值得的,一個軟件的開發週期最長的60%都是花在尋找錯誤和完善的過程中。</span><span style="font-family:Lucida Grande, Verdana, Arial, sans-serif;color:#333333;"><span style="font-size: 13.3333330154419px; line-height: 20px;">
</span></span>