项目大了,功能多了,各种自动测试工具少不了。在Yii1的时候,一般推荐使用PHPUnit进行单元测试,用selenium进行功能和集成测试。这两个玩意虽然很强大,但是安装配置过程真是让人郁闷,Windows环境下的PEAR安装更让人吐血。本来程序员就不大自觉自愿地写测试代码了,现在连环境配置都这么费劲,谁还有兴趣啊?
事物总是发展变化的,Composer成功取代PEAR成为PHP包管理工具,测试框架方面,Codeception也在2012年推出了稳定版本,目前已经是2.x版本了。Yii2特地做了个yii2-codecept扩展,用来简化Yii2应用的测试工作。
安装还是推荐用composer,方便快速,无痛苦。
<!-- lang: shell -->
composer global require "codeception/codeception=2.0.*"
composer global require "codeception/specify=*"
composer global require "codeception/verify=*"
安装好了之后,最好将composer目录下vendor/bin加到PATH里,这样运行codeception命令就很方便啦。如果安装composer,或者运行命令缓慢,可能参考教程:
安装
http://my.oschina.net/u/248080/blog/351424
更新
http://my.oschina.net/u/248080/blog/378825
安装成功后,打开要进行测试到项目,cd到项目根目录,运行命令codecept bootstrap,完成测试框架到初始化工作。
<!-- lang: shell -->
HuiMac:mf ice$ codecept bootstrap
Initializing Codeception in /Applications/XAMPP/xamppfiles/htdocs/mf
File codeception.yml created <- global configuration
tests/unit created <- unit tests
tests/unit.suite.yml written <- unit tests suite configuration
tests/functional created <- functional tests
tests/functional.suite.yml written <- functional tests suite configuration
tests/acceptance created <- acceptance tests
tests/acceptance.suite.yml written <- acceptance tests suite configuration
tests/_output was added to .gitignore
tests/_bootstrap.php written <- global bootstrap file
Building initial Tester classes
Building Actor classes for suites: acceptance, functional, unit
\AcceptanceTester includes modules: PhpBrowser, AcceptanceHelper
AcceptanceTester.php generated successfully. 48 methods added
\FunctionalTester includes modules: Filesystem, FunctionalHelper
FunctionalTester.php generated successfully. 13 methods added
\UnitTester includes modules: Asserts, UnitHelper
UnitTester.php generated successfully. 17 methods added
Bootstrap is done. Check out /Applications/XAMPP/xamppfiles/htdocs/mf/tests directory
初始化完毕后,在项目根目录下会多一个tests目录,目录结构如下:
值得注意的是,codeception目录下,包含了各种测试的示例代码,可以参考模仿示例代码写单元测试,交付测试和功能测试。
在acceptance目录下新建文件,LoginCept.php, 加入以下代码:
$I = new AcceptanceTester($scenario); //创建测试实例
$I->wantTo('perform login test'); //说明测试的目的
$I->amOnPage('/web/user/login/admin'); //打开登录页面
$I->see('Admin Login'); //是否看到Admin Login的文字?
$I->see('/mf/web/user/login/admin'); //是否看到登录链接
$I->fillField('AdminLoginForm[username]','root'); //填写用户名
$I->fillField('AdminLoginForm[password]','root'); //填写密码
$I->click(['name'=>"login-button"]);//点击登录按钮
$I->see('Module Admin');//查看是否登录成功,看到管理页面
通过命令 codecept run运行测试用例,结果如下:
<!-- lang: shell -->
HuiMac:mf ice$ codecept run
Codeception PHP Testing Framework v2.0.11
Powered by PHPUnit 4.4.5 by Sebastian Bergmann.
Acceptance Tests (1) ---------------------------------------------------------------------------------------------
Perform login test (LoginCept) Ok
------------------------------------------------------------------------------------------------------------------
Functional Tests (0) ------------------------
---------------------------------------------
Unit Tests (0) ------------------------------
---------------------------------------------
Time: 1.18 seconds, Memory: 12.25Mb
OK (1 test, 3 assertions)
1个测试用例运行成功,3次断言都是成功的,测试通过。