免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8777 | 回复: 9

[test]PHP单元测试工具PHPUnit初体验(原创) [复制链接]

论坛徽章:
0
发表于 2005-10-27 11:33 |显示全部楼层
PHP单元测试工具PHPUnit初体验

z33 <newbdez33 at gmail.com>;
PHP开发: http://newbdez33.blogspot.com/
2005.10.27

今天接到了个任务,需要对数字进行计算,因为涉及到整数,小数,和科学计数法等很多条件,所以人工测试非常麻烦,于是想到了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位有效数字的函数。
  1. ----------format_number.php-----------
  2. class fo {

  3.         function fo() {
  4.         }

  5.         function formatn($num) {
  6.                 $num = rtrim($num,"0");
  7.                 $pos = strpos($num,".");
  8.                 $num = str_replace(".","",$num);
  9.                 $count1 = strlen($num);
  10.                 $num = ltrim($num,"0");
  11.                 $count2 = strlen($num);
  12.                 $zeroc = $count1 - $count2;
  13.                 $num = substr($num,0,6);
  14.                 $num = round($num/10);
  15.                 //$num = str_pad($num, 5, "0");
  16.                 if ($pos !== false) {
  17.                         $num = str_pad($num, (strlen($num)+$zeroc), "0", STR_PAD_LEFT);
  18.                         $dotl = substr($num,0,$pos);
  19.                         $dotr = substr($num,$pos);
  20.                         $num = $dotl.".".$dotr;
  21.                 }
  22.                 return $num;
  23.         }

  24. }
复制代码



接着创建TestCase,继承自PHPUnit_TestCase
  1. ----------testcase.php-----------
  2. <?php

  3. require_once 'format_number.php';
  4. require_once 'PHPUnit.php';

  5. class foTest extends PHPUnit_TestCase {

  6.         //这个成员变量是存放要测试的类引用
  7.         var $abc;

  8.         //构造函数
  9.         function foTest($name) {
  10.                 $this->;PHPUnit_TestCase($name);
  11.         }

  12.         //new一个要测试的类为成员变量abc赋值
  13.         function setUp() {
  14.                 $this->;abc = new fo;
  15.         }

  16.         //unset要测试的类
  17.         function tearDown() {
  18.                 unset($this->;abc);
  19.         }

  20.         //自定义的testcase
  21.         function testFormatn1() {
  22.                 //调用要测试的类的方法,结果放到$result变量
  23.                 $result = $this->;abc->;formatn("100.234");
  24.                 //期望结果
  25.                 $expected = "100.23";
  26.                 //判断是否相等,这里使用assertTrue方法来判断布而值是否为true。
  27.                 $this->;assertTrue($result == $expected);
  28.         }

  29.         function testFormatn2() {
  30.                 $result = $this->;abc->;formatn("0.100234");
  31.                 $expected = "0.10023";
  32.                 $this->;assertTrue($result == $expected);
  33.         }

  34.         function testFormatn3() {
  35.                 $result = $this->;abc->;formatn("0.100235");
  36.                 $expected = "0.10024";
  37.                 $this->;assertTrue($result == $expected);
  38.         }

  39.         function testFormatn4() {
  40.                 $result = $this->;abc->;formatn("0.000100235");
  41.                 $expected = "0.00010024";
  42.                 $this->;assertTrue($result == $expected);
  43.         }

  44.         function testFormatn5() {
  45.                 $result = $this->;abc->;formatn("0.000100232");
  46.                 $expected = "0.00010023";
  47.                 $this->;assertTrue($result == $expected);
  48.         }

  49.         function testFormatn6() {
  50.                 $result = $this->;abc->;formatn("1343");
  51.                 $expected = "1343";
  52.                 $this->;assertTrue($result == $expected);
  53.         }

  54.         function testFormatn7() {
  55.                 $result = $this->;abc->;formatn("1343.01");
  56.                 $expected = "1343";
  57.                 $this->;assertTrue($result == $expected);
  58.         }

  59.         function testFormatn8() {
  60.                 $result = $this->;abc->;formatn("1343.05");
  61.                 $expected = "1343.1";
  62.                 $this->;assertTrue($result == $expected);
  63.         }

  64.         function testFormatn9() {
  65.                 $result = $this->;abc->;formatn("0");
  66.                 $expected = "0";
  67.                 $this->;assertTrue($result == $expected);
  68.         }

  69.         function testFormatn10() {
  70.                 $result = $this->;abc->;formatn("105.2342");
  71.                 $expected = "105.23";
  72.                 $this->;assertTrue($result == $expected);
  73.         }

  74.         function testFormatn11() {
  75.                 $result = $this->;abc->;formatn("105.2375");
  76.                 $expected = "105.24";
  77.                 $this->;assertTrue($result == $expected);
  78.         }

  79.         function testFormatn12() {
  80.                 $result = $this->;abc->;formatn("0.000523751");
  81.                 $expected = "0.00052375";
  82.                 $this->;assertTrue($result == $expected);
  83.         }

  84.         function testFormatn13() {
  85.                 $result = $this->;abc->;formatn("0.000523755");
  86.                 $expected = "0.00052376";
  87.                 $this->;assertTrue($result == $expected);
  88.         }

  89. }
