技术开发 频道

PHP单元测试工具PHPUnit初体验

【IT168 技术文章】

   今天接到了个任务,需要对数字进行计算,因为涉及到整数,小数,和科学计数法等很多条件,所以人工测试非常麻烦,于是想到了PHP的单元测试工具PHPUnit,所以写个文档备查。

    工作流程如下:
    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-----------
<?php

require_once 'format_number.php';
require_once 'PHPUnit.php';

class foTest extends PHPUnit_TestCase &#123;

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

        //构造函数
        function foTest&#40;$name&#41; &#123;
                $this->;PHPUnit_TestCase&#40;$name&#41;;
        &#125;

        //new一个要测试的类为成员变量abc赋值
        function setUp&#40;&#41; &#123;
                $this->;abc = new fo;
        &#125;

        //unset要测试的类
        function tearDown&#40;&#41; &#123;
                unset&#40;$this->;abc&#41;;
        &#125;

        //自定义的testcase
        function testFormatn1&#40;&#41; &#123;
                //调用要测试的类的方法,结果放到$result变量
                $result = $this->;abc->;formatn&#40;&quot;100.234&quot;&#41;;
                //期望结果
                $expected = &quot;100.23&quot;;
                //判断是否相等,这里使用assertTrue方法来判断布而值是否为true。
                $this->;assertTrue&#40;$result == $expected&#41;;
        &#125;

        function testFormatn2&#40;&#41; &#123;
                $result = $this->;abc->;formatn&#40;&quot;0.100234&quot;&#41;;
                $expected = &quot;0.10023&quot;;
                $this->;assertTrue&#40;$result == $expected&#41;;
        &#125;

        function testFormatn3&#40;&#41; &#123;
                $result = $this->;abc->;formatn&#40;&quot;0.100235&quot;&#41;;
                $expected = &quot;0.10024&quot;;
                $this->;assertTrue&#40;$result == $expected&#41;;
        &#125;

        function testFormatn4&#40;&#41; &#123;
                $result = $this->;abc->;formatn&#40;&quot;0.000100235&quot;&#41;;
                $expected = &quot;0.00010024&quot;;
                $this->;assertTrue&#40;$result == $expected&#41;;
        &#125;

        function testFormatn5&#40;&#41; &#123;
                $result = $this->;abc->;formatn&#40;&quot;0.000100232&quot;&#41;;
                $expected = &quot;0.00010023&quot;;
                $this->;assertTrue&#40;$result == $expected&#41;;
        &#125;

        function testFormatn6&#40;&#41; &#123;
                $result = $this->;abc->;formatn&#40;&quot;1343&quot;&#41;;
                $expected = &quot;1343&quot;;
                $this->;assertTrue&#40;$result == $expected&#41;;
        &#125;

        function testFormatn7&#40;&#41; &#123;
                $result = $this->;abc->;formatn&#40;&quot;1343.01&quot;&#41;;
                $expected = &quot;1343&quot;;
                $this->;assertTrue&#40;$result == $expected&#41;;
        &#125;

        function testFormatn8&#40;&#41; &#123;
                $result = $this->;abc->;formatn&#40;&quot;1343.05&quot;&#41;;
                $expected = &quot;1343.1&quot;;
                $this->;assertTrue&#40;$result == $expected&#41;;
        &#125;

        function testFormatn9&#40;&#41; &#123;
                $result = $this->;abc->;formatn&#40;&quot;0&quot;&#41;;
                $expected = &quot;0&quot;;
                $this->;assertTrue&#40;$result == $expected&#41;;
        &#125;

        function testFormatn10&#40;&#41; &#123;
                $result = $this->;abc->;formatn&#40;&quot;105.2342&quot;&#41;;
                $expected = &quot;105.23&quot;;
                $this->;assertTrue&#40;$result == $expected&#41;;
        &#125;

        function testFormatn11&#40;&#41; &#123;
                $result = $this->;abc->;formatn&#40;&quot;105.2375&quot;&#41;;
                $expected = &quot;105.24&quot;;
                $this->;assertTrue&#40;$result == $expected&#41;;
        &#125;

        function testFormatn12&#40;&#41; &#123;
                $result = $this->;abc->;formatn&#40;&quot;0.000523751&quot;&#41;;
                $expected = &quot;0.00052375&quot;;
                $this->;assertTrue&#40;$result == $expected&#41;;
        &#125;

        function testFormatn13&#40;&#41; &#123;
                $result = $this->;abc->;formatn&#40;&quot;0.000523755&quot;&#41;;
                $expected = &quot;0.00052376&quot;;
                $this->;assertTrue&#40;$result == $expected&#41;;
        &#125;

&#125;
    最后还需要一个运行测试的程序


CODE:----------runtest.php-----------
<?php
require_once 'testcase.php';
require_once 'PHPUnit.php';

$suite = new PHPUnit_TestSuite&#40;&quot;foTest&quot;&#41;;
$result = PHPUnit&#58;&#58;run&#40;$suite&#41;;

echo $result->;toString&#40;&#41;;
?>;
现在就可以通过命令行运行这个testcase
php runtest.php

    得到结果如下:


CODE:TestCase foTest->;testFormatn1&#40;&#41; passed
TestCase foTest->;testFormatn2&#40;&#41; passed
TestCase foTest->;testFormatn3&#40;&#41; passed
TestCase foTest->;testFormatn4&#40;&#41; passed
TestCase foTest->;testFormatn5&#40;&#41; passed
TestCase foTest->;testFormatn7&#40;&#41; passed
TestCase foTest->;testFormatn8&#40;&#41; passed
TestCase foTest->;testFormatn9&#40;&#41; passed
TestCase foTest->;testFormatn10&#40;&#41; passed
TestCase foTest->;testFormatn11&#40;&#41; passed
TestCase foTest->;testFormatn12&#40;&#41; passed
TestCase foTest->;testFormatn13&#40;&#41; passed
TestCase foTest->;testFormatn6&#40;&#41; failed&#58; expected TRUE, actual FALSE

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


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


CODE:        function testFormatn6&#40;&#41; &#123;
                $result = $this->;abc->;formatn&#40;&quot;1343&quot;&#41;;
                $expected = &quot;1343&quot;;
                $this->;assertEquals&#40;$expected, $result&#41;;
        &#125;
    如果失败得到对应的结果会直观一些(可以显示错误的结果):


CODE:TestCase foTest->;testFormatn8&#40;&#41; failed&#58; expected 1343 , actual 134.

0
相关文章