PHP单元测试工具PHPUnit初体验

Meta ·
更新时间:2024-11-13
· 981 次阅读

     接到了个任务,需要对数字进行计算,因为涉及到整数,小数,和科学计数法等很多条件,所以人工测试非常麻烦,于是想到了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);         }

}



phpunit 测试 PHP

需要 登录 后方可回复, 如果你还没有账号请 注册新账号