play手把手教你創(chuàng)建一個博客項目-0完整的應(yīng)用程序測試_第1頁
play手把手教你創(chuàng)建一個博客項目-0完整的應(yīng)用程序測試_第2頁
已閱讀5頁,還剩8頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、1 0 .完 整 的 應(yīng) 用 程 序 測 試現(xiàn)在,我們已經(jīng)結(jié)束了博客引擎的編碼工作, 但對項目來說還沒有完成, 為了讓 我們的代碼能夠完全正確的工作,我們還需要對項目進(jìn)行測試。當(dāng)然,我們之前已經(jīng)為 yabe 的模型層功能書寫的單元測試,并且確信博客引擎 的核心功能已經(jīng)進(jìn)行了完好的測試,但是對于一個 web 應(yīng)用程序來說模型層只是 其中的一部分,我們還需要確定 web 接口能否按預(yù)期的目標(biāo)一樣正常工作。 也就 是說還需要測試 yabe 博客引擎的控制器層, 甚至需要對 UI 自身進(jìn)行測試, 比如 我們的JavaScript 代碼。測試控制器部分Play 提供了一種功能,就是使用 JUnit 來直

2、接測試應(yīng)用程序的控制器。我們把 這些測試叫做功能性測試,這是因為我們打算測試 web 應(yīng)用程序的完整功基本上,一個功能性測試將直接調(diào)用Play 的 Actionlnvoker,和一個 HTTP 青求相似。因此我們需要給出一個 HTTP 方法、一個 URI 和多個 HTTP 參數(shù)。Play 之 后會路由這些青求,調(diào)用相應(yīng)的 action ,并且回發(fā)到填寫的響應(yīng)( filled response )。之后,你就可以對之進(jìn)行分析,以檢查響應(yīng)內(nèi)容是否你所預(yù)期的。接下來讓我們書寫第一個功能性測試代碼,打開yabe/test/ApplicationTest.java 單元測試 :import org.ju

3、nit.*;import play.test.*;import play.mvc.*;import play.mvc.Http.*;import models.*;public class ApplicationTest extends FunctionalTest Testpublic void testThatIndexPageWorks() Response response = GET(/);assertIsOk(response); assertContentType(text/html, response);assertCharset(utf-8, response);現(xiàn)在看,它還

4、是一個標(biāo)準(zhǔn)的 JUnit 測試。請注意,在這里我們使用 Play 的 FunctionalTest 超類,主要是為了得到所有可用的工具。 這個測試只對應(yīng)用程 序的主頁進(jìn)行了測試(/ URL 渲染一個 HTML向應(yīng),以200 OK 作為狀態(tài)代碼)接下來,我們將檢查管理區(qū)域( administration area )的安全工作能否正常工 作。在ApplicationTest.java 里添加下面這個新測試:Testpublic void testAdminSecurity() Response response = GET(/admin);assertStatus(302, response);

