play手把手教你創(chuàng)建一個(gè)博客項(xiàng)目10完整的應(yīng)用程序測(cè)試_第1頁(yè)
play手把手教你創(chuàng)建一個(gè)博客項(xiàng)目10完整的應(yīng)用程序測(cè)試_第2頁(yè)
play手把手教你創(chuàng)建一個(gè)博客項(xiàng)目10完整的應(yīng)用程序測(cè)試_第3頁(yè)
play手把手教你創(chuàng)建一個(gè)博客項(xiàng)目10完整的應(yīng)用程序測(cè)試_第4頁(yè)
play手把手教你創(chuàng)建一個(gè)博客項(xiàng)目10完整的應(yīng)用程序測(cè)試_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、play手把手教你創(chuàng)建一個(gè)博客項(xiàng)目W完整的應(yīng)用程序測(cè)試現(xiàn)在,我們差不多終止了博客引擎的編碼工作,但對(duì)項(xiàng)日來(lái)講還沒(méi)有完成,為了 讓我們的代碼能夠完全正確的工作,我們還需要對(duì)項(xiàng)U進(jìn)行測(cè)試。因此,我們之前差不多為yabe的模型層功能書(shū)寫(xiě)的單元測(cè)試,同時(shí)確信博客引 擎的核心功能差不多進(jìn)行了完好的測(cè)試,然而關(guān)于一個(gè)web應(yīng)用程序來(lái)講模型層 只是其中的一部分,我們還需要確定web接口能否按預(yù)期的U標(biāo)一樣正常工作。 也確實(shí)是講還需要測(cè)試Wbe博客引擎的操縱器層,其至需要對(duì)UI自身進(jìn)行測(cè)試, 比如我們的JavaScript代碼。測(cè)試操縱器部分Play提供了一種功能,確實(shí)是使用JUnit來(lái)直截了當(dāng)測(cè)試應(yīng)用程序的

2、操縱器。 我們把這些測(cè)試叫做功能性測(cè)試,這是因?yàn)槲覀兇蛩銣y(cè)試web應(yīng)用程序的完 整功能。差不多上,一個(gè)功能性測(cè)試將直截了當(dāng)調(diào)用Play的Act ioninvoker T和一個(gè)HTTP 要求相似。因此我們需要給出一個(gè)HTTP方法、一個(gè)lRI和多個(gè)HTTP參數(shù)。Play 之后會(huì)路山這些要求,調(diào)用相應(yīng)的action,同時(shí)回發(fā)到填寫(xiě)的響應(yīng)(filled response)。之后,你就能夠?qū)χM(jìn)行分析,以檢查響應(yīng)內(nèi)容是否你所預(yù)期的。import import import import importpublicorg. junit. *; play. test. *; play. mvc. *; pla

3、y. mvc. Http. *; models. *;class ApplicationTest extends FunctionalTest Testpublic void testThatIndexPageWorksO Response response = GET (/);assertIsOk(response); assertContentType(text/html, response); assertCharset(utf-8, response);Play! - Tests runner現(xiàn)在看,它依舊一個(gè)標(biāo)準(zhǔn)的JUnit測(cè)試。請(qǐng)注意,在那個(gè)地點(diǎn)我們使用Play的 Function

4、alTest超類,要緊是為了得到所有可用的工具。那個(gè)測(cè)試只對(duì)應(yīng)用程 序的主頁(yè)進(jìn)行了測(cè)試(/URL渲染一個(gè)HTML響應(yīng),以2000K作為狀態(tài)代碼)。接下來(lái),我們將檢查治理區(qū)域(administration area)的安全工作能否正常工 作。在ApplicationTest. java里添加下面那個(gè)新測(cè)試:Testpublic void testAdminSecurity() Response response = GET (/admin); assertStatus(302, response);assertHeaderEquals(Location, /login, response);現(xiàn)在

5、,用play test命令把yabe應(yīng)用程序運(yùn)行于測(cè)試模式,打開(kāi),選擇 ApplicationTest. java 測(cè)試并運(yùn)行。是綠色的嗎? | I I + jBhup:/bcalhost:900Q/tesi$Tests runnerSelect the tests to run, then click Start and prayStart!There is 1 unit test,BasicTestjava1 functional test.OkApphcationTest.javaI testTbatlrKtex PageWorks |tMAdminSecurity |Okand 1 s

6、elenium test,Application.te$t.html因此!通過(guò)這種方式,我們能夠?qū)λ械膽?yīng)用程序功能性進(jìn)行測(cè)試,但把這用于 測(cè)試一個(gè)基于html的web應(yīng)用程序時(shí),這并不是最好的方式。關(guān)于我們的博客 引擎項(xiàng)U來(lái)講,直截了當(dāng)在真實(shí)的掃瞄器進(jìn)行測(cè)試可能會(huì)更好。這確實(shí)是Phy 的Selenium tests測(cè)試要干的事。這種基于“功能性測(cè)試”的JUnit仍舊專門有用,專門是用于測(cè)試一個(gè)返回非 html 響應(yīng)(比如 JS0N 或 XML)的 Web services 時(shí)。書(shū)寫(xiě)Selenium測(cè)試代碼Selenium是一個(gè)專用于測(cè)試web應(yīng)用程序的測(cè)試工具那個(gè)工具最酷的確實(shí)是 Selen

