Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

6.5. PHPUnit - Unit testing framework for PHP

https://phpunit.de/

6.5.1. 安裝

6.5.1.1. 使用 pear 安裝

查找 phpunit

# pear search PHPUnit
Retrieving data...0%
.Matched packages, channel pear.php.net:
=======================================
Package  Stable/(Latest) Local
PHPUnit  1.3.2 (stable)        Regression testing framework for unit tests.
PHPUnit2 2.3.6 (stable)        Regression testing framework for unit tests.			
			

安裝 PHPUnit2

過程 6.1. PHPUnit2

  1. install

    pear install PHPUnit2
    					
  2. phpunit your.php

由於 PHPUnit2 之後 pear 庫就沒有再更新過,建議採用 phar 方案

6.5.1.2. phar 安裝

手工安裝

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
phpunit --version
			

6.5.1.3. 測試安裝是否正確

測試代碼

			
# cat test.php
<?php

class Test extends PHPUnit_Framework_TestCase
{
  function test1() { $this->assertTrue( 1 + 2  == 3 ); }
  function test2() { $this->assertTrue( 1 + 1  == 2 ); }
}
?>
			
			

運行測試腳本

# phpunit test.php
PHPUnit 3.7.32 by Sebastian Bergmann.

..

Time: 40 ms, Memory: 2.50Mb

OK (2 tests, 2 assertions)