亚洲好骚综合-亚洲黄色录像-亚洲黄色网址-亚洲黄色网址大全-99久久99久久-99久久99久久精品国产

您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > PHPUnit
PHP單元測試工具PHPUnit初體驗
作者:網絡轉載 發(fā)布時間:[ 2013/3/11 17:29:12 ] 推薦標簽:

     接到了個任務,需要對數字進行計算,因為涉及到整數,小數,和科學計數法等很多條件,所以人工測試非常麻煩,于是想到了PHP的單元測試工具PHPUnit,所以寫個文檔備查。

看了PHPUnit的文檔之后基本有了一些了解,
http://pear.php.net/manual/en/packages.php.phpunit.intro.php

工作流程如下:
1.設計你的class/API
2.創(chuàng)建測試程序集
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;
        }

}
接著創(chuàng)建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);
        }

}

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd
主站蜘蛛池模板: 张柏芝国产一区在线观看 | 国产看片一区二区三区 | 国产黄色片在线看 | 九九99视频在线观看视频观看 | 一区二区日本视频 | 亚洲一区二区三区精品国产 | 亚洲黄色在线视频 | 免费看欧美日韩一区二区三区 | 亚洲毛片视频 | 无遮挡动漫肉在线观看高清 | 国产成人在线免费视频 | 激情综合欧美 | 五月天在线观看免费视频播放 | 日韩美女片视频 | 大陆黄色一级片 | 久久网站免费 | 久久精品亚洲精品国产欧美 | 最近中文字幕经典版在线 | 成人资源在线 | 日本欧美在线视频 | 777久久| 日韩欧美高清视频 | 欧美大片va欧美在线播放 | 免费日韩在线视频 | 欧美日韩在线免费观看 | 伊人中文网 | 99ri国产在线观看 | 中文字幕一区二区三区5566 | 女女同性一区二区三区四区 | 草草视频在线 | 国产成人午夜性a一级毛片 国产成人午夜视频 | 九九九九精品视频在线播放 | www.黄色网.com | 视频一区 欧美 | 在线欧美不卡 | 午夜黄色福利 | 国产一区二区在线看 | 国产精品1024在线永久免费 | 二区三区不卡不卡视频 | 国产草比 | yy6080理aa级伦大片一级 |