ubuntu 12.04 安装phpUnit

Ubuntu上安装

sudo apt-get install phpunit

验证PHPUnit安装成功,命令行输入phpunit

$ phpunit

安装成功打印:

PHPUnit 3.6.11 by Sebastian Bergmann.

Usage: phpunit [switches] UnitTest [UnitTest.php]

phpunit [switches] <directory>

如果出现如下的错误。

PHP Warning: require_once(PHP/CodeCoverage/Filter.php): failed to open stream: No such file or directory in /usr/bin/phpunit on line 39

PHP Fatal error: require_once(): Failed opening required 'PHP/CodeCoverage/Filter.php' (include_path='.:/usr/share/php:/usr/share/pear') in /usr/bin/phpunit on line 39

更新 chanel 和 pear,来自 Debian 和 ubuntu 社区的方法。测试可用

sudo apt-get remove phpunit
apt-get autoremove
apt-get autoclean

root@ubuntu:~# pear channel-discover pear.phpunit.de
root@ubuntu:~# pear channel-discover pear.symfony-project.com
root@ubuntu:~# pear channel-discover components.ez.no
root@ubuntu:~# pear update-channels
root@ubuntu:sudo pear upgrade-all
root@ubuntu:sudo pear install --alldeps phpunit/PHPUnit  //这个可以省略

root@ubuntu:~# sudo pear install --force --alldeps phpunit/PHPUnit

成功:
root@ubuntu:~# phpunit
    PHP Deprecated:  Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/ming.ini on l    ine 1 in Unknown on line 0
    PHPUnit 3.7.14 by Sebastian Bergmann.

 测试:

class DependencyFailureTest extends PHPUnit_Framework_TestCase
{
    public function testOne()
    {
        $this->assertTrue(FALSE);
    }
 
    /**
     * @depends testOne
     */
    public function testTwo()
    {
    }
}

结果:

root@ubuntu:~# phpunit /home/beyourself/myphp/phpinfo.php 
PHP Deprecated:  Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/ming.ini on line 1 in Unknown on line 0

PHPUnit 3.7.14 by Sebastian Bergmann.

FS

Time: 0 seconds, Memory: 2.50Mb

There was 1 failure:

1) DependencyFailureTest::testOne
Failed asserting that false is true.

/home/beyourself/myphp/phpinfo.php:24

FAILURES!
Tests: 1, Assertions: 1, Failures: 1, Skipped: 1.

 成功的测试例子:

<?php
class ExpectedErrorTest extends PHPUnit_Framework_TestCase
{
    /**
     * @expectedException PHPUnit_Framework_Error
     */
    public function testFailingInclude()
    {
        include 'not_existing_file.php';
    }
}
?>
结果:

phpunit ExpectedErrorTest
PHPUnit 3.7.0 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 5.25Mb

OK (1 test, 1 assertion)

 解释一个函数:


1.
1
class ArrayHasKeyTest extends PHPUnit_Framework_TestCase 2 { 3 public function testFailure() 4 { 5 $this->assertArrayHasKey('foo', array('ave' => 'baz')); 6 } 7 }
assertArrayHasKey($key,$array) Reports an error identified by $message if $array does not have the $key.



1
root@ubuntu:~# phpunit /home/beyourself/myphp/phpinfo.php 2 PHP Deprecated: Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/ming.ini on line 1 in Unknown on line 0 3 4 PHPUnit 3.7.14 by Sebastian Bergmann. 5 6 F 7 8 Time: 0 seconds, Memory: 2.50Mb 9 10 There was 1 failure: 11 12 1) ArrayHasKeyTest::testFailure 13 Failed asserting that an array has the key 'foo'. 14 15 /home/beyourself/myphp/phpinfo.php:36 16 17 FAILURES! 18 Tests: 1, Assertions: 1, Failures: 1.
二//
1
class ArrayHasKeyTest extends PHPUnit_Framework_TestCase 2 2 { 3 3 public function testFailure() 4 4 { 5 5 $this->assertArrayHasKey('foo', array('foo' => 'baz')); 6 6 } 7 7 }
19 root@ubuntu:~# phpunit /home/beyourself/myphp/phpinfo.php 
20 PHP Deprecated:  Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/ming.ini on line 1 in Unknown on line 0
21 
22 PHPUnit 3.7.14 by Sebastian Bergmann.
23 
24 .
25 
26 Time: 0 seconds, Memory: 2.50Mb
27 
28 OK (1 test, 1 assertion)



原文地址:https://www.cnblogs.com/zhangjun516/p/2932311.html