7、ium承諾我們?cè)谝粋€(gè)掃瞄器里直截了當(dāng)運(yùn)行測(cè)試套件,山于它使用的是真 實(shí)的掃瞄器,因此,我們能夠確定測(cè)試通過(guò)后,項(xiàng)U就能夠在生產(chǎn)環(huán)境下完美的 運(yùn)行 一個(gè)Selenium測(cè)試套件確實(shí)是一個(gè)專門的html文件。HTML syntax required by Selenium必須使用的HTML語(yǔ)句比較單調(diào)(使用HTML表格元素進(jìn)行數(shù)據(jù)格式化顯 示),好消息是play將使用play模板引擎和一系列支持簡(jiǎn)單Selenium表示語(yǔ)法 的標(biāo)簽來(lái)關(guān)心你生成這些元素)使用模板最有味的特點(diǎn)是你全然不需要static scenarios* ,同時(shí)能夠使用play模板強(qiáng)大的功能(如循環(huán)、條件塊) 來(lái)書(shū)寫(xiě)更復(fù)朵的測(cè)試。然

8、而,你仍舊能夠連續(xù)在模板里使用原始的HTML Selenium語(yǔ)法,假如需要的話, 還能夠不記得特定的Selenium標(biāo)簽。假如你使用多個(gè)用于生成test scenarios(比如Selenium IDE)的Selenium IL具中的一個(gè),這將變得專門有味。新創(chuàng)建的Py應(yīng)用程序的默認(rèn)測(cè)試套件差不多包含了一個(gè)Selenium測(cè)試,打開(kāi) yabe/test/Application. test, html 文件s* You can use plain Selenium commands using the selenium tag * # selenium/ Open the home page,

9、 and check that no error occurred open(,/,)waitForPageToLoad(1000)assertNotTitle(*Application error*)#/selenium運(yùn)行那個(gè)測(cè)試應(yīng)該可不能有任何咨詢題。它只打開(kāi)了主頁(yè),并檢測(cè)頁(yè)面內(nèi)容是否 包含 了 Application error * 文本。然而,和任何復(fù)朵的測(cè)試一樣,在導(dǎo)航到應(yīng)用程序并進(jìn)行測(cè)試之前,你需要設(shè)置 一系列眾所周知的數(shù)據(jù),我們因此需要巫用fixture概念,同時(shí)在開(kāi)始測(cè)試之前 使用yabe/test/data. yml文件,# fixture /標(biāo)簽導(dǎo)入這些測(cè)試數(shù)據(jù):#fix

10、ture delete:* all*, load / data, yml* / #selenium/ Open the home page, and check that no error occurred open(,/,)WaitForPageToLoad(1000)assertNotTitle(*Application error*)#/selenium列外一個(gè)重要的悄況確實(shí)是我們要在測(cè)試啟動(dòng)時(shí)檢查我們是否有一個(gè)最新的用 戶sessiono那個(gè)session將儲(chǔ)備在掃肺器的臨時(shí)cookie里,你應(yīng)該在兩個(gè)連 續(xù)的測(cè)試運(yùn)行操作期間保持同一個(gè)session,因此,讓我們用一個(gè)特定的命令開(kāi) 始

11、測(cè)試:#fixture delete:* all*, load / data, yml* /#selenium clearSessionO/ Open the home page, and check that no error occurred open(,r )waitForPageToLoad(1000) assertNotTitle(*Application error*)#/selenium運(yùn)行那個(gè)測(cè)試,并確定沒(méi)有錯(cuò)誤發(fā)生,結(jié)果應(yīng)該是綠色的。接下來(lái)我們將書(shū)寫(xiě)專門專門的測(cè)試,測(cè)試打開(kāi)主頁(yè)后檢查默認(rèn)的博文是否顯示出 來(lái):#fixture delete:* all*, load / dat

12、a, yml* /#selenium * Check home page* clearSessionO/ Open the home page open ( r )/ Check that the front post is present assertTextPresent(* About the model layer*) assertTextPresent (* by Bob, 14 Jun 09) assertTextPresent(2 comments , latest by Guest*) assertTextPresent(*It is the domain-specific r

13、epresentation*)/ Check older postsassertTextPresent(,The MVC application*) assertTextPresent( Just a test of YABE*) #/selenium在那個(gè)地點(diǎn),我們使用了標(biāo)準(zhǔn)的Selenium語(yǔ)法,它叫Selenese。運(yùn)行它(你能夠運(yùn)行于一個(gè)不同的掃瞄器窗口里)Ctiedf homeiclearS&ssior Open the home page open/ Check that the front post: is present assertText Present assertTex

