phpunit/framework.php,PHPUnit札记(一)

PHPUnit笔记(一)

PHPUnit笔记(一)

最近构想自己要做的项目,涉及到web上的东西,要用php来做,因此自然就接触到了phpunit这个东西。

开发平台:Debian

首先是安装PHPUnit,配置环境当然是越简单越好,我是个懒人,我认...

PHPUnit安装1pear channel-discover pear.phpunit.de

2pear channel-discover components.ez.no

3pear channel-discover pear.symfony-project.com

4pear install phpunit/PHPUnit

结束后,会等一阵子,一开始我心急了,看见没啥反映,就直接ctrl+c了。事实证明我2了。

安装需要pear,当然,获取方法也继续 my style。

pear安装1apt-get install php5-curl php-pear安装curl,pear

2pear upgrade-all更新软件包

一切就绪后,可以在终端里输入phpunit,如果没有反映,就准备好好找原因吧。反正我相信linux上的软件问题,大多是依赖问题,以前尝试玩过lfs,好家伙,没让我少死脑细胞。

接着是使用phpunit。

根据baidu和google,大多使用如下:

require_once 'PHPUnit/Framework.php';

class ExceptionTest extends PHPUnit_Framework_TestCase

{

/**

* @expectedException InvalidArgumentException

*/

public function testException()

{

}

}

?>

然后在终端输入

1phpuint test.phptest.php为测试代码文件

悲剧的是,找不到PHPUnit/Framework.php这个文件,于是查看php.ini的include_path参数后,找到了phpunit目录,发现了Framework目录。

require_once 'PHPUnit/Framework/TestCase.php';

class ArrayTest extends PHPUnit_Framework_TestCase{

public function testNewArrayIsEmpty(){

/*Create the Array fixture*/

$fixture = array();

/* Assert that the size of the Array * fixture is 0*/

$this->assertEquals(0, sizeof($fixture));

}

public function testArrayContainsAnElement(){

/* Create the Array fixture*/

$fixture = array();

/*Add an element to the Array * fixture*/

$fixture[] = 'Element';

/*Assert that the size of the * Array fixture is 1*/

$this->assertEquals(1, sizeof($fixture));

}

}

?>

这次ok了,基本上一个简单的testcase就是跑通了。

当然,如果测试文件比较多,这么挨个phpunit,是相当蛋疼的事情。所以,我去找到了xml配置文件的使用方法。

Tests

Tests/Freezer/HashGenerator/NonRecursiveSHA1Test.php

Tests/Freezer/IdGenerator/UUIDTest.php

Tests/Freezer/UtilTest.php

Tests/FreezerTest.php

Tests/Freezer/StorageTest.php

Tests/Freezer/Storage/CouchDB/WithLazyLoadTest.php

Tests/Freezer/Storage/CouchDB/WithoutLazyLoadTest.php

大概就是这样,可以去http://www.phpunit.de/manual/3.6/en/index.html了解更多的用法。

终端命令1phpunit --configuration test.xml --debugtest.xnl为测试配置文件

ok,第一份笔记完毕。

最后感慨下,phpunit真的是相当不错,想java用JUnit,要mock还需要Jmock,easymock;net也差不多,单元测试NUnit,mock需要Rhino Mocks;c就更要泪奔了。phpunit,不仅仅包含mock,连Code Coverage都有,不得不赞叹下啊。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值