play手把手教你创建一个博客项目-10完整的应用程序测试.docx

上传人:小飞机 文档编号:1701576 上传时间:2022-12-15 格式:DOCX 页数:11 大小:739.59KB
返回 下载 相关 举报
play手把手教你创建一个博客项目-10完整的应用程序测试.docx_第1页
第1页 / 共11页
play手把手教你创建一个博客项目-10完整的应用程序测试.docx_第2页
第2页 / 共11页
play手把手教你创建一个博客项目-10完整的应用程序测试.docx_第3页
第3页 / 共11页
play手把手教你创建一个博客项目-10完整的应用程序测试.docx_第4页
第4页 / 共11页
play手把手教你创建一个博客项目-10完整的应用程序测试.docx_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《play手把手教你创建一个博客项目-10完整的应用程序测试.docx》由会员分享,可在线阅读,更多相关《play手把手教你创建一个博客项目-10完整的应用程序测试.docx(11页珍藏版)》请在三一办公上搜索。

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.*;import

3、play.mvc.Http.*;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

5、test命令把yabe应用程序运行于测试模式,打开http:/localhost:9000/tests, 选择ApplicationTest.java测试并运行。是绿色的吗?当然!通过这种方式,我们可以对所有的应用程序功能性进行测试,但把这用于测试一个基于html的web应用程序时,这并不是最好的方式。对于我们的博客引擎项目来说,直接在真实的浏览器进行测试可能会更好。这就是play的Selenium tests测试要干的事。这种基于“功能性测试”的JUnit仍旧很有用,特别是用于测试一个返回非html响应(比如JSON或XML)的Web services时。书写Selenium测试代码Sele

6、nium 是一个专用于测试web应用程序的测试工具。这个工具最酷的就是Selenium允许我们在一个浏览器里直接运行测试套件,由于它使用的是真实的浏览器,因此,我们可以确定测试通过后,项目就可以在生产环境下完美的运行。一个Selenium测试套件就是一个特殊的html文件。HTML syntax required by Selenium必须使用的HTML语句比较单调(使用HTML表格元素进行数据格式化显示),好消息是play将使用play模板引擎和一系列支持简单Selenium表示语法的标签来帮助你生成这些元素)。使用模板最有趣的特点是你根本不需要static scenarios,并且可以使用

7、play模板强大的功能(如循环、条件块)来书写更复杂的测试。然而,你仍旧可以继续在模板里使用原始的HTML Selenium语法,如果需要的话,还可以忘记特定的Selenium标签。如果你使用多个用于生成test scenarios(比如Selenium IDE)的Selenium工具中的一个,这将变得非常有趣。新创建的play应用程序的默认测试套件已经包含了一个Selenium测试,打开yabe/test/Application.test.html文件:* You can use plain Selenium commands using the selenium tag * #seleni

8、um / Open the home page, and check that no error occurred open(/) waitForPageToLoad(1000) assertNotTitle(Application error)#/selenium运行这个测试应该不会有任何问题。它只打开了主页,并检测页面内容是否包含了 Application error文本。然而,和任何复杂的测试一样,在导航到应用程序并进行测试之前,你需要设置一系列众所周知的数据,我们当然需要重用fixture概念,并且在开始测试之前使用yabe/test/data.yml文件,#fixture /标签导入

9、这些测试数据:#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将存储在浏览器的临时cookie里,你应该在两个连续的测试运行操作期间保持同一个session,因此,让我们用一个特定的命令开始测

10、试:#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运行这个测试,并确定没有错误发生,结果应该是绿色的。接下来我们将书写很特殊的测试,测试打开主页后检查默认的博文是否显示出来:#fixture delete:all, load:data.yml / #seleni

11、um 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(It is the domain-specific representation) / Check olde

12、r posts assertTextPresent(The MVC application) assertTextPresent(Just a test of YABE)#/selenium在这里,我们使用了标准的Selenium语法,它叫Selenese。运行它(你可以运行于一个不同的浏览器窗口里)。我们现在就可以测试评论窗体了,只需要添加一个 #selenium / 到模板里即可:#selenium Test comments / Click on The MVC application post clickAndWait(link=The MVC application) assertT

13、extPresent(The MVC application) assertTextPresent(no comments) / Post a new comment type(content, Hello) clickAndWait(css=inputtype=submit) / Should get an error assertTextPresent(no comments) assertTextPresent(Author is required) type(author, Me) clickAndWait(css=inputtype=submit) / Check assertTex

14、tPresent(Thanks for posting Me) assertTextPresent(1 comment) assertTextPresent(Hello)#/selenium再次才能,哦,失败了!这里有一个严重的问题出现。我们事实上不能正确测试captcha验证码机制,因此,我们必须搞一些欺骗手段。在测试模式下,我们将验证任何代码作为一个正确的验证码。我们知道当框架a. We know that were in test mode when the framework id is test. So lets modify the postComment action in th

15、e 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 type it again);Now just modify the test case to type any code in the text field, as is:type(author, Me)type(code, XXXXX)

16、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 enough for this tutorial. Now in a real-world project, how can we know if we have written enough test cases? We need so

17、mething called code 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 this module only for test mode. So add this line to the application.conf file, and restart the app

18、lication 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 green.When all tests are passed, stop the application and cobertura will then

19、generate the code coverage report. You can then open the yabe/test-result/code-coverage/index.html in your browser and check the report.If you start the application again, you can also view it at http:/localhost:9000/cobertura.As you see were far from testing all of the applications cases. A good testing suite should approach 100%, even if it is of course nearly impossible to check all the code. Typically because we often need to hack in test mode, like we did for the captcha.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号