PHP单元测试工具PHPUnit初体验

2023-07-29,,

今天接到了个任务,需要对数字进行计算,因为涉及到整数,小数,和科学计数法等很多条件,所以人工测试非常麻烦,于是想到了PHP的单元测试工具PHPUnit,所以写个文档备查。
看了PHPUnit的文档之后基本有了一些了解,
http://pear.php.net/manual/en/packages.php.phpunit.intro.php
工作流程如下:
1.设计你的class/API
2.创建测试程序集
3.实现class/API
4.运行测试
5.修正测试失败或错误,回到第4步。
我们来举个例子:
下面是你要测试的class,其中formatn函数一个取任意数字的5位有效数字的函数。 CODE: ----------format_number.php-----------
class fo {
function fo() {
}
function formatn($num) {
$num = rtrim($num,"0");
$pos = strpos($num,".");
$num = str_replace(".","",$num);
$count1 = strlen($num);
$num = ltrim($num,"0");
$count2 = strlen($num);
$zeroc = $count1 - $count2;
$num = substr($num,0,6);
$num = round($num/10);
//$num = str_pad($num, 5, "0");
if ($pos !== false) {
$num = str_pad($num, (strlen($num)+$zeroc), "0", STR_PAD_LEFT);
$dotl = substr($num,0,$pos);
$dotr = substr($num,$pos);
$num = $dotl.".".$dotr;
}
return $num;
}
}接着创建TestCase,继承自PHPUnit_TestCase
CODE: ----------testcase.php-----------
require_once 'format_number.php';
require_once 'PHPUnit.php';
class foTest extends PHPUnit_TestCase {
//这个成员变量是存放要测试的类引用
var $abc;
//构造函数
function foTest($name) {
$this->;PHPUnit_TestCase($name);
}
//new一个要测试的类为成员变量abc赋值
function setUp() {
$this->;abc = new fo;
}
//unset要测试的类
function tearDown() {
unset($this->;abc);
}
//自定义的testcase
function testFormatn1() {
//调用要测试的类的方法,结果放到$result变量
$result = $this->;abc->;formatn("100.234");
//期望结果
$expected = "100.23";
//判断是否相等,这里使用assertTrue方法来判断布而值是否为true。
$this->;assertTrue($result == $expected);
}
function testFormatn2() {
$result = $this->;abc->;formatn("0.100234");
$expected = "0.10023";
$this->;assertTrue($result == $expected);
}
function testFormatn3() {
$result = $this->;abc->;formatn("0.100235");
$expected = "0.10024";
$this->;assertTrue($result == $expected);
}
function testFormatn4() {
$result = $this->;abc->;formatn("0.000100235");
$expected = "0.00010024";
$this->;assertTrue($result == $expected);
}
function testFormatn5() {
$result = $this->;abc->;formatn("0.000100232");
$expected = "0.00010023";
$this->;assertTrue($result == $expected);
}
function testFormatn6() {
$result = $this->;abc->;formatn("1343");
$expected = "1343";
$this->;assertTrue($result == $expected);
}
function testFormatn7() {
$result = $this->;abc->;formatn("1343.01");
$expected = "1343";
$this->;assertTrue($result == $expected);
}
function testFormatn8() {
$result = $this->;abc->;formatn("1343.05");
$expected = "1343.1";
$this->;assertTrue($result == $expected);
}
function testFormatn9() {
$result = $this->;abc->;formatn("0");
$expected = "0";
$this->;assertTrue($result == $expected);
}
function testFormatn10() {
$result = $this->;abc->;formatn("105.2342");
$expected = "105.23";
$this->;assertTrue($result == $expected);
}
function testFormatn11() {
$result = $this->;abc->;formatn("105.2375");
$expected = "105.24";
$this->;assertTrue($result == $expected);
}
function testFormatn12() {
$result = $this->;abc->;formatn("0.000523751");
$expected = "0.00052375";
$this->;assertTrue($result == $expected);
}
function testFormatn13() {
$result = $this->;abc->;formatn("0.000523755");
$expected = "0.00052376";
$this->;assertTrue($result == $expected);
}
}最后还需要一个运行测试的程序
CODE: ----------runtest.php-----------
require_once 'testcase.php';
require_once 'PHPUnit.php';
$suite = new PHPUnit_TestSuite("foTest");
$result = PHPUnit::run($suite);
echo $result->;toString();
?>;现在就可以通过命令行运行这个testcase
php runtest.php
得到结果如下:
CODE: TestCase foTest->;testFormatn1() passed
TestCase foTest->;testFormatn2() passed
TestCase foTest->;testFormatn3() passed
TestCase foTest->;testFormatn4() passed
TestCase foTest->;testFormatn5() passed
TestCase foTest->;testFormatn7() passed
TestCase foTest->;testFormatn8() passed
TestCase foTest->;testFormatn9() passed
TestCase foTest->;testFormatn10() passed
TestCase foTest->;testFormatn11() passed
TestCase foTest->;testFormatn12() passed
TestCase foTest->;testFormatn13() passed
TestCase foTest->;testFormatn6() failed: expected TRUE, actual FALSE其中testFormatn6的测试失败,
我们就可以去检查一下我们的代码在什么地方出问题了。
补充一点
也可以把assertTrue方法换assertEquals,如下:
CODE: function testFormatn6() {
$result = $this->;abc->;formatn("1343");
$expected = "1343";
$this->;assertEquals($expected, $result);
}如果失败得到对应的结果会直观一些(可以显示错误的结果):
CODE: TestCase foTest->;testFormatn8() failed: expected 1343 , actual 134. http://developer.51cto.com/art/200809/87882.htm

