PHPUnit筆記(一)
PHPUnit筆記(一)
最近構想自己要做的項目,涉及到web上的東西,要用php來做,因此自然就接觸到了phpunit這個東西。
開發平台:Debian
首先是安裝PHPUnit,配置環境當然是越簡單越好,我是個懶人,我認...
PHPUnit安裝
| 1 |
pear channel-discover pear.phpunit.de |
| 2 |
pear channel-discover components.ez.no |
| 3 |
pear channel-discover pear.symfony-project.com |
| 4 |
pear install phpunit/PHPUnit |
結束後,會等一陣子,一開始我心急了,看見沒啥反映,就直接ctrl+c了。事實證明我2了。
安裝需要pear,當然,擷取方法也繼續 my style。
pear安裝
| 1 |
apt-get install php5-curl php-pear |
安裝curl,pear |
| 2 |
pear upgrade-all |
更新軟體包 |
一切就緒後,可以在終端裡輸入phpunit,如果沒有反映,就準備好好找原因吧。反正我相信linux上的軟體問題,大多是依賴問題,以前嘗試玩過lfs,好傢夥,沒讓我少死腦細胞。
接著是使用phpunit。
根據baidu和google,大多使用如下:
然後在終端輸入
| 1 |
phpuint test.php |
test.php為測試代碼檔案 |
悲劇的是,找不到PHPUnit/Framework.php這個檔案,於是查看php.ini的include_path參數後,找到了phpunit目錄,發現了Framework目錄。
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瞭解更多的用法。
終端命令
| 1 |
phpunit --configuration test.xml --debug |
test.xnl為測試組態檔案 |
ok,第一份筆記完畢。
最後感慨下,phpunit真的是相當不錯,想java用JUnit,要mock還需要Jmock,easymock;net也差不多,單元測試NUnit,mock需要Rhino Mocks;c就更要淚奔了。phpunit,不僅僅包含mock,連Code Coverage都有,不得不讚歎下啊。