• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    PHP單元測試工具PHPUnit初體驗

    發布: 2007-4-22 19:26 | 作者: 未知    | 來源: ChinaUnix     | 查看: 193次 | 進入軟件測試論壇討論

    領測軟件測試網

    今天接到了個任務,需要對數字進行計算,因為涉及到整數,小數,和科學計數法等很多條件,所以人工測試非常麻煩,于是想到了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-----------
    <?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-----------
    <?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://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>