安装
-
安装PHPUnit
去官网下载相应的版本 https://phpunit.de/
shell $ chmod +x phpunit-x.x.phar $ sudo mv phpunit-x.x.phar /usr/local/bin/phpunit $ phpunit --version
-
CodeIgniter3集成PHPUnit
shell git clone https://github.com/kenjis/ci-phpunit-test.git
下载完后把tests
文件夹拷贝到CI目录application
文件夹里 -
配置环境
php define('HOME_URL', 'http://jingtao.cn'); //自定义常量
假如
index.php
上设置了自定义的常量就要得添加到tests/Bootstrap.php
这个文件里 -
测试
执行测试方法很简单,在
Terminal
上跳到tests
目录后执行phpunit
就可以了。shell $ cd /CI/apllication/tests/ $ phpunit
大概返回这样的``` shell $ phpunit
PHPUnit 5.7.23 by Sebastian Bergmann and contributors.
Error: No code coverage driver is available
... 3 / 3 (100%)
Time: 652 ms, Memory: 15.50MB
OK (3 tests, 3 assertions) ```
-
一些坑
-
显示下面错误:
Error: No code coverage driver is available
要安装xdebug
才可以, 用下面语句来查询是否成功安装xdebug
shell $ php -i | grep xdebug
显示PHP Warning: Unknown:
就说明没开启。 要注意的是命令行
和web服务器
用的php
是不一样的,我用的是MAMP
但是跟这个一毛钱关系都没有。shell $ php -i | grep php.ini
用这个命令来查找要修改的环境设置文件。 -
生成日志文件失败
tests/phpunit.xml
在这个文件上logging
标签给注释掉
-