复制代码


最后还需要一个运行测试的程序
  1. ----------runtest.php-----------
  2. <?php
  3. require_once 'testcase.php';
  4. require_once 'PHPUnit.php';

  5. $suite = new PHPUnit_TestSuite("foTest");
  6. $result = PHPUnit::run($suite);

  7. echo $result->;toString();
  8. ?>;
复制代码



现在就可以通过命令行运行这个testcase
php runtest.php

得到结果如下:
  1. TestCase foTest->;testFormatn1() passed
  2. TestCase foTest->;testFormatn2() passed
  3. TestCase foTest->;testFormatn3() passed
  4. TestCase foTest->;testFormatn4() passed
  5. TestCase foTest->;testFormatn5() passed
  6. TestCase foTest->;testFormatn7() passed
  7. TestCase foTest->;testFormatn8() passed
  8. TestCase foTest->;testFormatn9() passed
  9. TestCase foTest->;testFormatn10() passed
  10. TestCase foTest->;testFormatn11() passed
  11. TestCase foTest->;testFormatn12() passed
  12. TestCase foTest->;testFormatn13() passed
  13. TestCase foTest->;testFormatn6() failed: expected TRUE, actual FALSE
复制代码


其中testFormatn6的测试失败,
我们就可以去检查一下我们的代码在什么地方出问题了。


补充一点
也可以把assertTrue方法换assertEquals,如下:

  1.         function testFormatn6() {
  2.                 $result = $this->;abc->;formatn("1343");
  3.                 $expected = "1343";
  4.                 $this->;assertEquals($expected, $result);
  5.         }
复制代码


如果失败得到对应的结果会直观一些(可以显示错误的结果):

  1. TestCase foTest->;testFormatn8() failed: expected 1343 , actual 134.
复制代码

[ 本帖最后由 z33 于 2009-6-13 17:37 编辑 ]

论坛徽章:
0
发表于 2005-11-04 19:43 |显示全部楼层
晕,,一堆乱码。。看不懂。希望论坛管理员们把这问题解决了。。。

论坛徽章:
0
发表于 2005-11-04 22:38 |显示全部楼层
呆.............................

论坛徽章:
0
发表于 2005-11-04 23:48 |显示全部楼层
顶。。。

论坛徽章:
0
发表于 2005-11-05 11:39 |显示全部楼层
晕,一时半会看不懂的说,不过先收藏,顺便顶一下

论坛徽章:
0
发表于 2005-11-05 13:47 |显示全部楼层

论坛徽章:
0
发表于 2005-11-08 09:26 |显示全部楼层
原帖由 z33 于 2005-11-5 13:47 发表
http://spaces.msn.com/members/ne ... rAHSyXIiQ!234.entry

这儿没乱码了


谢谢了..

论坛徽章:
0
发表于 2005-12-01 12:23 |显示全部楼层
lz的签名档会骗人!

论坛徽章:
0
发表于 2005-12-12 10:30 |显示全部楼层

既然认识

顶~!~!

论坛徽章:
0
发表于 2005-12-12 16:18 |显示全部楼层
pear里面的好东西真多。只是都不会用~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP