什么是phpunit(来自百度百科)
PHPUnit是一个轻量级的PHP测试框架。它是在PHP5下面对JUnit3系列版本的完整移植,是xUnit测试框架家族的一员(它们都基于模式先锋Kent Beck的设计)。
单元测试是几个现代敏捷开发方法的基础,使得PHPUnit成为许多大型PHP项目的关键工具。这个工具也可以被Xdebug扩展用来生成代码覆盖率报告 ,并且可以与phing集成来自动测试,最后它还可以和Selenium整合来完成大型的自动化集成测试。
安装部署有两种方式
方法一: 使用Pear
运行 pear channel-discover pear.phpunit.de; pear install phpunit/PHPUnit
方法二 :手动安装 1 从http://pear.phpunit.de/get/下载软件包并解压 2 把解压后的目录加入php.ini中的include_path 3 将脚本pear-phpunit改名为phpunit 4 将phpunit脚本中的@php_bin@替换成php可执行脚本的路径 5 为phpunit脚本增加可执行权限并加入$PATH 6 将PHPUnit/Runner/Version.php中的@package_version@替换成3位 PHPUnit版本号
这里我们使用的Pear方式进行安装。以windows下环境为例。
在建立一个完整的php工作开发环境后,phpunit的的部署首先需要xdebug与pear的支持,才能安装phpunit。
1、安装xdebug
[Xdebug]
extension=php_xdebug.dll
xdebug.profiler_enable=on
xdebug.trace_output_dir="E:/php/xdebug"
xdebug.profiler_output_dir="E:/php/xdebug"
在安装xdebug后,重启apache,在phpinfo中即可看到了xdebug的信息。
接下来cmd进入php的安装目录,如E:/wamp/php/
输入pear,会提示“pear不是内部或外部的命令,也不是可运行的程序...”,这说明系统还没有安装pear,如果出现如下的命令方式
则说明pear安装成功了,可以直接去安装phpunit了。
在这里需要注意的是,xdebug的的加载方式必须以zend的方式进行加载;而extension是以php的加载方式进行实现的
所以在最后安装成功phpunit后,如果xdebug还是以php方式进行加载的话,系统会提示如下信息
zend_extension 加载是以zend引擎的方式进行模块加载的,详细信息如下
{
根据 PHP 版本,zend_extension 指令可以是以下之一:
zend_extension (non ZTS, non debug build)
zend_extension_ts ( ZTS, non debug build)
zend_extension_debug (non ZTS, debug build)
zend_extension_debug_ts ( ZTS, debug build)
ZTS:ZEND Thread Safety
可通过phpinfo()查看ZTS是否启用,从而决定用zend_extension还是zend_extension_ts。既查看phpinfo综合信息处的状态
extension意为基于php引擎的扩展;zend_extension意为基于zend引擎的扩展
注:php是基于zend引擎的
不同的扩展安装后,在php.ini里是用extension还是zend_extension,是取决于该扩展,有的扩展可能只能用zend_extension,如xdebug,也有的扩展可以用extension或zend_extension,如mmcache。
注:上面的结论不保证准确。
zend_extension加载php扩展时需用全路径,而extension加载时可以用相对extension_dir的路径。
}
这里我的 Thread Safety是启用状态,即线程安全版本,所以使用zend_extension_ts来加载。且xdebug必须使用全路径,这点很重要;不然添加到php.ini后,重启apache不会报错,但xdebug信息也是没有的。
2、安装pear
还是在php安装目录中,cmd输入如下命令
go-pear
连续输入两次yes,在直接按Enter。然后就自动从服务器上下载pear安装包,进行自动安装了。
再次输入pear命令,则会出现commands列表了。说pear安装成功了。
3、安装phpunit
继续cmd命令输入
pear channel-discover pear.phpunit.de
pear channel-discover pear.symfony-project.com
pear install phpunit/PHPUnit
这时可能会提示缺少依赖包内容,如下图
我们可以使用
pear install Image_GraphViz
pear install log
pear install YAML
进行依赖包得依次安装。安装完成后,继续输入
pear install phpunit/PHPUnit
耐心等待,可能有点慢,直到下载安装成功。
至此,在cmd中键入phpunit --version,提示phpunit版本信息则表示安装成功可以进入单元测试阶段了。
4、结合netbeans 使用phpunit进行单元测试
主要有两步,一、对某个类或者整个项目创建phpunit单元测试;二、单击源文件或项目进行单元测试。详细进行请进入使用 PHPUnit 和 Selenium 进行测试阅读。
其中对需要测试的类添加@assert 标注需要注意,点击这里详细了解,在netbeans中可以直接“创建phpunit单元测试”生成测试类;
<?php class Calculator { /** * @assert (0, 0) == 0 * @assert (0, 1) == 1 * @assert (1, 0) == 1 * @assert (1, 1) == 2 * @assert (1, 2) == 4 */ public function add($a, $b) { return $a + $b; } // 添加用来测试“代码覆盖率”的 public function addnow($a, $b) { return $a + $b; } } ?>
同时也可以使用命令来完成
phpunit --skeleton-test Calculator
注意,Calculator是类名,不是文件名。
会在同目录下自动生成Calculator的测试类,CalculatorTest.php;
CalculatorTest.php类详细内容
<?php require_once 'PHPUnit/Framework.php'; require_once 'D:\www\phpunit\Calculator.php'; /** * Test class for Calculator. * Generated by PHPUnit on 2012-09-25 at 10:17:34. */ class CalculatorTest extends PHPUnit_Framework_TestCase { /** * @var Calculator */ protected $object; /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protected function setUp() { $this->object = new Calculator; } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protected function tearDown() { } /** * Generated from @assert (0, 0) == 0. */ public function testAdd() { $this->assertEquals( 0, $this->object->add(0, 0) ); } /** * Generated from @assert (0, 1) == 1. */ public function testAdd2() { $this->assertEquals( 1, $this->object->add(0, 1) ); } /** * Generated from @assert (1, 0) == 1. */ public function testAdd3() { $this->assertEquals( 1, $this->object->add(1, 0) ); } /** * Generated from @assert (1, 1) == 2. */ public function testAdd4() { $this->assertEquals( 2, $this->object->add(1, 1) ); } /** * Generated from @assert (1, 2) == 4. */ public function testAdd5() { $this->assertEquals( 4, $this->object->add(1, 2) ); } /** * @todo Implement testAddnow(). */ public function testAddnow() { // Remove the following lines when you implement this test. $this->markTestIncomplete( 'This test has not been implemented yet.' ); } } ?>
这时就可以测试这个测试类了。
phpunit CalculatorTest.php unittest
结果信息如下
点号表示测试通过,没有错误;
F表示其中一个测试结果有误。错误信息直观展示了。
I表示测试没有实现执行
对比netbeans的执行更加抽象一点,如下为netbeans执行的测试结果。
这个只是一个简单的示例,更详细的服务于项目级别的对于每一个独立的模块都需要写新的独立测试类来进行测试。
phpunit安装成功后,会在c盘建立一个php5文件夹。其中可以找到PHPUnit的接测试例子
C:\php5\pear\docs\PHPUnit\PHPUnit\Samples
(说实话money例子挺复杂的)很容易绕晕了。
参考信息:
PHP Warning: Xdebug MUST be loaded as a Zend extension in Unknown on line 0 解决办法
PHPUnit安装 部分配图来源此博客,鸣谢
PHPUnit是一个轻量级的PHP测试框架。它是在PHP5下面对JUnit3系列版本的完整移植,是xUnit测试框架家族的一员(它们都基于模式先锋Kent Beck的设计)。
单元测试是几个现代敏捷开发方法的基础,使得PHPUnit成为许多大型PHP项目的关键工具。这个工具也可以被Xdebug扩展用来生成代码覆盖率报告 ,并且可以与phing集成来自动测试,最后它还可以和Selenium整合来完成大型的自动化集成测试。