这里linux环境为centos6.7,此版本需要php5.6及以上环境
PHPUnit全局安装
1、下载 PHPUnit 的 PHP 档案包 (PHAR),它将 PHPUnit 所需要的所有必要组件(以及某些可选组件)捆绑在单个文件中:
$ wget https://phar.phpunit.de/phpunit-5.6.3.phar
2、添加可执行权限
$ chmod +x phpunit-5.6.3.phar
3、安装到指定目录
$ mv phpunit-5.6.3.phar /usr/local/bin/phpunit
4、测试是否安装
a、查看版本信息
$ phpunit --version
会看到如下信息:
PHPUnit 5.6.3 by Sebastian Bergmann and contributors.
建立测试文件:
/www/wwwroot/test/hrTest.php(文件名为xxTest.php格式)
其中内容为(注意类名及方法名的写法):
<?php
class hrTest extends PHPUnit_Framework_TestCase{
public function testIndex(){
$arr=array();
$this->assertEquals(1,count($arr));//期待的结果为1,但是实际上为0,这是错误的,为什么要这么做,是为了看看phpunit是否生效
}
}
?>
开始测试:phpunit hrTest.php
[root@iZ62vgzgugyZ test]# phpunit hrTest.php
PHPUnit 5.6.3 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 164 ms, Memory: 3.00MB
There was 1 failure:
1) hrTest::testIndex
Failed asserting that 0 matches expected 1.
/www/wwwroot/test/hrTest.php:5
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
看到失败,表示程序有错误,我们再将hrTest.php中的那行错误代码改一下
$this->assertEquals(0,count($arr));
再测一次,成功:
[root@iZ62vgzgugyZ test]# phpunit hrTest.php
PHPUnit 5.6.3 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 207 ms, Memory: 3.00MB
OK (1 test, 1 assertion)
[root@iZ62vgzgugyZ test]#
如果用phpunit hrTest.php时报如下错误:
/usr/bin/env: php: No such file or directory
主要是php安装文件不在/usr/local/bin。假如php安装在/usr/local/php5513中
找到php的可执行文件,/usr/local/php5513/bin/php
ln -s /usr/local/php5513/bin/php /usr/local/bin/php
把可执行文件连接过去就可以了。
- 相关文章
- phpunit单元测试例子-断言
- 热门文章
- win7中将文件拷贝到虚拟机linux下
- phpexcel设置行高及列宽,背景颜色,
- rabbitmq无法启动
- intellij idea不显示git push按钮
- php7中使用mongodb的aggregate进行
- centos7.4 64位下swoole安装及配置
- laravel页面静态化的方法
- navicate连接mycat报1184错误
- 单点登录sso原理及php实现方式及de
- devops-jenkins容器为pending状态
- 好评文章
- phpexcel设置行高及列宽,背景颜色,
- php7中使用mongodb的aggregate进行
- intellij idea打开文件所在文件夹
- windows下使用MongoDB Compass Com
- win7中将文件拷贝到虚拟机linux下
- laravel 中悲观锁 & 乐观锁的使用
- 单点登录sso原理及php实现方式及de
- navicate连接mycat报1184错误
- rabbitmq无法启动
- laravel整合dingo/api方法步骤:jwt
- 我的项目
- 【github】www.github.com/hurong241
- 【码云】gitee.com/hu_rong/projects
- 【docker hub】hub.docker.com/repositories/hurong241
- 【packagist】packagist.org/users/hurong241/packages
- 站点信息
- 建站时间:2011年
- 文章数:607篇
- 浏览数:915207