


版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、10. 完整的应用程序测试现在,我们已经结束了博客引擎的编码工作, 但对项目来说还没有完成, 为了让 我们的代码能够完全正确的工作,我们还需要对项目进行测试。当然,我们之前已经为 yabe 的模型层功能书写的单元测试,并且确信博客引擎 的核心功能已经进行了完好的测试,但是对于一个web应用程序来说模型层只是 其中的一部分,我们还需要确定web接口能否按预期的目标一样正常工作。 也就 是说还需要测试 yabe 博客引擎的控制器层, 甚至需要对 UI 自身进行测试, 比如 我们的 JavaScript 代码。测试控制器部分Play 提供了一种功能,就是使用 JUnit 来直接测试应用程序的控制器。
2、我们把 这些测试叫做功能性测试,这是因为我们打算测试web应用程序的完整功基本上,一个功能性测试将直接调用Play的Actionlnvoker,和一个HTTP青求相似。因此我们需要给出一个 HTTP方法、一个URI和多个HTTP参数。Play之 后会路由这些青求,调用相应的 action ,并且回发到填写的响应( filled response )。之后,你就可以对之进行分析,以检查响应内容是否你所预期的。接下来让我们书写第一个功能性测试代码,打开 yabe/test/ApplicationTest.java 单元测试 :import org.junit.*;import play.test.
3、*;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);现在看,它还是一个标准的JUnit测试。请注意,在这里我们
4、使用 Play的 Fun cti on alTest超类,主要是为了得到所有可用的工具。这个测试只对应用程 序的主页进行了测试(/ URL渲染一个HTML向应,以200 OK作为状态代码)接下来,我们将检查管理区域(admi ni stration area)的安全工作能否正常工作。在ApplicationTest.java里添加下面这个新测试:Testpublic void testAdmi nSecurity() Resp onse resp onse = GET(/adm in);assertStatus(302, resp on se); assertHeaderEquals(Loca
5、tio n, /logi n, resp on se);现在,用play test 命令把yabe应用程序运行于测试模式,打开 http:/localhost:9000/tests ,选择 ApplicationTest.java测试并运行。是绿色的吗?-J-0httpf/localhosr9OOO/test&Ti.1 lest to run (BooHnnarkth隐 lin技to 生合w th淸 configulotion)There is 1 unit test,BasicTestjava1 functional test,ApplicationTestJavat fist That I
6、 ndex Page WorksOktest Adm i nS ecurityOkand 1 selenium test,Application.testhtml当然!通过这种方式,我们可以对所有的应用程序功能性进行测试,但把这用于测试一个基于html的web应用程序时,这并不是最好的方式。对于我们的博客 引擎项目来说,直接在真实的浏览器进行测试可能会更好。这就是 play的Selenium tests 测试要干的事。这种基于“功能性测试”的JUnit仍旧很有用,特别是用于测试一个返回非html 响应(比如 JSON或 XML的 Web services 时。书写Selenium测试代码Sel
7、enium是一个专用于测试web应用程序的测试工具。这个工具最酷的就是Selenium允许我们在一个浏览器里直接运行测试套件,由于它使用的是真实的 浏览器,因此,我们可以确定测试通过后,项目就可以在生产环境下完美的运行。一个Selenium测试套件就是一个特殊的 html文件。HTML syntax required bySelenium必须使用的HTM语句比较单调(使用HTM表格元素进行数据格式化显 示),好消息是play将使用play模板引擎和一系列支持简单 Selenium表示语法 的标签来帮助你生成这些元素)。使用模板最有趣的特点是你根本不需要 static scenarios ,并且
8、可以使用play模板强大的功能(如循环、条件块) 来书写更复杂的测试。然而,你仍旧可以继续在模板里使用原始的 HTMLSelenium语法,如果需要的话, 还可以忘记特定的Selenium标签。如果你使用多个用于生成test seenarios(比 如Selenium IDE )的Selenium工具中的一个,这将变得非常有趣。新创建的play应用程序的默认测试套件已经包含了一个Selenium测试,打开yabe/test/Applicatio n. test.html文件:* You can use pla in Sele nium comma nds using the sele nium
9、 tag *#sele nium/ Open the home page, and check that no error occurredope n(/)waitForPageToLoad(IOOO)assertNotTitle(Application error)#/sele nium运行这个测试应该不会有任何问题。它只打开了主页,并检测页面内容是否包含了 Applicati on error文本。然而,和任何复杂的测试一样,在导航到应用程序并进行测试之前, 你需要设置 一系列众所周知的数据,我们当然需要重用fixture概念,并且在开始测试之前 使用yabe/test/data.yml
10、文件,#fixture / 标签导入这些测试数据:#fixture delete:all, load:data.yml /#sele nium/ Open the home page, and check that no error occurredope n(/)waitForPageToLoad(IOOO)assertNotTitle(Application error)#/sele nium另外一个重要的事情就是我们要在测试启动时检查我们是否有一个最新的用户 session 。这个session将存储在浏览器的临时cookie里,你应该在两个连续的测试运行操作期间保持同一个session
11、,因此,让我们用一个特定的命令开始测 试:#fixture delete:all, load:data.yml /#sele niumclearSessio n()/ Open the home page, and check that no error occurredope n(/)waitForPageToLoad(1000)assertNotTitle(Application error)#/sele nium运行这个测试,并确定没有错误发生,结果应该是绿色的。接下来我们将书写很特殊的测试,测试打开主页后检查默认的博文是否显示出 来:#fixture delete:all, load:
12、data.y ml /#sele nium Check home pageclearSessio n()/ Open the home pageope n(/)/ Check that the front post is prese ntassertTextPrese nt(About the model layer)assertTextPrese nt(by Bob, 14 Jun 09)assertTextPrese nt(2 comme nts , latest by Guest)assertTextPresent(It is the domain-specific representa
13、tion)/ Check older postsassertTextPrese nt(The MVC 即 plicatio n)assertTextPrese nt(Just a test of YABE)#/sele nium在这里,我们使用了标准的 Selenium语法,它叫Selenese。运行它(你可以运行于一个不同的浏览器窗口里)。RjyX testCAecft home pagedearSssi onff Open the home pagepen/Seleinium Fun匚日I T 亡st Runner vl.C-bet/ Check vhat the front post 凸
14、 presenrass.ertTextPrsent.About thelayerasertText Presentby Bob, H Jun 09esse rtTe)ct Present2 comments latest byGuestassertText PresentIt is th e domain-sped fie representation/Check older postsrrT ext PresentThe MVC applieatiorassercText PresentJust a test of YA8Eyabe.Yet another blogWe will write
15、 about no thingThe MVC applicationbv Jeff.OSJunOd我们现在就可以测试评论窗体了,只需要添加一个#sele nium /到模板里即可:#sele nium Test comme nts/ Click on The MVC application post clickA ndWait(li nk=The MVC applicatio n) assertTextPrese nt(The MVC 即 plicatio n) assertTextPrese nt( no comme nts)/ Post a new commenttype(content,
16、 Hello) clickAndWait(css=inputtype=submit)/ Should get an errorassertTextPresent(no comments) assertTextPresent(Author is required) type(author, Me) clickAndWait(css=inputtype=submit)/ Check assertTextPresent(Thanks for posting Me) assertTextPresent(1 comment) assertTextPresent(Hello)#/selenium再次才能,
17、哦,失败了!这里有一个严重的问题出现Pfyf testApplication .test.htiTilesse rtT e xtPr esentTte MVC applicatiorassertrext Presentno comments P&st 3 n&w出白)ttypecontentHeclickAndWa itcss-ir puttype-5ii bmitZf Should get errorasMrtTert Presentno commentsassertText PresentAuthor if requiredtypeauthorMedickAndWattcss=ir put
18、tpe=su bmit/fCh&ck assertTextPre&entf&r posting MefnlwassertTexl Present1 ommentassertText PresentHeliono commentsPost a commentPlease 1ype the codeYour name:MeYour imsssage:我们事实上不能正确测试captcha验证码机制,因此,我们必须搞一些欺骗手段。 在测试模式下,我们将验证任何代码作为一个正确的验证码。我们知道当框架a. Weknow that we re in test modewhenthe framework i
19、d is test. So let s modify the postComme nt acti on in theyabe/app/controllers/Application .java file to skip this validation intest mode: if(!Play.id.equals(test) validati on. equals(code,Cache.get(ra ndomlD).message(l nv alidcode.Please type it aga in);Now just modify the test case to type any cod
20、e in the text field, as is: type(author, Me)type(code, XXXXX)clickA ndWait(css=in puttype=submit)And now run the test aga in, it should work.Measuri ng code coverageOf course we haven t written all required test cases for the application. But it s eno ugh for this tutorial. Now in a real -world proj
21、ect, how can we know if we have writte n eno ugh test cases? We n eed someth ing called code coverage .The Cobertura module gen erates code coverage reports using the Cobertura tool. I nstall the module using the in stall comma nd:play in stall cobertura-versionWeneed to enable this module only for
22、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 http:/localhost:9000/tests URL, select all tests and run them. All should be g
23、ree n.a c cPlayf - Tests runner vl.O- beta-2 嘴 卜+ J0http:/locdhost:9000sts-#Tests runnerStart ISelect the tests to run, then click Start and pray3 tests to mn Bookmark this link 怕 sa帕 this configu包IkmThere is 1 unit test,BaskTestjava1 functi onal test.ApplicationT estjavaand 1 selenium test,Applicat
24、ion.te st.htm Ici&arSession“ Open th$ home pageop&n/Check that the front post 眉 presentassertT&xtPrfrsentA tout the model layerassertTextPr&sentby Bob: 14 Jun 09assertTextPr&sent2 comments , latest by Gu&slassertTeixtPr&sentIt Ie the(lom a in-specific representationJf Ch&k older tensRftrfT rx iPrss
25、Rnl-Thft MVC 恥nli仃卅ienWhe n all tests are passed, stop the applicati on and cobertura will the n gen erate the code coverage report. You can the n ope n thein your browser and check theyabe/test-result/code-coverage/i ndex.html report.Coverage Fte port+ 圖 file/Volumes/Dila/desktop/pldy/sampl-es-and-
26、tests/yabe/test-res-ut/tode-caverage/indePackages仏fault】Coverage Reoort - controllers,ApplicationClasses in thi FileLir Cove rageAddIic ationcontxoLLra;contra flersddmin (6%) 戌口口limti口n 匚RUD f7%; 匚hek WA) CcnnmEntg5CK ll r亡伍丿Security 丁(0%)U5CS (0%)12J45肝7851011121314150161718g9gig202122232222402526272829222300313233343536373S39404122042432inQirt pityjin.porL pl ay , mvc + * :import play.daLftHvalidaLionH*;Lnpart pl ay.lib *;import j av a uta.1 uL( bloCaael
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年高考物理“专项突破”针对性强化试题(二)
- 2025年高考物理“物理与医疗健康”关联试题
- 改革专项答题题库及答案
- 工厂职员考试题目及答案
- 2025年福建省水投勘测设计有限公司劳务派遣综合办公室招聘1人模拟试卷及一套参考答案详解
- 爱的礼物妈妈的手套写物(8篇)
- 2025内蒙古自治区直属厅局某协会招聘工作人员模拟试卷及完整答案详解1套
- 干涉仪考试题及答案
- 甘肃导演艺考试题及答案
- 项目会议纪要与任务跟进模板
- 《肺炎性假瘤》课件
- 照片档案整理规范
- 公安新闻宣传知识讲座
- 2023新能源集控中心及智慧电厂建设方案
- 人工智能(基础版)高职人工智能基础课程PPT完整全套教学课件
- 10胃十二指肠溃疡临床路径表单
- 高标准农田施工组织设计(全)
- 学法减分100道题题库及答案(驾驶证学法减分学法免分题库及答案)
- 《安娜·卡列尼娜》-课件-
- 2022年新版体系文件药品零售单体连锁总部质量管理体系文件
- 校服登记表模板
评论
0/150
提交评论