新聞中心
單元測(cè)試工具Junit是一個(gè)開(kāi)源項(xiàng)目,昨天學(xué)習(xí)了一下這個(gè)東西,總結(jié)下心得。

成都創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供吉隆企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、HTML5建站、小程序制作等業(yè)務(wù)。10年已為吉隆眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)的建站公司優(yōu)惠進(jìn)行中。
1.創(chuàng)建相應(yīng)的test類(lèi)
package:測(cè)試類(lèi)存放位置。
Name:測(cè)試類(lèi)名字。
setUp,tearDown:測(cè)試類(lèi)創(chuàng)建測(cè)試環(huán)境以及銷(xiāo)毀測(cè)試環(huán)境,這兩個(gè)方法只執(zhí)行一次。
Class Under test:需要被測(cè)試的類(lèi)路徑及名稱(chēng)。
點(diǎn)擊下一步就會(huì)讓你選擇需要給哪些方法進(jìn)行測(cè)試。
測(cè)試類(lèi)創(chuàng)建完成后在類(lèi)中會(huì)出現(xiàn)你選擇的方法的測(cè)試方法:
- package test.com.boco.bomc.alarmrelevance.show.dao;
- import junit.framework.TestCase;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.BeforeClass;
- import org.junit.Test;
- public class ShowStrategyDaoTest extends TestCase{
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- System.out.println("OK1");
- }
- @Before
- public void setUp() throws Exception {
- }
- @After
- public void tearDown() throws Exception {
- }
- @Test
- public final void testGetDataByApplyNameOrHostIp() {
- fail("Not yet implemented"); // TODO
- }
- @Test
- public final void testGetDataByObject() {
- fail("Not yet implemented"); // TODO
- }
- @Test(timeout=1)
- public final void testGetApplyUser() {
- fail("Not yet implemented"); // TODO
- }
- @Test
- public final void testGetVoiceUser() {
- fail("Not yet implemented"); // TODO
- }
- @Test
- public final void testSearchInAera() {
- fail("Not yet implemented"); // TODO
- }
- @Test
- public final void testGetDataByPolicyId() {
- fail("Not yet implemented"); // TODO
- }
- }
其中的@before,@test,@after表示在執(zhí)行測(cè)試方法前執(zhí)行,需執(zhí)行的測(cè)試方法,在測(cè)試方法執(zhí)行后執(zhí)行。
可以給@test添加timeout,exception參數(shù)。
在測(cè)試方法中可以用assertEquals(arg0,arg1);
可以用TestSuite把多個(gè)測(cè)試類(lèi)集中到一起,統(tǒng)一執(zhí)行測(cè)試,例如:
- package test.com.boco.bomc.alarmrelevance.show.dao;
- import junit.framework.Test;
- import junit.framework.TestSuite;
- public class TestAll {
- public static Test suite(){
- TestSuite suite = new TestSuite("Running all the tests");
- suite.addTestSuite(ShowStrategyDaoTest.class);
- suite.addTestSuite(com.boco.bomc.alarmrelevance.show.dao.ShowStrategyDaoTest.class);
- return suite;
- }
- }
另外還可以把多個(gè)TestSuite組合到一個(gè)Test類(lèi)里面,例如:
- package test.com.boco.bomc.alarmrelevance.show.dao;
- import junit.framework.Test;
- import junit.framework.TestCase;
- import junit.framework.TestSuite;
- public class TestAll1 extends TestCase {
- public static Test suite(){
- TestSuite suite1 = new TestSuite("TestAll1");
- suite1.addTest(TestAll.suite());
- suite1.addTest(TestAll2.suite());
- return suite1;
- }
- }
這就更方便與集中測(cè)試,一個(gè)方法測(cè)試完了,可以對(duì)個(gè)方法,多個(gè)類(lèi)一起測(cè)試。
注意:在寫(xiě)代碼的時(shí)候TestSuite,TestCase,Test的包不要到錯(cuò)了。
測(cè)試效果如下:
原文鏈接:http://www.cnblogs.com/God-froest/archive/2011/11/18/JunitTest.html
編輯推薦:
- Struts2單元測(cè)試:使用Junit測(cè)試Action
- Jython開(kāi)發(fā)的JUnit測(cè)試包
- JUnit常用斷言方法
- 在Junit中測(cè)試私有函數(shù)的方法
- JUnit與JTiger的比較
網(wǎng)站題目:?jiǎn)卧獪y(cè)試?yán)鱆Unit的實(shí)踐與總結(jié)
瀏覽地址:http://fisionsoft.com.cn/article/dhedihp.html


咨詢(xún)
建站咨詢(xún)
