Yii2 Day 7: 使用codeception测试框架

Codeception简介

项目大了,功能多了,各种自动测试工具少不了。在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目录,目录结构如下:

tests目录结构

值得注意的是,codeception目录下,包含了各种测试的示例代码,可以参考模仿示例代码写单元测试,交付测试和功能测试。

交付测试例子

在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运行测试用例,结果如下:

<!-- 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次断言都是成功的,测试通过。

转载于:https://my.oschina.net/u/248080/blog/380966

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值