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 社区的方法。测试可用

  1. 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

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

 

 

 测试:

  1. class DependencyFailureTest extends PHPUnit_Framework_TestCase
  2. {
  3. public function testOne()
  4. {
  5. $this->assertTrue(FALSE);
  6. }
  7. /**
  8. * @depends testOne
  9. */
  10. public function testTwo()
  11. {
  12. }
  13. }

结果:

  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. PHPUnit 3.7.14 by Sebastian Bergmann.
  4. FS
  5. Time: 0 seconds, Memory: 2.50Mb
  6. There was 1 failure:
  7.  
  8. 1) DependencyFailureTest::testOne
  9. Failed asserting that false is true.
  10.  
  11. /home/beyourself/myphp/phpinfo.php:24
  12. FAILURES!
  13. Tests: 1, Assertions: 1, Failures: 1, Skipped: 1.

 

 成功的测试例子:

  1. <?php
  2. class ExpectedErrorTest extends PHPUnit_Framework_TestCase
  3. {
  4. /**
  5. * @expectedException PHPUnit_Framework_Error
  6. */
  7. public function testFailingInclude()
  8. {
  9. include \'not_existing_file.php\';
  10. }
  11. }
  12. ?>
  13. 结果:
  14. phpunit ExpectedErrorTest
  15. PHPUnit 3.7.0 by Sebastian Bergmann.
  16.  
  17. .
  18.  
  19. Time: 0 seconds, Memory: 5.25Mb
  20. OK (1 test, 1 assertion)

 解释一个函数:

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

 


  1. 1
    root@ubuntu:~# phpunit /home/beyourself/myphp/phpinfo.php
  2. 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. 3
  4. 4 PHPUnit 3.7.14 by Sebastian Bergmann.
  5. 5
  6. 6 F
  7. 7
  8. 8 Time: 0 seconds, Memory: 2.50Mb
  9. 9
  10. 10 There was 1 failure:
  11. 11
  12. 12 1) ArrayHasKeyTest::testFailure
  13. 13 Failed asserting that an array has the key \'foo\'.
  14. 14
  15. 15 /home/beyourself/myphp/phpinfo.php:36
  16. 16
  17. 17 FAILURES!
  18. 18 Tests: 1, Assertions: 1, Failures: 1.
  1. //
    1
    class ArrayHasKeyTest extends PHPUnit_Framework_TestCase
  2. 2 2 {
  3. 3 3 public function testFailure()
  4. 4 4 {
  5. 5 5 $this->assertArrayHasKey(\'foo\', array(\'foo\' => \'baz\'));
  6. 6 6 }
  7. 7 7 }

 

  1. 19 root@ubuntu:~# phpunit /home/beyourself/myphp/phpinfo.php
  2. 20 PHP Deprecated: Comments starting with \'#\' are deprecated in /etc/php5/cli/conf.d/ming.ini on line 1 in Unknown on line 0
  3. 21
  4. 22 PHPUnit 3.7.14 by Sebastian Bergmann.
  5. 23
  6. 24 .
  7. 25
  8. 26 Time: 0 seconds, Memory: 2.50Mb
  9. 27
  10. 28 OK (1 test, 1 assertion)

 

版权声明:本文为zhangjun516原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zhangjun516/archive/2013/02/25/2932311.html