




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、play手把手教你创建一个博客项目W完整的应用程序测试现在,我们差不多终止了博客引擎的编码工作,但对项日来讲还没有完成,为了 让我们的代码能够完全正确的工作,我们还需要对项U进行测试。因此,我们之前差不多为yabe的模型层功能书写的单元测试,同时确信博客引 擎的核心功能差不多进行了完好的测试,然而关于一个web应用程序来讲模型层 只是其中的一部分,我们还需要确定web接口能否按预期的U标一样正常工作。 也确实是讲还需要测试Wbe博客引擎的操纵器层,其至需要对UI自身进行测试, 比如我们的JavaScript代码。测试操纵器部分Play提供了一种功能,确实是使用JUnit来直截了当测试应用程序的
2、操纵器。 我们把这些测试叫做功能性测试,这是因为我们打算测试web应用程序的完 整功能。差不多上,一个功能性测试将直截了当调用Play的Act ioninvoker T和一个HTTP 要求相似。因此我们需要给出一个HTTP方法、一个lRI和多个HTTP参数。Play 之后会路山这些要求,调用相应的action,同时回发到填写的响应(filled response)。之后,你就能够对之进行分析,以检查响应内容是否你所预期的。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现在看,它依旧一个标准的JUnit测试。请注意,在那个地点我们使用Play的 Function
4、alTest超类,要紧是为了得到所有可用的工具。那个测试只对应用程 序的主页进行了测试(/URL渲染一个HTML响应,以2000K作为状态代码)。接下来,我们将检查治理区域(administration area)的安全工作能否正常工 作。在ApplicationTest. java里添加下面那个新测试:Testpublic void testAdminSecurity() Response response = GET (/admin); assertStatus(302, response);assertHeaderEquals(Location, /login, response);现在
5、,用play test命令把yabe应用程序运行于测试模式,打开,选择 ApplicationTest. java 测试并运行。是绿色的吗? | 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因此!通过这种方式,我们能够对所有的应用程序功能性进行测试,但把这用于 测试一个基于html的web应用程序时,这并不是最好的方式。关于我们的博客 引擎项U来讲,直截了当在真实的扫瞄器进行测试可能会更好。这确实是Phy 的Selenium tests测试要干的事。这种基于“功能性测试”的JUnit仍旧专门有用,专门是用于测试一个返回非 html 响应(比如 JS0N 或 XML)的 Web services 时。书写Selenium测试代码Selenium是一个专用于测试web应用程序的测试工具那个工具最酷的确实是 Selen
7、ium承诺我们在一个扫瞄器里直截了当运行测试套件,山于它使用的是真 实的扫瞄器,因此,我们能够确定测试通过后,项U就能够在生产环境下完美的 运行 一个Selenium测试套件确实是一个专门的html文件。HTML syntax required by Selenium必须使用的HTML语句比较单调(使用HTML表格元素进行数据格式化显 示),好消息是play将使用play模板引擎和一系列支持简单Selenium表示语法 的标签来关心你生成这些元素)使用模板最有味的特点是你全然不需要static scenarios* ,同时能够使用play模板强大的功能(如循环、条件块) 来书写更复朵的测试。然
8、而,你仍旧能够连续在模板里使用原始的HTML Selenium语法,假如需要的话, 还能够不记得特定的Selenium标签。假如你使用多个用于生成test scenarios(比如Selenium IDE)的Selenium IL具中的一个,这将变得专门有味。新创建的Py应用程序的默认测试套件差不多包含了一个Selenium测试,打开 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运行那个测试应该可不能有任何咨询题。它只打开了主页,并检测页面内容是否 包含 了 Application error * 文本。然而,和任何复朵的测试一样,在导航到应用程序并进行测试之前,你需要设置 一系列众所周知的数据,我们因此需要巫用fixture概念,同时在开始测试之前 使用yabe/test/data. yml文件,# fixture /标签导入这些测试数据:#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列外一个重要的悄况确实是我们要在测试启动时检查我们是否有一个最新的用 户sessiono那个session将储备在扫肺器的临时cookie里,你应该在两个连 续的测试运行操作期间保持同一个session,因此,让我们用一个特定的命令开 始
11、测试:#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运行那个测试,并确定没有错误发生,结果应该是绿色的。接下来我们将书写专门专门的测试,测试打开主页后检查默认的博文是否显示出 来:#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在那个地点,我们使用了标准的Selenium语法,它叫Selenese。运行它(你能够运行于一个不同的扫瞄器窗口里)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-加我们现在就能够测试评论窗体了,只需要添加一个#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再次才能,哦,失败了!那个地点有一个严峻的咨询题显现。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:我们事实上不能正确测试captcha验证码机制,因此,我们必须搞一些欺诈手段。 在测试模式下,我们将验证任何代码作为一个正确的验证码。我们明白当框架 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 现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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 境外投资管理办法新
- 处方管理办法常用量
- 双城区拆迁管理办法
- 南宁诚信卡管理办法
- 机运队管理办法细则
- 国企保证金管理办法
- 时间投资与管理办法
- 某公司报价管理办法
- 完善津补贴管理办法
- 注册消防师管理办法
- 脓毒性休克患儿的护理
- 广东省深圳市南山区2025年小升初数学模拟试卷含解析
- 二手房预订合同定金协议书模板
- 全渠道电商平台业务中台解决方案
- (高清版)DB36∕T 1324-2020 公路建设项目档案管理规范
- 深基坑开挖专项安全监理实施细则方案(2篇)
- 对赌融资协议书范文范本
- 国家开放大学《Web开发基础》形考任务实验1-5参考答案
- 《进一步规范管理燃煤自备电厂工作方案》发改体改〔2021〕1624号
- 中等职业技术学校人工智能技术应用专业(三年制)人才培养方案
- 箱式变电站技术规范书
评论
0/150
提交评论