1.2 junit單元測試
大部分整合工具都整合了junit單元測試外掛程式,並有嚮導協助寫單元測試。Junit發行包的文檔很詳細地介紹了Junit的設計概念和所使用的設計模式。在這裡我簡單地說明如何寫測試案例、在ant設定檔中調用測試案例和產生測試報告的方法。
寫測試案例
下面是在eclipse junit嚮導對MyCode類編寫的測試案例TestMyCode檔案基礎上寫的代碼:
import junit.framework.TestCase;
/*
* Created on 2003-4-30
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
/**
* @author gongys
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class TestMyCode extends TestCase {
MyCode myFixture=null;
/**
* Constructor for TestTest.
* @param arg0
*/
public TestTest(String arg0) {
super(arg0);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
myFixture = new MyCode();
System.out.println("setup");
}
/*
* @see TestCase#tearDown()
*/