您的位置:首页 > 编程语言 > PHP开发

ubuntu 12.04 安装phpUnit

2013-02-25 16:43 399 查看
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
{
public function testFailure()
{
$this->assertArrayHasKey('foo', array('ave' => 'baz'));
}
}

assertArrayHasKey($key,$array) Reports an error identified by [code]$message
if
$array
does not have the
$key
.[/code]



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

F

Time: 0 seconds, Memory: 2.50Mb

There was 1 failure:

1) ArrayHasKeyTest::testFailure
Failed asserting that an array has the key 'foo'.

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

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


二//
1 class ArrayHasKeyTest extends PHPUnit_Framework_TestCase
2 {
3     public function testFailure()
4     {
5         $this->assertArrayHasKey('foo', array('foo' => 'baz'));
6     }
7 }


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.

.

Time: 0 seconds, Memory: 2.50Mb

OK (1 test, 1 assertion)




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: