Centos5.4下安裝PHPUnit

來源:互聯網
上載者:User

PHPUnit之於PHP正如JUnit之於Java,是PHP程式員用來寫單元測試代碼的工具。由於上級要求員工寫代碼時也要寫單元測試代碼,於是我在網上搜集相關資料,並整理了一套符合自己虛擬機器環境的PHPUnit安裝方法。

要求

系統:centos5.4

PHP版本:php5.3.

 

一、安裝Pear

 

首先要安裝最新的pear。安裝命令如下:

 wget http://pear.php.net/go-pear.phar /usr/local/php5.3/bin/php go-pear.phar

pear 安裝成功!如果以上命令執行不成功:

 

1、用 find / -name php找到php可執行檔所在的位置;

2、可以試著在命令開始加上sudo,或者用su root切換到root使用者。

 

二、安裝phpunit

 

下面用 pear 來安裝phpunit。下面的安裝過程可能會提示沒有找到pear,那是因為pear沒有加入到環境變數中。我的解決方案是:

 

1、用 find / -name pear 找到pear所在的位置

2、輸入這條語句 export PATH="$PATH:/usr/local/php5.3/bin/pear"

 

pear channel-discover pear.phpunit.depear channel-discover components.ez.nopear channel-discover pear.symfony-project.compear upgrade-allpear install phpunit/PHPUnit  

在安裝的過程中可能會出現依賴包,按照提示資訊進行安裝。

 

如:yum install php-dom -y等

 

恭喜!如果一切順利,證明你已經安裝成功了PHPUnit。

 

三、使用PHPUnit

 

例子:hello.php

 

<?phpclass hello_test extends PHPUnit_Framework_TestCase{    public function test_equal()    {        $stack = array();        $this->assertEquals(0,123);    //判斷0和123是否相等    }}?>

phpunit hello.php 看到OK,表示成功,否則……

 

但是我這邊報錯沒有找到phpunit,當發現不是路徑問題時,找到如下解決方案


pear update-channels pear upgrade-all 
 

完了之後安裝: 

 

pear install –alldeps phpunit/PHPUnit 

 

結果報錯了:

 

Unknownremote channel: pear.symfony.com

 

phpunit/PHPUnit requires package"channel://pear.symfony.com/Yaml" (version >= 2.1.0)

 

Google一下,找到一個有用的解決方案:

 

1 pear channel-discover pear.symfony.com2 pear install pear.symfony.com/Yaml 


然後再次執行

 

pear install –alldeps phpunit/PHPUnit 

 

這回成功了

 

四、總結

 

在這裡我與大家分享了我在自己虛擬機器上安裝PHPUnit時從失敗到成功的過程。失敗主要是因為找不到可執行檔缺,或者少某些依賴庫;失敗也可能是工具與系統的版本有高有低相互不相容。

 

前面分享的例子中使用了斷言值相等的assertEquals,當然PHPUnit中還內建了不少其它的斷言方法,如assertEmpty assertTrue ……

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.