14、t PresentassertTextPrese ntassertText PresentAbout the rwcxJel layer by Bob, 14 Jun 09 2 comments. latest by GuestIt is the domain-sped fie represe ntation Check older posts assertText Present assertText PresentThe MVC application Just a test of YABEyabe.Yet another blogWe will write about nothingJu

15、st a test of YABEThe MVC applicationbv J 金 ff-加我們現(xiàn)在就能夠測(cè)試評(píng)論窗體了,只需要添加一個(gè)#selenium /到模板里即 可:#selenium * Test comments*/ Click on * The MVC application post* clickAndWait(*link=The MVC application*) assertTextPresent(* The MVC application*) assertText Present (no comments*)content ,/ Post a new comment t

16、ype C content*, Hello*) clickAndWait(* css=inputtype=submit*)/ Should get an errorassertText Present (no comments*) assertTextPresent(,Author is required*) type (,author*,,Me*)clickAndWait(* css=inputtype=submit*)/ CheckassertTextPresent(,Thanks for posting Me*)assertTextPresent ( 1 comment*)assertT

17、extPresent ( Hello*)# /selenium再次才能,哦,失敗了!那個(gè)地點(diǎn)有一個(gè)嚴(yán)峻的咨詢題顯現(xiàn)。PZayy testApplication .test.htmlassertText PresentThe MVC applicationasserCTextPrese ntno comme nts/ Post a n&wtypecontentclickArdWaitcss=inpuCtype=submit Should get an errorassertText Presentno commentsassertText PresentAuthor is requiredtyp

18、eauthordickAndWaitcss=inputtype=submit/Ch&ckassertText PresentThanks for posting MeassertText Press nt1 commentassertText Prese ntHelloHeMefahno commentsPost a commentPlease type the codeYour name:我們事實(shí)上不能正確測(cè)試captcha驗(yàn)證碼機(jī)制,因此,我們必須搞一些欺詐手段。 在測(cè)試模式下,我們將驗(yàn)證任何代碼作為一個(gè)正確的驗(yàn)證碼。我們明白當(dāng)框架 a. We know that we * re in t

19、est mode when the framework id is test. So let s modify the postComment action in the yabe/app/controllers/Application. java skip this validation in test mode: if (IPlay. id. equals (test) validation, equals(code, Cache, get(randomID). message(Invalid code. Please type it again);Now just modify the

20、test case to type any code in the text field, as is: type (,author*,,Me*)type Ccode XXXXX) clickAndWait(* css=inputtype=submit*)And now run the test again, it should work.Measuring code coverageOf course we haven t written all required test cases for the application. But it s enough for this tutoria

21、l. Now in a real-world project, how can we know if we have written enough test cases? We need something calledcode coverage.The Cobertura module generates code coverage reports using the Cobertura tool. Install the module using the install command: play install cobertura-versionWe need to enable thi

22、s module only for test mode. So add this line to the application, conf file, and restart the application in test mode.# Import the cobertura module in test mode%test. module. cobertura=$play, path/modules/coberturaNow reopen the browser at the URL, select all tests and run them. All should be green.

23、Play! - Tests runner vl.O-beta-2【c O cV A! + 0hnp:/l&cdlhosi:9OOO/tesu#Tests runnerStartSelect the tests to run, then click Start and pray3 tests to run (Bookmark this link to 鈕ve this 8nfigiiration)There is 1 unit test,+ BasicTestjava1 fun ctional test,AppHcationTest.javaand 1 sele nium test.App li

24、cation.t令 sthtmTICfieefi homo pagecl ears ess ion Open ihe home page open Check that th& front post js presentassertTfext PresentassertTextPr&sentassertTextPresentassertTextPr&sentAbout the model layerby Bob, 14 Jun 092 comments , latest by GuestIt is the domain-specific representationThe MVR annlic

25、atinn Check older po&ts flssfirtT ftxtPrftfi ArtWhen all tests are passed, stop the application and cobertura will then generate the code coverage report. You can then open theyabe/test-result/code-coverage/index, html in your browser and check the report.Coverage ReportI V 1 j +file:/Volumes/Daia/D

26、eskcop/plav/sampls-and-iesi5Zyabe/ie5C-resulcZPackagesCoverage Report - controllers.ApplicationAil (default) controllQ mod 色 IsCl asses in this FileAoolicati onLine Coverage6 現(xiàn)controllersClassesAdmin 6%丿 Aslication CRUD f7%J Check (N/A) Comments Posts Secure (6%丿 Security Taas fO%J Us亡 rs fO%J123456

27、789 10 11 121314151617181920 21 22232425262728293031323334353637383940414243package controllers;import import import import imperfcimport imperfepuHicplay-*;play.rave*;play .datcL. validation . play . libs- * ; play.cache*;java.util.*; models.class Appli-calxori expends Cchtrdller f0Beforestatic void

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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)論