PHPUNIT 單元測試

來源:互聯網
上載者:User

標籤:

在windows上的安裝可以參考其手冊

首先下載phpunit.phar檔案

1. 為php的二進位可執行檔建立 一個目錄,如C:\bin

2. 將C:\bin添加到系統內容變數中,

3. 開啟命令列cmd

4. 建立批處理指令碼,C:\bin\phpunit.cmd

    

cd C:\binecho @php "%~dp0phpunit.phar" %* > phpunit.cmdexit

在命令提示字元後執行以上命令後,會在C:\bin目錄下產生 phpunit.cmd檔案

5. 開啟一個新視窗後,確認一下PHPUNIT在任意路徑下可以執行

  

C:\Users\username> phpunit --versionPHPUnit x.y.z by Sebastian Bergmann.

有此提示後說明PHPUNIT安裝成功。

 

二、編寫PHPUNIT測試案例

  用PHPUNIT測試數組操作

  1.針對類 Class 的測試寫在類 ClassTest 中。
  2.ClassTest(通常)繼承自 PHPUnit_Framework_TestCase。
  3.
  4.在測試方法內,類似於 assertEquals()(參見附錄 A, 斷言)這樣的斷言方法用來際值與預期值的匹配做出斷言。

例 2.1. 用 PHPUnit 測試數組操作

<?phpclass StackTest extends PHPUnit_Framework_TestCase{    public function testPushAndPop()    {        $stack = array();        $this->assertEquals(0, count($stack));        array_push($stack, ‘foo‘);        $this->assertEquals(‘foo‘, $stack[count($stack)-1]);        $this->assertEquals(1, count($stack));        $this->assertEquals(‘foo‘, array_pop($stack));        $this->assertEquals(0, count($stack));    }}?>

例子中我使用斷言的方法assertEquals來斷言我期待$stack相關的值

注意代碼中高亮的方法indexEquals,它並沒有使用test開頭,但是我在注釋中增加了@test標籤,那麼phpunit依然會將其作為一個測試方法運行,下面我們來運行方法和運行結果

 

進入命令列,使用phpunit StackTest來執行StackTest.php的測試,運行結果如下:

。。。。

詳細的使用可以參考官方手冊。

 

PHPUNIT 單元測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.