開發(fā)人員項(xiàng)目開發(fā)準(zhǔn)備無限極pos2.0測試手冊(cè)_第1頁
開發(fā)人員項(xiàng)目開發(fā)準(zhǔn)備無限極pos2.0測試手冊(cè)_第2頁
開發(fā)人員項(xiàng)目開發(fā)準(zhǔn)備無限極pos2.0測試手冊(cè)_第3頁
開發(fā)人員項(xiàng)目開發(fā)準(zhǔn)備無限極pos2.0測試手冊(cè)_第4頁
開發(fā)人員項(xiàng)目開發(fā)準(zhǔn)備無限極pos2.0測試手冊(cè)_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、2.0測試手冊(cè)無限極Author: Creation Date: Last Updated:Ref:Stanley Liu 2016年1月22日2016年1月22日Ver:1.0文檔控制更改日期章節(jié)作者版本更改參考內(nèi)容目錄文檔控制21.測試準(zhǔn)備4測試覆蓋率插件4Eclipse下的JUnit單元測試6單元測試6測試方法10代碼覆蓋率檢查13示例代碼141.1.2.2.1.2.2.2.3.2.4.1.測試準(zhǔn)備1.1.測試覆蓋率插件1.1.1.在Eclipse中添加覆蓋率插件ElcEmma1.Eclipse菜單選擇Help - Install New Software2.添加Eclemma的Site

2、3.選擇下一步按提示完成安裝,Eclipse重啟后可以在找到按鈕:2.Eclipse下的JUnit單元測試本章節(jié)主要描述Eclipse中如何進(jìn)行JUnit單元測試2.1.單元測試2.1.1.Eclipse中創(chuàng)建測試類4.在Project Explorer中選擇需要?jiǎng)?chuàng)建被測試類,右鍵選擇”New-Other”,然后選中”JUnit Test Case”。5.按照命名規(guī)范,更改”Source folder”以及”Name”。其它內(nèi)容不用更改,直接點(diǎn)擊確認(rèn)。6.根據(jù)規(guī)范選擇需要測試的方法7.8.Finish,生成測試類。將執(zhí)行類替換為Spring提供的ClassRunner,以及指定spring配置

3、文件路徑9.到這里已經(jīng)完成測試類的創(chuàng)建,可以往各個(gè)自動(dòng)生成的測試方法中寫相關(guān)的測試邏輯了。當(dāng)配置好junit后,mvn build也會(huì)自動(dòng)檢索測試類進(jìn)行單元測試。如果不需要在10.build的時(shí)候進(jìn)試,可以在mvn build選項(xiàng)中勾選忽略測試:2.1.2.運(yùn)行單元測試1.在Project Explorer中選中需要運(yùn)行的測試類,右鍵選擇”Run As - JUnit Test”2.在Eclipse的JUnit View查看2.1.3.mvn test可以直接在2.0項(xiàng)目下執(zhí)行 mvn test 命令,自動(dòng)檢索所有的測試類,逐個(gè)運(yùn)行單元測試。2.2.測試方法2.2.1.測試方法Test表示該方

4、法用于測試,最后會(huì)生成成功或失敗的Ignore忽略該方法。測試方法可以先寫,但是如果實(shí)現(xiàn)方法還沒有寫完的時(shí)候,可以加上這個(gè) ,那么執(zhí) 試的時(shí)候就不會(huì)運(yùn)行該方法。Before每執(zhí)行一個(gè)Test前,都固定執(zhí)行這個(gè)方法。通常用來重置或者預(yù)置某些對(duì)象或參數(shù)。After每執(zhí)行一個(gè)Test后,都固定執(zhí)行這個(gè)方法。通常用來重置或者預(yù)置某些對(duì)象或參數(shù)。BeforeClass在類測試開始前執(zhí)行一次AfterClass在類測試結(jié)束后執(zhí)行一次Tranional將方法設(shè)置為事務(wù)BeforeTranion/ AfterTranion標(biāo)注 Before 或 After 注解的方法和測試方法運(yùn)行在同一個(gè)事務(wù)中,但有時(shí)希望在

5、測試方法的事務(wù)開始之前或完成之后執(zhí)行某些方法以便獲取數(shù)據(jù)庫現(xiàn)場的一些情況。這時(shí),可以使用 Spring TestContext 的 BeforeTranion 和 AfterTranion 注解來達(dá)到目錄Rollback(true)事務(wù)測試完成之后進(jìn)行回滾Autowired按類型裝配依賴對(duì)象,默認(rèn)情況下,要求依賴對(duì)象必須存在,若允許null 值,可以設(shè)置它的required 屬性為 false。如果想使用按名稱裝配,可以結(jié)合Qualifier 注解一起使用。如:Autowired Qualifier(xmlBean1)private XMLBean xmlBean;Resource和Autow

