命令行方式执行YUITest单元测试 — Kejun's Blog
命令行方式执行YUITest单元测试
用命令行方式自动执行单元测试首先是方便。重要的是用YUITest+Selenium+Hudson可以形成一个完整的CI系统。Zakas开发的YUITest Selenium Driver可以实现用命令行方式自动执行YUITest写的单元测试。虽然用Rhino + Envjs + Qunit也能实现,但毕竟不是在真实的浏览器下测试,同时测一些DOM交互操作时还会出问题。用Selenium IDE直接录的test case只能完成简单的UI功能测试。YUITest很强,可以测Ajax异步交互,可以模拟各种用户操作,详细介绍(http://developer.yahoo.com/yui/yuitest/)。在Resig统计的Top 10 Testing Tools排老四(http://www.flickr.com/photos/jeresig/4139206863/sizes/o/)环境要求:1. JRE 1.5+2. Tomcat (Mac OS X下安装说明http://www.malisphoto.com/tips/tomcatonosx.html)3. Selenium Core, Selenium RC (http://seleniumhq.org/download/)安装YUITest selenium driver:到这里 http://github.com/nzakas/yuitest 将最新源码git clone到本地,再用ant编译。最后得到3个jar文件:-rw-r–r– 1 kejun staff 890K 12 8 10:56 yuitest-coverage-0.3.7.jar-rw-r–r– 1 kejun staff 768K 12 8 10:56 yuitest-coverage-report-0.3.7.jar-rw-r–r– 1 kejun staff 797K 12 8 10:56 yuitest-selenium-driver-0.4.3.jar前两个用来生成报告的这里不讨论。yuitest-selenium-driver-0.4.3.jar是利用Selenium自动执行YUITest单元测试。为了省事,也可以直接到这里下载:http://github.com/nzakas/yuitest/downloadsSelenium Core部署到Tomcat下。Selenium RC解压在任意目录下,它包含下面几个目录:drwxr-xr-x@ 13 kejun staff 442B 6 9 2009 selenium-dotnet-client-driver-1.0.1drwxr-xr-x@ 7 kejun staff 238B 6 9 2009 selenium-java-client-driver-1.0.1drwxr-xr-x@ 9 kejun staff 306B 6 9 2009 selenium-perl-client-driver-1.0.1drwxr-xr-x@ 9 kejun staff 306B 6 9 2009 selenium-php-client-driver-1.0.1drwxr-xr-x@ 10 kejun staff 340B 6 9 2009 selenium-python-client-driver-1.0.1drwxr-xr-x@ 3 kejun staff 102B 6 9 2009 selenium-ruby-client-driver-1.0.1drwxr-xr-x@ 9 kejun staff 306B 6 9 2009 selenium-server-1.0.1一个selenium server和几个不同语言的client。这里我用了java版的,把selenium-java-client-driver.jar拷到JAVA_HOM/lib/ext下。如:Mac OS X是复制到/Library/Java/Home/lib/ext/下。环境就算搞完了。下面从YUI 2文件包的tests目录下随便找个单元测试页面试试。比如测试拖拽tests/dragdrop.html。但注意目前YUI 2.8.0的YUITest还不支持Selenium Driver,2.8.1就会支持了。没关系先从github上存一份:http://github.com/yui/yui2/raw/master/build/yuitest/yuitest-min.js 修改dragdrop.html用最新的yuitest-min.js。ok,开始测试。启动selenium server:$ java -jar selenium-server-1.0.1/selenium-server.jar00:17:39.674 INFO – Java: Apple Inc. 1.5.0_20-14100:17:39.712 INFO – OS: Mac OS X 10.5.8 i38600:17:39.813 INFO – v1.0.1 [2696], with Core v@VERSION@ [@REVISION@]00:17:40.185 INFO – Version Jetty/5.1.x00:17:40.186 INFO – Started HttpContext[/,/]00:17:40.188 INFO – Started HttpContext[/selenium-server,/selenium-server]00:17:40.188 INFO – Started HttpContext[/selenium-server/driver,/selenium-server/driver]….执行测试:$ java -jar yuitest-selenium-driver-0.4.3.jar http://localhost/~kejun/yui/tests/dragdrop.htmlRunning http://localhost/~kejun/yui/tests/dragdrop.htmlTestsuite: yuisuite on firefoxTests run: 8, Failures: 0, Ignored: 0很酷。同时在当前目录下生成测试报告文件:rw-r–r– 1 kejun staff 376B 12 8 17:40 test-firefox-yuitests1260265210491.xmlYUITest Selenium driver的命令行格式: