PHP 單元測試(PHPUnit)(2)

來源:互聯網
上載者:User

    3.3 樣本

    檔案 Demo.php:

    <?php
    class Demo
    {
      public function sum($a, $b)
      {
        return $a + $b;
      }

      public function subtract($a, $b)
      {
        return $a - $b;
      }
    }
    ?>

    測試案例:檔案 DemoTest.php:

    <?php
    require_once('PHPUnit/Framework.php');
    require_once(dirname(__FILE__). '/Demo.php');

    class DemoTest extends PHPUnit_Framework_TestCase
    {
      public function testSum()
      {
        $demo = new Demo();
        $this->assertEquals(4, $demo->sum(2, 2));
        $this->assertNotEquals(3, $demo->sum(1, 1));
      }
    }
    ?>

    測試結果:

    PHPUnit 3.4.14 by Sebastian Bergmann.
    .
    Time: 0 seconds, Memory: 3.75Mb
    OK (1 test, 3 assertions)

    3.4 樣本

    檔案 Foo.php:

    <?php
      class Foo
      {
        function foo()
        {
        }

        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;
        }
      }
    ?>

    測試案例:檔案 FooTest.php:

    <?php
    require_once('PHPUnit/Framework.php');
    require_once(dirname(__FILE__). '/Foo.php');

    class fooTest extends PHPUnit_Framework_TestCase
    {
      //這個成員變數是存放要測試的類引用
      private $obj;

      //建構函式
      function fooTest($name)
      {
      }

      //new一個要測試的類為成員變數obj賦值
      function setUp()
      {
        $this->obj = new Foo;
      }

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

      //自訂的testcase
      function testformatn1()
      {
        //調用要測試的類的方法,結果放到$result變數
        $result = $this->obj->formatn("100.234");
        //期望結果
        $expected = "100.23";
        //判斷是否相等,這裡使用assertTrue方法來判斷布而值是否為true。
        $this->assertTrue($result == $expected);
      }

      function testformatn2()
      {
        $result = $this->obj->formatn("0.100234");
        $expected = "0.10023";
        $this->assertTrue($result == $expected);
      }

      function testformatn3()
      {
        $result = $this->obj->formatn("0.100235");
        $expected = "0.10024";
        $this->assertTrue($result == $expected);
      }

      function testformatn4()
      {
        $result = $this->obj->formatn("0.000100235");
        $expected = "0.00010024";
        $this->assertTrue($result == $expected);
      }

      function testformatn5()
      {
        $result = $this->obj->formatn("0.000100232");
        $expected = "0.00010023";
        $this->assertTrue($result == $expected);
      }

      function testformatn6()
      {
        $result = $this->obj->formatn("1343");
        $expected = "1343";
        $this->assertTrue($result == $expected);
      }

      function testformatn7()
      {
        $result = $this->obj->formatn("1343.01");
        $expected = "1343";
        $this->assertTrue($result == $expected);
      }

      function testformatn8()
      {
        $result = $this->obj->formatn("1343.05");
        $expected = "1343.1";
        $this->assertTrue($result == $expected);
      }

      function testformatn9()
      {
        $result = $this->obj->formatn("0");
        $expected = "0";
        $this->assertTrue($result == $expected);
      }

      function testformatn10()
      {
        $result = $this->obj->formatn("105.2342");
        $expected = "105.23";
        $this->assertTrue($result == $expected);
      }

      function testformatn11()
      {
        $result = $this->obj->formatn("105.2375");
        $expected = "105.24";
        $this->assertTrue($result == $expected);
      }

      function testformatn12()
      {
        $result = $this->obj->formatn("0.000523751");
        $expected = "0.00052375";
        $this->assertTrue($result == $expected);
      }

      function testformatn13()
      {
        $result = $this->obj->formatn("0.000523755");
        $expected = "0.00052376";
        $this->assertTrue($result == $expected);
      }
    }
    ?>

    測試結果:

    PHPUnit 3.4.14 by Sebastian Bergmann.
    .....F.......
    Time: 0 seconds, Memory: 4.00Mb
    There was 1 failure:

    1) fooTest::testformatn6
    Failed asserting that <boolean:false> is true.

    C:/Program Files/Zend/Apache2/htdocs/unit_test-2/FooTest.php:70

    FAILURES!
    Tests: 13, Assertions: 13, Failures: 1.

    3.5 測試套件樣本:

    檔案 AllTests.php:

    <?php
    require_once 'PHPUnit/Framework.php';
    require_once 'PHPUnit/TextUI/TestRunner.php';
    require_once 'DemoTest.php';
    require_once 'FooTest.php';

    class AllTests
    {
      public static function suite()
      {
        $suite = new PHPUnit_Framework_TestSuite('Zend Framework - Zend');
        $suite->addTestSuite('DemoTest');
        $suite->addTestSuite('FooTest');
        return $suite;
      }

      public static function main()
      {
        PHPUnit_TextUI_TestRunner::run(self::suite());
      }
    }
    ?>

    測試結果:

    PHPUnit 3.4.14 by Sebastian Bergmann.
    ......F.......
    Time: 0 seconds, Memory: 4.00Mb
    There was 1 failure:

    1) fooTest::testformatn6
    Failed asserting that <boolean:false> is true.

    C:/Program Files/Zend/Apache2/htdocs/unit_test-2/FooTest.php:70

    FAILURES!
    Tests: 14, Assertions: 16, Failures: 1.

    ======================================================================
    相關連結:
    PHPUnit官方:http://www.phpunit.de/
    PHPUnit文檔:http://www.phpunit.de/pocket_guide/3.2/en/index.html

    作者:張慶(網眼) 西安 PHP 教育培訓中心 2010-7-4
    來自“網眼視界”:http://blog.why100000.com
    作者微博:http://t.qq.com/zhangking
    “十萬個為什麼”電腦學習網:http://www.why100000.com

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.