6、ired 一樣,也可以標(biāo)注在字段或?qū)傩缘?setter 方法上,但它默認(rèn)是按名稱裝配。名稱可以通過Resource 的name 屬性指定,如果沒有指定name 屬性,當(dāng)注解標(biāo)注在字段上,即默認(rèn)取字段的名稱作為bean 名稱尋找對(duì)象,當(dāng)注解標(biāo)注在屬性的 setter 方法上,即默認(rèn)取屬性名作為 bean 名稱尋找依賴對(duì)象。當(dāng)沒有使用name 屬性時(shí),如果按照字段名找不到 bean,就會(huì)轉(zhuǎn)而使用按類型裝配的方式進(jìn)行查找;但當(dāng)使用了 name 屬性,只能按照指定的 name 查找bean,當(dāng)找不到相應(yīng)的bean 時(shí),就會(huì)拋異常。Resource(name=xmlBeanx)private XMLBe

7、an xmlBean;/用于字段上2.2.2.測試拋出異常提供一段的例子參考:2.2.3.判斷方法 - assertt 斷言測試方法必須使用.junit.Assert包來判斷守log規(guī)范使用Loger。除了標(biāo)準(zhǔn)的斷言,JUnit4中基本上可以只使用assert maven pom中加入hamcrest:一、字符匹配符1、equalTo:,中間測試過程的其它可以遵t來進(jìn)行常用的斷言,需要在assertt(testedValue, equalTo(expectedValue);斷言被測的testedValue等于expectedValue,equalTo可以斷言數(shù)值之間,字符串之間和對(duì)象之間是否相

8、等,相當(dāng)于Object的equals方法2、equalToIgnoringCase:assertt(testedString, equalToIgnoringCase(expectedString);斷言被測的字符串testedString在忽略大小寫的情況下等于expectedString3、equalToIgnoringWhiteSpace:assertt(testedString, equalToIgnoringWhiteSpace(expectedString);斷言被測的字符串testedString在忽略頭尾的任意個(gè)空格的情況下等于expectedString4、containsS

9、tring:assertt(testedString, containsString(subString) );斷言被測的字符串testedString包含子字符串subString5、endsWith:assertt(testedString, endsWith(suffix);斷言被測的字符串testedString以子字符串suffix結(jié)尾6、startsWith:assertt(testedString, startsWith(prefix);斷言被測的字符串testedString以子字符串prefix開始二、一般匹配符private NoWorkFoundStepExecution

10、Listener tested = new NoWorkFoundStepExecutionListener(); Testpublic void testAfterStep() StepExecution stepExecution = new StepExecution(NoProsingStep, new JobExecution(new JobInstance(1L, new JobParameters(),NoProsingJob);stepExecution setReadCount(0); try tested.afterStep(stepExecution); fail();

11、catch (NoWorkFoundException e) assertEquals(Step has not prosed any items, e.getMessage();1、nullValue():assertt(object,nullValue();斷言被測object的值為null*/2、notNullValue():assertt(object,notNullValue();斷言被測object的值不為null*/3、is:assertt(testedString, is(equalTo(expectedValue);斷言被測的object等于后面給出匹配表達(dá)式1)is匹配符簡

12、寫應(yīng)用之一:assertt(testedValue, is(expectedValue);is(equalTo(x)的簡寫,斷言testedValue等于expectedValue2)is匹配符簡寫應(yīng)用之二:assertt(testedObject, is(Cheddar.class);is(instanceOf(SomeClass.class)的簡寫,斷言testedObject為Cheddar的實(shí)例4、not:assertt(testedString, not(expectedString);與is匹配符正好相反,斷言被測的object不等于后面給出的object5、allOf:asser

13、tt(testedNumber, allOf( greatern(8), lessn(16) ) );斷言符合所有條件,相當(dāng)于“與”(&)6、anyOf:assertt(testedNumber, anyOf( greatern(16), lessn(8) ) );斷言符合條件之一,相當(dāng)于“或”(|)三、數(shù)值相關(guān)匹配符1、closeTo:assertt(testedDouble, closeTo( 20.0, 0.5 );斷言被測的浮點(diǎn)型數(shù)testedDouble在20.0-0.5 20.0+0.5范圍之內(nèi)2、greaterassertn:t(testedNumber, greatern(16

14、.0);斷言被測的數(shù)值testedNumber大于16.03、lessn:assertt(testedNumber, lessn (16.0);斷言被測的數(shù)值testedNumber小于16.04、greaterassertnOrEqualTo:t(testedNumber, greaternOrEqualTo (16.0);斷言被測的數(shù)值testedNumber大于等于16.05、lessnOrEqualTo:assertt(testedNumber, lessnOrEqualTo (16.0);斷言被測的testedNumber小于等于16.0四、集合相關(guān)匹配符1、hasEntry:ass

15、ertt(mapObject, hasEntry(key, value ) );斷言被測的Map對(duì)象mapObject含有一個(gè)鍵值為key對(duì)應(yīng)元素值為value的Entry項(xiàng)2、hasItem:assertt(iterableObject, hasItem (element);表明被測的迭代對(duì)象iterableObject含有元素element項(xiàng)則測試通過3、hasKey:assertt(mapObject, hasKey (key);斷言被測的Map對(duì)象mapObject含有鍵值“key”4、hasValue:assertt(mapObject, hasValue(value);斷言被測的M

16、ap對(duì)象mapObject含有元素值value2.2.4.參數(shù)化當(dāng)需要多組數(shù)據(jù)來校驗(yàn)程序時(shí),可以使用 RunWith 進(jìn)行參數(shù)化。具體方式可以參照2.4的實(shí)例代碼。2.3.代碼覆蓋率檢查2.3.1.Coverage as1.每寫完一個(gè)JUnit測試都要檢查代碼覆蓋率,100%。要求被測試類的代碼覆蓋率達(dá)到2.選擇測試類,右鍵彈出菜單中,Coverage As - JUnit Test3.JUnit測試會(huì)開始運(yùn)行,運(yùn)行完成后在Coverage View里面可以看到覆蓋率結(jié)果:4.雙擊打開后,可以看到哪些代碼沒有覆蓋到。2.4.示例代碼3.package.hand.hap.service; 4.i

17、mport s ic.junit.Assert.*;import s ic.hamcrest.CoreMatchers.*;import java.util.Date; 8.import.junit.Before;import.junit.ClassRule;import.junit.Rule;import.junit.Test;import.junit runner.RunWith;import.slf4j.Logger;import.slf4j.LoggerFactory;import.springframework.beans.factory.annoion.Autowired;impo

18、rt.springframework.test.anno ion.Rollback;import.springframework.test.context.ContextConfiguration;import.springframework.test.context.TestContextManager;import.springframework.test.context.junit4.rules.SpringClassRule;import.springframework.test.context.junit4.rules.SpringMethodRule;import.springfr

19、amework.tranion.anno ion.Tranional;23.import com.code.zohhak.api.TestWith;import com.code.zohhak.api.runners.ZohhakRunner;import com hand hap.dto.sys.Resource;import com hand hxception.BaseException;import com hand hap.service.IResourervice;import com hand hap.test.TestBase; 30.RunWith(ZohhakRunner.

20、class)TranionalContextConfiguration(locations=classpath:spring/applicationContext-test.xml)public class IResourerviceTest ClassRulepublic s ic final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();Rulepublic final SpringMethodRule springMethodRule = new SpringMethodRule(); 39.private TestC

21、ontextManager testContextManager;privaogger log = LoggerFactory.getLogger(getClass(); 42.Before/一些公用的“初始化”代碼public void before()this.testContextManager = new TestContextManager(getClass();try this.testContextManagrepareTestInstance(this); catch (Exception e) / TODO Auto-generated catch blocke.prStac

22、kTrace();52.53.Autowiredprivate IResourervice resourervice; 56.TestWith(/login.html, Selected,sys/sys_profile html, Unselected 60.)public void testSelectResourceByUrl(String inputUrl, String expectedValue) Resource loginResou = resourervielectResourceByUrl(inputUrl);if(expectedValue.equals(Selected)

23、64.65.assertNull(login not Selected, loginResou);66.67.else68.69.assertNotNull(haected, loginResou);70.71.72.Testpublic void testSelectResourceById() Long profileId = new Long(6);Resource profile = resourervielectResourceById(profileId);assertNotNull(sys/sys profile html haected, profile);78.79.TestWith(stanleyjunittest, stanleyjunittest, PAGE, junitde

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論