ubuntu下安装和使用phpunit实例
执行 pear 命令,查看是否安装了pear
如果没有安装,则去安装
sudo apt-get install php-pear
获取phpunit:
pear config-set auto_discover 1
pear install pear.phpunit.de/PHPUnit
如果失败, 先更新
pear upgrade-all
pear install pear.phpunit.de/PHPUnit
在/tmp/pear/download 下会找到文件包 PHPUnit-3.7.10.tgz
解压到要使用的地方
创建测试文件 test.php
1 <?php 2 error_reporting(-1); 3 require(‘./PHPUnit/Autoload.php’); 4 class ArrayTest extends PHPUnit_Framework_TestCase { 5 public function testNewArrayIsEmpty(){ 6 $fixture = array(); 7 $this->assertEquals(0, count($fixture)); 8 } 9 10 public function testArrayContainsAnElement(){ 11 $fixture = array(); 12 $fixture[] = ‘Element’; 13 $this->assertEquals(1, sizeof($fixture)); 14 } 15 } 16 ?>
执行:
phpunit test.php
返回:
PHPUnit 3.7.10 by Sebastian Bergmann.
..
Time: 0 seconds, Memory: 2.75Mb
OK (2 tests, 2 assertions)
修改
$this->assertEquals(0, count($fixture));
为
$this->assertEquals(0, count($fixture));
在执行:
phpunit test.php
返回:
PHPUnit 3.7.10 by Sebastian Bergmann.
F.
Time: 1 second, Memory: 2.75Mb
There was 1 failure:
1) ArrayTest::testNewArrayIsEmpty
Failed asserting that 0 matches expected 1.
/var/www/test.php:7
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.