介紹 Netbeans 執行 PHPUnit (單元測試)
前言
Netbeans 6.8 出來之後 , 我覺得已經非常好用了 , 尤其在程式碼的自動完成的方面已經改成和 Zend Studio 一樣 , 不需要按甚麼 ALT+J 了 , 而且 Netbeans 的反應速度就是比 Zend Studio 來的快 , 現在我也都改用 Netbeans 了 , 而本篇是稍微介紹一下如何用 Netbeans 來執行 PHP 的單元測試
PHPUnit 介紹
PHPUnit 其實是個 PHP 寫成的 Framework , 主要功能就是自動對我們所寫的PHP程式做輸入輸出的檢查
程式寫久的人應該都知道動一髮牽全身的道理 , 我自己也常常更動了某個物件的程式碼之後 , 結果很多相關連的物件或網頁就跟著就出錯了 , 但我們不可能每次更動程式之後 , 就去用瀏覽器慢慢的測試可能發生的狀況 , 人的記憶有限 , 誰會知道這個物件有那些網頁會用到 , 也因此我們需要一個方便的工具來幫我們自動測試 , 而 PHPUnit 就可以讓我們自行撰寫測試的條件 , 當我們所寫的測試條件隨著時間累積的越多時 , 就可以避免一定程度的錯誤發生了
安裝 PHPUnit
由於 PHPUnit 是純 PHP 寫的 , 所以也不需要編譯了啦 , 這裡就介紹如何用 pear 安裝 , 不管是 Windows 或 Linux 只要執行下列三行即可
pear channel-discover pear.phpunit.de pear channel-discover pear.symfony-project.com pear install phpunit/PHPUnit |
如果是 Linux 安裝 , 打一下 which phpunit 應該會顯示出 phpunit 這個執行檔的位置 , 在 windows 上若很多人用 xampp 這咚咚 , 就會發現在 xampp 目錄下有 phpunit.bat , 提這個是為了後面介紹 Netbeans 要執行 PHPUnit 用的 , 所以很重要
官方文件也有介紹手動安裝法 : http://www.phpunit.de/manual/3.4/en/installation.html
使用 Netbeans 建立 PHPUnit 單元測試
現在先假設我的 Nettbeans 下有個 test 的專案 , 其下有個有個 Class1.php 裡面就是一個 Class1 物件 , 程式碼如下
<?php class Class1 { /** * @param int $a * @param int $b * @return int */ public function add($a , $b) { return $a+$b; } } ?> |
接下來只要在 Class1.php 那邊按下滑鼠右鍵 , 就會出現選單 , 我們要選"工具" 然後是 "Create PHPUnit tests" , 如下圖

當我們第一次建立 PHPUnit 的時候 , Netbeans 會跳出對話框問你測試的程式碼目錄要放那裏 , 我們可以任意去指定一個目錄 , 反正不能和我們的專案目錄的路徑一樣就是了 , 如果跳出下圖的對話視窗 , 代表尚未設定 phpunit 執行檔路徑 , 這個路徑就是前面第二段安裝 PHPUnit 提到的路徑了

都設定好之後會發現左邊的專案視窗多了個 Test Files 目錄 , 並且有個 Class1Test.php
如下圖

這個 Class1Test.php 的內容是預設的 , 沒有甚麼測試條件 , 我們要自己手動加一些程式碼 , 例如我修改了預設程式碼 , 主要是測試 1+1 一定要等於 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php require_once 'PHPUnit/Framework.php'; require_once dirname(__FILE__).'/../Class1.php'; /** * Test class for Class1. * Generated by PHPUnit on 2010-02-20 at 22:36:43. */ class Class1Test extends PHPUnit_Framework_TestCase { /** * @var Class1 */ protected $object; /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protected function setUp() { $this->object = new Class1; } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protected function tearDown() { } /** * @todo Implement testAdd(). */ public function testAdd() { $this->assertEquals( 2, $this->object->add(1, 1) ); } } ?> |
然後我在左邊專案視窗中的 test 那邊按滑鼠右鍵 , 選 Test (如下圖) , 就會開始自動測試結果

測試結果會輸出在 Netbeans 下方 , 如下圖所示是正確的

如果我修改測試條件呢 ? 假設改一下程式碼如下
<?php require_once 'PHPUnit/Framework.php'; require_once dirname(__FILE__).'/../Class1.php'; /** * Test class for Class1. * Generated by PHPUnit on 2010-02-20 at 22:36:43. */ class Class1Test extends PHPUnit_Framework_TestCase { /** * @var Class1 */ protected $object; /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protected function setUp() { $this->object = new Class1; } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protected function tearDown() { } /** * @todo Implement testAdd(). */ public function testAdd() { // Remove the following lines when you implement this test. $this->assertEquals( 2, $this->object->add(2, 1) ); } } ?> |
然後得到的結果就如下是紅字的錯誤了

應該不難吧 , 比較難的應該是要去學 PHPUnit 這套 Framework , 以下是相關的網址
PHPUnit 官方網站 : http://www.phpunit.de/
PHPUnit 說明文件 : http://www.phpunit.de/manual/3.4/en/index.html
另外 , PHPUnit 3 之後有支援 selenium 功能 , 可以模擬瀏覽器的動作 , 有玩 YII 的人可以看下面網址人家怎麼玩的
http://www.yiiframework.com/forum/index.php?/topic/4819-netbeans-68-selenium-and-yii/
近期迴響