PHPUnit 安装和使用

来源:互联网 发布:大数据元年是哪一年 编辑:程序博客网 时间:2024/06/05 16:46

PHP环境说明:

PHP版本:5.2.6(采用Appserv2.5.9搭建)

安装目录:D:/AppServ/php5

文档根目录:D:/Appserv/www


安装

-------------------------------------------

 

1.双击运行 D:\AppServ\php5\go-pear.bat

 

CMD下会出现以下提示:全部回车即可

Are you installing a system-wide PEAR or a local copy?

(system|local) [system] :

 

1-10, 'all' or Enter to continue:

 

Would you like to alter php.ini <C:\WINDOWS\php.ini>? [Y/n] :

 

Current include path           : .;C:\php5\pear

Configured directory           : D:\AppServ\php5\pear

Currently used php.ini (guess) : C:\WINDOWS\php.ini

Press Enter to continue:

 

 

** WARNING! Old version found at D:\AppServ\php5, please remove it or be sure to

 use the new d:\appserv\php5\pear.bat command

The 'pear' command is now at your service at d:\appserv\php5\pear.bat

 

* WINDOWS ENVIRONMENT VARIABLES *

For convenience, a REG file is available under D:\AppServ\php5\PEAR_ENV.reg .

This file creates ENV variables for the current user.

Double-click this file to add it to the current user registry.

 

2.安装好之后,php5目录下有了pear.bat。运行cmd命令,D:\AppServ\php5>pear install phpunit2,将会提示

pear/PHPUnit2 can optionally use PHP extension "xdebug"

pear/PHPUnit2 requires package "pear/Benchmark"

 

安装xdebug

注意

1.xdebug.dll文件必须与php版本、TS相匹配,下载地址 http://xdebug.org/files/php_xdebug-2.2.3-5.2-vc9.dll

2.必须安装为zend_extension (extension=php_xdebug.dll这句不能写!)

在 C:/Windows/php.ini 文件中写入如下内容

[Xdebug]

zend_extension = "d:/AppServ/php5/ext/php_xdebug.dll"

xdebug.remote_enable = on

xdebug.trace_output_dir = "D:/AppServ/php5/tmp"

 

安装Benchmark:运行cmd命令

D:\AppServ\php5>pear install Benchmark

 

安装phpunit:运行cmd命令

D:\AppServ\php5>pear install phpunit2

 

测试示例

-------------------------------------------

 

1.项目文件:Trans.php

内容为:

<?php

class Trans{

    public function func1( $a ){

        return $a+2;

    }

}

?>

2.PHPUnit单元测试文件:Test.php

内容为:

<?php

require_once 'PHPUnit2\Framework\TestCase.php';

require_once 'Trans.php';

class Test extends PHPUnit2_Framework_TestCase {

    public $obj;

    protected function setUp() {

        parent::setUp ();

        $this->obj = new Trans();

    }

    protected function tearDown() {

        parent::tearDown ();

    }

    public function __construct() {}

    public function testFunc1(){

        $a = $this->obj->func1( 1 );

        $b = 3;

        $this->assertEquals( $b,$a );

    }

}

?>

3.在cmd下运行命令:D:\AppServ\php5>phpunit Test D:/AppServ/www/Test.php

会出现以下提示

Time: 0.000232

OK (1 test)

0 0
原创粉丝点击