来源:http://trac.seagullproject.org/wiki/Standards/UnitTesting
=====单元测试=====
==== 简介 ====
Seagull有一套单元测试的方案,按其重要性它们正在被顺序实现:
* trunk/lib/SGL目录中内核库文件的单元测试
* 确保正确无误载入页面的各个模块的web测试
* 每个模块的单元测试
文件类型
* .wdb.php - DB with tables
* .wdd.php - DB with tables and data
* .ndb.php - PHP only
* .web.php - web测试,如一个HTTP请求的解析结果
Seagull安装默认包含一个在浏览器中运行测试的测试工具。要运行测试工具你需要使用PEAR包管理器安装SimpleTest库:
* 前期需求:
o 安装PEAR包管理器 ([[http://pear.php.net/manual/en/installation.getting.php|How do I install the PEAR package manager?]])
o 你的PEAR库要包含在你的php.ini文件中的include_path指令中(通常是通过PEAR管理器安装时设置的)。
o 如果你刚刚安装了PEAR包管理器,在运行测试之前不要忘了重起web服务器(include_path, you know)。
* 步骤1: 将下列包安装到PEAR库。不要把它们放到Seagull的PEAR库。
$ pear channel-discover pear.phpkitchen.com
$ pear install phpkitchen/SimpleTest
$ pear install HTML_TreeMenu
* 步骤2: 导航到[[http://myhost/seagull/tests/]]
注意:在虚拟主机上如果web根目录是seagull/www它是不能正常工作的。请安装一个独立的虚拟主机并创建一个新的conf.ini文件。
* 步骤3: 第一次运行测试时,会将一个test.conf.ini文件复制到你的seagull/var目录,它应该包含和下面差不多的内容:
结果大概如下:
;------------------------------------------------------------------------------------------;
; Test Environment Settings - Make Sure The Following Are Correct! ;
;------------------------------------------------------------------------------------------;
[database]
type = mysql_SGL
host = localhost
port = 3306
user = root ; enter the appropriate user of your database
pass = ; enter the appropriate password of that user
name = simpletest ; Don't set this to be your project database - the test
; database is often dropped during testing!
[project]
name = My Project
projectMnemonic = _SGL ; use this to override global vars
[general]
directoriesToScan = modules,lib/SGL
initBridge = /etc/sglBridge.php
; Path should be relative to the tests install directory
[schemaFiles]
file1 = etc/sequence.my.sql
file2 = modules/default/data/schema.my.sql
file3 = modules/user/data/schema.my.sql
file4 = modules/block/data/schema.my.sql
[dataFiles]
file1 = modules/default/data/data.default.my.sql
file2 = modules/user/data/data.default.my.sql
file3 = modules/block/data/data.default.my.sql
注意:如果你在Seagull中使用的是PostgreSQL,你将不得不手工复制tests/test.pg.conf.ini-dist这个文件到var/test.conf.ini.php。
这将足够让你创建并运行.点击左手边的任何一个节点,导航将执行测试当前结点与其子结点.这个框架可以运行任何适合PHP项目的简单的单元测试
这些就足够你运行测试。在浏览器中,单击左边导航中的结点将执行当前结点和它的所有子结点的测试。Seagull简化了所有PHP项目的单元测试。在浏览器中测试[[http://trac.seagullproject.org/attachment/wiki/Standards/UnitTesting/unit_tests.png|看起来像这样]]
==== 定义你自己的测试 ====
当你创建新模块时,你想要给它添加单元测试和web测试。测试工具是基于一个测试数据库。每次测试的执行都需要建立测试数据库,它是基于ini文件建立,测试数据库一直被称为simpletest 。当你创建一个新模块时你要在/modules/$module/data目录中创建3个SQL文件,模式,默认数据和样品数据。将下列这几行添加到上面的配置文件中以便测试运行时载入这些数据:
。
[schemaFiles]
file5 = modules/foo/data/schema.my.sql
[dataFiles]
file4 = modules/foo/data/data.default.my.sql
file5 = modules/foo/data/data.sample.my.sql
==== 使用Selenium验收测试 ====
* 获取Selenium内核[[http://www.openqa.org/selenium-core/download.action]]
* 将解压后得到的文件放到www目录下,并重命名为selenium-core
* 导航到Seagull测试目录
* 进入Selenium Acceptance Test Runner
* 单击'run all tests'按钮,在框架的右上方,最左边的按钮
要自己写验收测试
* Firefox中使用Selenium IDE的记录测试 [[http://www.openqa.org/selenium-ide/]]
==== 更多参考资料 ====
* [[http://www.lastcraft.com/simple_test.php|lastcraft.com上的SimpleTest文档]]
* [[http://simpletest.sourceforge.net/|SimpleTest的API]]
* [[http://qa.php.net/write-test.php]]
* [[http://en.wikipedia.org/wiki/Test_driven_development|关于Test Driven Development的wikipedia站点]]
==== 附件 ====
* unit_tests.png (82.6 kB) - 测试屏幕截图,demian在05/16/06 10:56:23添加