項目大了,功能多了,各類自動測試工具少不了。在Yii1的時候,通常推薦使用PHPUnit進行單元測試,用selenium進行功能和集成測試。這兩個玩意雖然很強大,可是安裝配置過程真是讓人鬱悶,Windows環境下的PEAR安裝更讓人吐血。原本程序員就不大自覺自願地寫測試代碼了,如今連環境配置都這麼費勁,誰還有興趣啊?php
事物老是發展變化的,Composer成功取代PEAR成爲PHP包管理工具,測試框架方面,Codeception也在2012年推出了穩定版本,目前已是2.x版本了。Yii2特意作了個yii2-codecept擴展,用來簡化Yii2應用的測試工做。git
安裝仍是推薦用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,或者運行命令緩慢,可能參考教程:web
安裝 http://my.oschina.net/u/248080/blog/351424 更新 http://my.oschina.net/u/248080/blog/378825shell
安裝成功後,打開要進行測試到項目,cd到項目根目錄,運行命令codecept bootstrap,完成測試框架到初始化工做。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目錄,目錄結構以下:yii2
值得注意的是,codeception目錄下,包含了各類測試的示例代碼,能夠參考模仿示例代碼寫單元測試,交付測試和功能測試。composer
在acceptance目錄下新建文件,LoginCept.php, 加入如下代碼:框架
<!-- lang: 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運行測試用例,結果以下:yii
<!-- 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次斷言都是成功的,測試經過。