PHP单元测试工具phpunit安装(windows版)

简述:本程序是经过本人测试后才发布上来的,这样减少误差。

环境:

php5.2.9-2,

apach2.2.11.

windows xp

1 安装pear

1) 安装前的工作 安装PHP后所产生的文件夹下面有一个go-pear.bat文件

(我的php环境D:\wamp\bin\php\php5.2.9-2) 2) 执行installer 双击go-pear.bat,有问答时 用y,然后Enter,其它全部按“Enter” 3) 追加路径 打开你的php.ini文件,在 ; Paths and Directories ; (我的路径D:\wamp\bin\php\php5.2.9-2\PEAR) 下面看一下有没有"include-path=",没有就追加,有的话就加写pear include-path=".;C:\PHP;C:\PHP\smarty\libs; D:\wamp\bin\php\php5.2.9-2\PEAR" 4) 查看PHP文件夹 除了go-pear.bat文件,又多了pear.bat和PEAR_ENV.reg 5) 注册表的修改 双击PEAR_ENV.reg文件,选择“ok”,即可完成修改 6) 环境参数的自动设定 双击pear.bat文件即可 7) 确认安装成功与否 打开运行cmd,cd D:\wamp\bin\php\php5.2.9-2

输入 "pear version"

显示版本为1.7.2

输入“pear list”看安装在文件夹pear下面library是否都显示出来了

如果没有Benchmark

请加入 pear install Benchmark

2 pear升级

pear upgrade pear   (更新pear) 显示版本为1.9.4

比原来版本高吧,表示升级成功。

3 安装phpunit

pear channel-discover pear.phpunit.de pear channel-discover components.ez.no pear channel-discover pear.symfony-project.com

pear install --alldeps phpunit/PHPUnit[安装PHPUnit的所有元素] (pear install phpunit/PHPUnit只安装一部分文件,之后会报错,解决方法删掉php/pear/phpunit,再执行pear install --alldeps phpunit/PHPUnit)

测试是否安装成功:

phpunit --version 看到版本信息了吗,哈哈,恭喜,表示安装成功!