5、 assertHeaderEquals(Location, /login, response);現(xiàn)在,用 play test 命令把 yabe 應(yīng)用程序運行于測試模式,打開 , 選擇 ApplicationTest.java 測試并運行。是綠色的嗎?Play - Tests runner+ http:/localhost:9000/testsTests runnerSelect the testa to run, than click Start言nd prayn(Bookmark thi今link to save曲倒configuratiorOThere is 1 unit test,- B

6、asicTestjava1 functional test,ApplicattonTe st.JavatestThat I ndexPageWorksOktestAdm inSecurityOkand 1 selenium test,Application.testhtml當(dāng)然!通過這種方式,我們可以對所有的應(yīng)用程序功能性進(jìn)行測試,但把這用于測試一個基于 html 的 web 應(yīng)用程序時,這并不是最好的方式。對于我們的博客 引擎項目來說,直接在真實的瀏覽器進(jìn)行測試可能會更好。這就是 play 的Selenium tests 測試要干的事。這種基于“功能性測試”的 JUnit 仍舊很有用,特別是

7、用于測試一個返回非 html 響應(yīng)(比如 JSON 或XML 的 Web services 時。IA St書寫Selenium測試代碼是一個專用于測試 web 應(yīng)用程序的測試工具。這個工具最酷的就是 Selenium 允 許我們在一個瀏覽器里直接運行測試套件, 由于它使用的是真實的瀏覽器, 因此, 我們可以確定測試通過后,項目就可以在生產(chǎn)環(huán)境下完美的運行。一個 Selenium 測試套件就是一個特殊的 html 文件。 HTML syntax required by Selenium 必須使用的 HTM 語句比較單調(diào)(使用 HTM 表格元素進(jìn)行數(shù)據(jù)格式化顯 示) ,好消息是 play 將使用

8、play 模板引擎和一系列支持簡單 Selenium 表示語法 的標(biāo)簽來幫助你生成這些元素)。使用模板最有趣的特點是你根本不需要 static scenarios ,并且可以使用 play 模板強(qiáng)大的功能(如循環(huán)、條件塊) 來書寫更復(fù)雜的測試。然而,你仍舊可以繼續(xù)在模板里使用原始的 HTMLSelenium 語法,如果需要的話, 還可以忘記特定的 Selenium 標(biāo)簽。如果你使用多個用于生成 test scenarios( 比 如)的 Selenium 工具中的一個,這將變得非常有趣。新創(chuàng)建的 play 應(yīng)用程序的默認(rèn)測試套件已經(jīng)包含了一個 Selenium 測試,打開yabe/test/A

9、pplication.test.html文件:* You can use plain Selenium commands using the selenium tag *#selenium/ Open the home page, and check that no error occurredopen(/)waitForPageToLoad(1000)assertNotTitle(Application error)#/selenium運行這個測試應(yīng)該不會有任何問題。 它只打開了主頁, 并檢測頁面內(nèi)容是否包含 了 Applicationerror 文本。然而,和任何復(fù)雜的測試一樣, 在導(dǎo)航到

10、應(yīng)用程序并進(jìn)行測試之前, 你需要設(shè)置 一系列眾所周知的數(shù)據(jù), 我們當(dāng)然需要重用 fixture 概念,并且在開始測試之前 使用 yabe/test/data.yml 文件,#fixture / 標(biāo)簽導(dǎo)入這些測試數(shù)據(jù):#fixture delete:all, load:data.yml /#selenium/ Open the home page, and check that no error occurredopen(/)waitForPageToLoad(1000)assertNotTitle(Application error)#/selenium另外一個重要的事情就是我們要在測試啟動時

11、檢查我們是否有一個最新的用戶session 。這個 session 將存儲在瀏覽器的臨時 cookie 里,你應(yīng)該在兩個連續(xù)的測試運行操作期間保持同一個 session ,因此,讓我們用一個特定的命令開始測 試:#fixture delete:all, load:data.yml /#seleniumclearSession()/ Open the home page, and check that no error occurred open(/)waitForPageToLoad(1000)assertNotTitle(Application error) #/selenium運行這個測試

12、,并確定沒有錯誤發(fā)生,結(jié)果應(yīng)該是綠色的。接下來我們將書寫很特殊的測試,測試打開主頁后檢查默認(rèn)的博文是否顯示出 來:#fixture delete:all, load:data.yml /#selenium Check home page clearSession()/ Open the home pageopen(/)/ Check that the front post is present assertTextPresent(About the model layer)assertTextPresent(by Bob, 14 Jun 09) assertTextPresent(2 comm

13、ents , latest by Guest)assertTextPresent(It is the domain-specific representation)/ Check older postsassertTextPresent(The MVC application)assertTextPresent(Just a test of YABE)#/selenium在這里,我們使用了標(biāo)準(zhǔn)的 Selenium 語法,它叫。運行它(你可以運行于一個不同的瀏覽器窗口里)。Sefenium Functional Test Runner vl.O-bet+ * 9 lnp:/Focalhost:9

14、000/publk/teswunner/selemum/Te5tRunnerhtml ?baseUrl=http:/local hostCheck homejdearSesson/ Open the home pagepen/ Check that the front post is presentassertTextPrese ntAbout the model fayerassertTextPrese ntby Bob, 14 Jun 09assertTextPrese nt2 commentstlatest by Gu estassertTextPrese ntIt is the dom

15、ain -specificrepresentationff Check older postsassertTextPrese ntThe MVC applicationassertTextPrese ntJust a test of YABEyabe.Yet another blogWe will write about nothing Just g test of BEThe MVC applicationbv Jeff OS Jun 09_我們現(xiàn)在就可以測試評論窗體了,只需要添加一個#sele nium /到模板里即可:#sele nium Test comme nts/ Click on

