play手把手教你创建一个博客项目-10完整的应用程序测试_第1页
play手把手教你创建一个博客项目-10完整的应用程序测试_第2页
play手把手教你创建一个博客项目-10完整的应用程序测试_第3页
play手把手教你创建一个博客项目-10完整的应用程序测试_第4页
play手把手教你创建一个博客项目-10完整的应用程序测试_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.:.;10.完好的运用程序测试如今,我们曾经终了了博客引擎的编码任务,但对工程来说还没有完成,为了让我们的代码可以完全正确的任务,我们还需求对工程进展测试。 当然,我们之前曾经为yabe的模型层功能书写的单元测试,并且确信博客引擎的中心功能曾经进展了完好的测试,但是对于一个web运用程序来说模型层只是其中的一部分,我们还需求确定web接口能否按预期的目的一样正常任务。也就是说还需求测试yabe博客引擎的控制器层,甚至需求对UI本身进展测试,比如我们的JavaScript代码。测试控制器部分Play提供了一种功能,就是运用JUnit来直接测试运用程序的控制器。我们把这些测试叫做功能性测试,这是

2、由于我们计划测试web运用程序的完好功能。根本上,一个功能性测试将直接调用Play的ActionInvoker,和一个HTTP恳求类似。因此我们需求给出一个HTTP方法、一个URI和多个HTTP参数。 Play之后会路由这些恳求,调用相应的action,并且回发到填写的呼应filled response。之后,他就可以对之进展分析,以检查呼应内容能否他所预期的。接下来让我们书写第一个功能性测试代码,翻开 yabe/test/ApplicationTest.java单元测试:import org.junit.*;import play.test.*;import play.mvc.*;impor

3、t play.mvc.*;import models.*; public class ApplicationTest extends FunctionalTest Test public void testThatIndexPageWorks() Response response = GET(/); assertIsOk(response); assertContentType(text/html, response); assertCharset(utf-8, response); 如今看,它还是一个规范的JUnit测试。请留意,在这里我们运用Play的 FunctionalTest超类,

4、主要是为了得到一切可用的工具。这个测试只对运用程序的主页进展了测试/ URL渲染一个HTML呼应,以200 OK作为形状代码)。接下来,我们将检查管理区域administration area的平安任务能否正常任务。在ApplicationTest.java里添加下面这个新测试:Testpublic void testAdminSecurity() Response response = GET(/admin); assertStatus(302, response); assertHeaderEquals(Location, /login, response);如今,用play test命令

5、把yabe运用程序运转于测试方式,翻开 HYPERLINK localhost:9000/tests localhost:9000/tests, 选择ApplicationTest.java测试并运转。是绿色的吗?当然!经过这种方式,我们可以对一切的运用程序功能性进展测试,但把这用于测试一个基于html的web运用程序时,这并不是最好的方式。对于我们的博客引擎工程来说,直接在真实的阅读器进展测试能够会更好。这就是play的Selenium tests测试要干的事。这种基于“功能性测试的JUnit仍旧很有用,特别是用于测试一个前往非html呼应(比如JSON或XML)的Web services时

6、。书写Selenium测试代码 HYPERLINK Selenium 是一个公用于测试web运用程序的测试工具。这个工具最酷的就是Selenium允许我们在一个阅读器里直接运转测试套件,由于它运用的是真实的阅读器,因此,我们可以确定测试经过后,工程就可以在消费环境下完美的运转。一个Selenium测试套件就是一个特殊的html文件。HTML syntax required by Selenium必需运用的HTML语句比较单调(运用HTML表格元素进展数据格式化显示),好音讯是play将运用play模板引擎和一系列支持简单Selenium表示语法的标签来协助 他生成这些元素。运用模板最有趣的特点

7、是他根本不需求static scenarios,并且可以运用play模板强大的功能如循环、条件块来书写更复杂的测试。然而,他仍旧可以继续在模板里运用原始的HTML Selenium语法,假设需求的话,还可以忘记特定的Selenium标签。假设他运用多个用于生成test scenarios(比如 HYPERLINK /projects/ide Selenium IDE)的Selenium工具中的一个,这将变得非常有趣。新创建的play运用程序的默许测试套件曾经包含了一个Selenium测试,翻开yabe/test/Application.test.html文件:* You can use pla

8、in Selenium commands using the selenium tag * #selenium / Open the home page, and check that no error occurred open(/) waitForPageToLoad(1000) assertNotTitle(Application error)#/selenium运转这个测试应该不会有任何问题。它只翻开了主页,并检测页面内容能否包含了 Application error文本。然而,和任何复杂的测试一样,在导航到运用程序并进展测试之前,他需求设置一系列众所周知的数据,我们当然需求重用fix

9、ture概念,并且在开场测试之前运用yabe/test/data.yml文件,#fixture /标签导入这些测试数据:#fixture delete:all, load:data.yml / #selenium / Open the home page, and check that no error occurred open(/) waitForPageToLoad(1000) assertNotTitle(Application error)#/selenium另外一个重要的事情就是我们要在测试启动时检查我们能否有一个最新的用户session。这个session将存储在阅读器的暂时co

10、okie里,他应该在两个延续的测试运转操作期间坚持同一个session,因此,让我们用一个特定的命令开场测试:#fixture delete:all, load:data.yml / #selenium clearSession() / Open the home page, and check that no error occurred open(/) waitForPageToLoad(1000) assertNotTitle(Application error)#/selenium运转这个测试,并确定没有错误发生,结果应该是绿色的。接下来我们将书写很特殊的测试,测试翻开主页后检查默许的

11、博文能否显示出来:#fixture delete:all, load:data.yml / #selenium Check home page clearSession() / Open the home page open(/) / 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(I

12、t is the domain-specific representation) / Check older posts assertTextPresent(The MVC application) assertTextPresent(Just a test of YABE)#/selenium在这里,我们运用了规范的Selenium语法,它叫 HYPERLINK /docs/02_selenium_ide.html l selenium-commands-selenese Selenese。运转它他可以运转于一个不同的阅读器窗口里。我们如今就可以测试评论窗体了,只需求添加一个 #seleni

13、um / 到模板里即可:#selenium Test comments / Click on The MVC application post clickAndWait(link=The MVC application) assertTextPresent(The MVC application) assertTextPresent(no comments) / Post a new comment type(content, Hello) clickAndWait(css=inputtype=submit) / Should get an error assertTextPresent(no

14、 comments) assertTextPresent(Author is required) type(author, Me) clickAndWait(css=inputtype=submit) / Check assertTextPresent(Thanks for posting Me) assertTextPresent(1 comment) assertTextPresent(Hello)#/selenium再次才干,哦,失败了!这里有一个严重的问题出现。我们现实上不能正确测试captcha验证码机制,因此,我们必需搞一些欺骗手段。在测试方式下,我们将验证任何代码作为一个正确的验

15、证码。我们知道当框架a. We know that were in test mode when the framework id is test. So lets modify the postComment action in the yabe/app/controllers/Application.java file to skip this validation in test mode:if(!Play.id.equals(test) validation.equals(code, Cache.get(randomID).message(Invalid code. Please ty

16、pe it again);Now just modify the test case to 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 application. But its e

17、nough for this tutorial. Now in a real-world project, how can we know if we have written enough test cases? We need something called code coverage. The HYPERLINK /modules/cobertura Cobertura module generates code coverage reports using the HYPERLINK / Cobertura tool. Install the module using the ins

18、tall command:play install cobertura-versionWe need to enable this 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 HYPERLINK localhost:9000/tests localhost:9000/tests URL, select all tests and run

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论