乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      張杰, PHP, Impossible is nothing: PHP單元測試工具PHPUnit初體驗(yàn)(原創(chuàng))

       Ralf_Jones 2006-06-27
      PHP單元測試工具PHPUnit初體驗(yàn)

      z33 <newbdez33 at gmail.com>
      2005.10.27

      今天接到了個(gè)任務(wù),需要對數(shù)字進(jìn)行計(jì)算,因?yàn)樯婕暗秸麛?shù),小數(shù),和科學(xué)計(jì)數(shù)法等很多條件,所以人工測試非常麻煩,于是想到了PHP的單元測試工具PHPUnit,所以寫個(gè)文檔備查。

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

      工作流程如下:
      1.設(shè)計(jì)你的class/API
      2.創(chuàng)建測試程序集
      3.實(shí)現(xiàn)class/API
      4.運(yùn)行測試
      5.修正測試失敗或錯(cuò)誤,回到第4步。

      我們來舉個(gè)例子:
      下面是你要測試的class,其中formatn函數(shù)一個(gè)取任意數(shù)字的5位有效數(shù)字的函數(shù)。
      代碼:
      ----------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
      代碼:
      ----------testcase.php-----------
      <?php

      require_once ‘format_number.php‘;
      require_once ‘PHPUnit.php‘;

      class foTest extends PHPUnit_TestCase {

         //這個(gè)成員變量是存放要測試的類引用
         var $abc;

         //構(gòu)造函數(shù)
         function foTest($name) {
            $this->PHPUnit_TestCase($name);
         }

         //new一個(gè)要測試的類為成員變量abc賦值
         function setUp() {
            $this->abc = new fo;
         }

         //unset要測試的類
         function tearDown() {
            unset($this->abc);
         }

         //自定義的testcase
         function testFormatn1() {
            //調(diào)用要測試的類的方法,結(jié)果放到$result變量
            $result = $this->abc->formatn("100.234");
            //期望結(jié)果
            $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);
         }

      }


      最后還需要一個(gè)運(yùn)行測試的程序
      代碼:
      ----------runtest.php-----------
      <?php
      require_once ‘testcase.php‘;
      require_once ‘PHPUnit.php‘;

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

      echo $result->toString();
      ?>



      現(xiàn)在就可以通過命令行運(yùn)行這個(gè)testcase
      php runtest.php

      得到結(jié)果如下:
      代碼:
      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的測試失敗,
      我們就可以去檢查一下我們的代碼在什么地方出問題了。

      補(bǔ)充一點(diǎn)
      也可以把a(bǔ)ssertTrue方法換assertEquals,如下:
      代碼:

         function testFormatn6() {
            $result = $this->abc->formatn("1343");
            $expected = "1343";
            $this->assertEquals($expected, $result);
         }


      如果失敗得到對應(yīng)的結(jié)果會(huì)直觀一些(可以顯示錯(cuò)誤的結(jié)果):
      代碼:

      TestCase foTest->testFormatn8() failed: expected 1343 , actual 134.

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多