16、 The MVC application post clickA ndWait(li nk=The MVC applicatio n) assertTextPresent(The MVC 即 plicatio n) assertTextPrese nt( no comme nts)Playf testApplies ticn-testhtml/ Post a new commenttype(content, Hello) clickAndWait(css=inputtype=submit)/ Should get an errorassertTextPresent(no comments) a

17、ssertTextPresent(Author is required) type(author, Me)clickAndWait(css=inputtype=submit)/ Check assertTextPresent(Thanks for posting Me) assertTextPresent(1 comment)assertTextPresent(Hello)#/selenium再次才能,哦,失敗了!這里有一個嚴(yán)重的問題出現(xiàn)Sefenium Functional Test Runner vlLO-bet小+ * http:/localhost:9000/public/test-r

18、unner/selenium/TestRunner.htmPbaseUrlliup:/localhostPlease type the codeYour name:MeYour messagB:我們事實上不能正確測試 captcha 驗證碼機(jī)制,因此,我們必須搞一些欺騙手段。 在測試模式下,我們將驗證任何代碼作為一個正確的驗證碼。我們知道當(dāng)框架a. Weknow that we re in test modewhenthe framework id is test. So let s modify the postComme nt action in theyabe/app/controlle

19、rs/Application .java file to skip this validation intest mode: if(!Play.id.equals(test) validation.equals(code, Cache.get(randomID).message(Invalid code. Please type it again);Pfay! restApplication, test htmlassertText PresentTTie MVC applicationa ssertText Presentno comments/ Post a new commenttype

20、contentHedickAndWaitcssinputt/fe=submit/ Should get an errorassertText Press ntno commentsassertTextPresentAuthor Is requiredtypeauthorMedickAndWaitcss-inputtype=submit/CheckassertTextPresentThanks for posting Mefal:白ssertText Present1 commenta ssertText Present| HelloNow just modify the test case t

21、o type any code in the text field, as is:type(author, Me) type(code, XXXXX) clickAndWait(css=inputtype=submit)And now run the test again, it should work.Measuring code coverageOf course we havent written all required test cases for the appli cation. But it s enough for this tutorial.Now in a real -w

22、orld project, howcanwe know if we have written enough test cases? We need something called code coverage .The generates code coverage reports using the tool. Install the module using the install command:play install cobertura-versionWeneed to enable this module only for test mode. So add this line t

23、o the application.conf file, and restartthe application in test mode.# Import the cobertura module in test mode %test.module.cobertura=$play.path/modules/coberturaNowreopen the browser at the URL, select all tests and run them. All should be green.Playt - Tests runner vl,0-beta-2 |+ * http: / /local

24、 host:9000/tests#Tests runnerSelect the tests to run, then click Start and prayThere is 1 unit test,+ BasicTestjava1 functional test,+ ApplicationTestjavaand 1 selenium test,Applicatkn.test.htmlChock hom9 pgeclearSession/ Open the home pageopen/ Check that the front post is presentassertTextPresentA

25、bout the model layerassert Text Pres entby Bob, 14 Jun 09assertT ext Present2 comments , latest by GuestassertT extPnesentVt is the tiomaispecific representation/ Check older postsassort TftjetPrpRRntTh冉MVH AnnliratinnWhe n all tests are passed, stop the applicati on and cobertura will the n gen era

26、te the code coveragereport. You can the n ope n theyabe/test-result/code-coverage/i ndex.html report.Coverage Report+ 鼻file:/Volumes/Data/Desktop/play/samples-and-rests/yabe/test-resuit/code-coverage/incfe)Coverage Report - controhers.Applicationin your browser and check thePackagesAllf(i亡fuit)gntro

27、llgr$modelsClasses in this FileLine CoverageApplication68% 20;controllersCfassesAdmin f6%JMDliuHiOn (6m)匸RUD (7o)Check WA) Ctjrnm亡cts fMJPxts fO%J(6%)gEturity (Oa) Tansfo%J1234567890134516 917gISg1920import play,*;iaport play.mve.* jimport play data 旦1丨過己tion * 7import playlib*;import play.cache.*;iapcrt java util *;models-*;0 public clasb Application extends Controller Beforestatic void addDfault&() renderArgs put ( blogTit

溫馨提示

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

評論

0/150

提交評論