头一次使用这种东西,开始有些手忙脚乱,弄了二天了,终于有点眉目了,记录一下过程。

  以下都是在windows下进行,我的php版本是php-5.1.4-win32

  因为phpunit要通过pear安装,所以首先要安装pear

  找到php的目录下有一个go-pear.bat,双击运行,提示你安装系统级别的还是一个本地拷贝,直接回车,定制安装目录,选择默认即可,直接回车。程序会自动从网站上下载所需要的文件,过一会就提示你安装好了。

  安装好pear后,在php的目录下发现有一个pear.bat,这个是pear安装包用的程序,

  在命令行进行php目录,输入 pear install phpunit2

  即可,安装完成。

  在php目录下会生成一个phpunit.bat,这个是命令行下的测试命令。

  我们可以把他复制到我们要测试程序的目录下面。

  在命令行下输入 phpunit sampleTest

  就是对sampleTest这个case进行测试了。

  有二点需要注意的地方:

  phpunit需要pear的benchmark包,所以要安装 pear install benchmark即可。

  在windows下安装完成后还不能直接进行测试,运行测试程序时会出现 'php'不是内部或外部命令,也不是可运行的程序。的错误,经我一路跟踪,最后在PHPUnit2/Util/Fileloader.php这个文件里找到了问题所在,这个文件是载入测试文件用的,同时使用php解释器进行了语法检查,shell_exec('php -l ' . escapeshellarg($filename));,而我的php.exe并没有在系统的path中,所以出现了上述问题,一种办法是将$output到include之前的代码全部注释掉,这样就不用语法检查了,还有一种办法就是在系统path中加入php的安装目录。

三、--------------------

搞单元测试的大概步骤是:编写待测试类,编写测试用例类,编写测试类,测试。

单元测试首先就是要安装测试的类库了,用pear安装PHPUnit,window下这样操作,首先安装pear,在pear下发现phpunit的频道pear channel-discover pear.phpunit.de,然后安装之pear install phpunit/PHPUnit(使用这条指令,将不会完全安装PHPUnit的所有元素,请使用pear install --alldepsphpunit/PHPUnit),这时在php\pear会有PHPUnit.php和PHPUnit文件夹,这样就安装好了。

首先编写待测试类,这里用一个计算器作为例子,计算器类为代码一:

class calculator{
function add($p1,$p2)
{
return $p1+$p2;
}
} 编写测试用例类,这个类引入了PHPUnit.php和待测试的计算器类,然后初始化待测试类,编写断言。 require_once("c8-2.php");
require_once("PHPUnit.php"); class calculatorTest extends PHPUnit_TestCase
{
public $o;
//开始的时候初始化一个待测试类
function setUp()
{
$this->o = new calculator();
}
//最后消亡的时候清除掉这个类
function tearDown() {
unset($this->o);
}
function testadd()
{
$r = $this->o->add(1,2);
$e = 5;
//assertEquals和assertTrue基本一样,不过这个返回的参数更加详细
//这里的1+2肯定等于3,我们故意写错看下他的反应。注意这里是故意写错,实际测试时,这些结果必须是完全正确的,因为它的功能就是检测类方法是否正确。
$this->assertEquals($r,$e);
}
function testadd2()
{
$r = $this->o->add(102,106);
$e = 208;
$this->assertTrue($r == $e);
} } 最后编写测试类,不编写这个类也可以,在命令行下直接跑phpunitcalculatorTest就行。 require_once("testc8-2.php");
require_once("PHPUnit.php");
//载入测试用例
$s = new PHPUnit_TestSuite("calculatorTest");
//测试
$r = PHPUnit::run($s);
//测试结果
echo $r->toString();
//print_r($r); 跑一边这个测试类就可以了,它输出了测试用例中所有的测试结果。如果你有多个类,多个测试类的test类,那么可以编写一个AllTests套件。包含所有的待测试的测试类,然后在phpunit下统一执行就行。这个类可能是这样的: <?php
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';//这里引入了这个文件 require_once 'DemoTest.php'; //引入了两个测试类
require_once 'calculatortest.php'; class AllTests {
public static function main() {
PHPUnit_TextUI_TestRunner::run(self::suite());
} public static function suite() {
$suite = new PHPUnit_Framework_TestSuite('Zend Framework - Zend');
$suite->addTestSuite('DemoTest'); //最佳测试类
$suite->addTestSuite('calculatortest');
return $suite;
}
}

PHP单元测试工具PHPUnit初体验的相关教程结束。

《PHP单元测试工具PHPUnit初体验.doc》

下载本文的Word格式文档,以方便收藏与打印。