PHPUnit 入門案例,phpunit入門案例_PHP教程

來源:互聯網
上載者:User

PHPUnit 入門案例,phpunit入門案例


瞭解PHPUnit

本案例是關於建立三角形的一個單元測試入門案例,在netbeans環境中完成,關於在此環境中搭建phpunit這裡不再描述,可以參考以下資料完成搭建工作:

http://www.cnblogs.com/x3d/p/phpunit-in-netbeans8.html

https://phpunit.de/manual/current/zh_cn/installation.html

https://github.com/sebastianbergmann/phpunit-skeleton-generator

原代碼類:

initSide($a, $b, $c);    }    /**     * 初始化三邊     * @param int $a     * @param int $b     * @param int $c     */    protected function initSide(&$a = 0, &$b = 0, &$c = 0)    {        $this->a = intval($a);        $this->b = intval($b);        $this->c = intval($c);        return $this;    }    /**     * 組建     */    public function create($a, $b, $c)    {        return $this->initSide($a, $b, $c)->verifySideIsValid();    }    /**     * 擷取類型     */    public function getType()    {        return $this->verifyType()->type;    }    /**     * 驗證三邊是否有效     * @return boolean     */    protected function verifySideIsValid()    {        if (intval($this->a) <= 0 || intval($this->b) <= 0 || intval($this->c) <= 0) {            return false;        }        if ($this->a + $this->b <= $this->c) {            return false;        }        if ($this->a + $this->c <= $this->b) {            return false;        }        if ($this->b + $this->c <= $this->a) {            return false;        }        if ($this->a - $this->b >= $this->c) {            return false;        }        if ($this->a - $this->c >= $this->b) {            return false;        }        if ($this->b - $this->c >= $this->a) {            return false;        }        return true;    }    /**     * 驗證類型     */    protected function verifyType()    {        if ($this->isEquilateral()) {            $this->type = self::TYPE_EQUILATERAL;            return $this;        }        if ($this->isIsosceles()) {            $this->type = self::TYPE_ISOSCELES;            return $this;        }        $this->type = self::TYPE_ORDINARY;        return $this;    }    /**     * 是否為等邊三角形     */    protected function isEquilateral()    {        return (($this->a == $this->b ) && ($this->b == $this->c)) ? true : false;    }    /**     * 是否為等腰三角形     */    protected function isIsosceles()    {        return (($this->a == $this->b ) || ($this->b == $this->c) || ($this->a == $this->c)) ? true : false;    }}

產生的測試類別檔案:

object = new Triangle;    }    /**     * Tears down the fixture, for example, closes a network connection.     * This method is called after a test is executed.     */    protected function tearDown()    {            }    /**     * @dataProvider addDataProvider     * @covers Triangle::create     * @todo   Implement testCreate().     */    public function testCreate($a, $b, $c)    {        // Remove the following lines when you implement this test.        /**          $this->markTestIncomplete(          'This test has not been implemented yet.'          );         *          */        /* 實現代碼 */        $this->assertTrue($this->object->create($a, $b, $c));    }    /**     * @covers Triangle::getType     * @todo   Implement testGetType().     */    public function testGetType()    {        // Remove the following lines when you implement this test.        $this->markTestIncomplete(                'This test has not been implemented yet.'        );    }    /**     * 測試案例     * @return array     */    public function addDataProvider()    {        return [            [3, 4, 5], //yes            [2, 2, 2], //yes            [8, 10, 8], //yes            [2, 3, 4], //yes            [1, 2, 3], //no            [5, 6, 7], //yes            [8, 8, 15], //yes            [0, 0, 0], //no            [-10, 2, 5], //no            [0, 2, 1], //no        ];    }}

這裡需要注意,在我們執行“建立/更新測試”後產生的測試檔案類與上面會有些不同,這裡的測試案例是手動加上去的,這裡具體實現可以查看手冊裡的說明!

附執行結果:

"/usr/bin/php" "/usr/local/bin/phpunit" "--colors" "--log-junit" "/tmp/nb-phpunit-log.xml" "--bootstrap" "/var/www/html/phpunit/test/bootstrap.php" "/usr/local/netbeans-8.1/php/phpunit/NetBeansSuite.php" "--" "--run=/var/www/html/phpunit/test/core/triangleTest.php"PHPUnit 5.2.10 by Sebastian Bergmann and contributors.....F..FFFI                                                       11 / 11 (100%)Time: 105 ms, Memory: 10.50MbThere were 4 failures:1) TriangleTest::testCreate with data set #4 (1, 2, 3)Failed asserting that false is true./var/www/html/phpunit/test/core/triangleTest.php:472) TriangleTest::testCreate with data set #7 (0, 0, 0)Failed asserting that false is true./var/www/html/phpunit/test/core/triangleTest.php:473) TriangleTest::testCreate with data set #8 (-10, 2, 5)Failed asserting that false is true./var/www/html/phpunit/test/core/triangleTest.php:474) TriangleTest::testCreate with data set #9 (0, 2, 1)Failed asserting that false is true./var/www/html/phpunit/test/core/triangleTest.php:47FAILURES!Tests: 11, Assertions: 10, Failures: 4, Incomplete: 1.完成。

  

http://www.bkjia.com/PHPjc/1108859.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1108859.htmlTechArticlePHPUnit 入門案例,phpunit入門案例 瞭解 PHPUnit 本案例是關於建立三角形的一個單元測試入門案例,在 netbeans 環境中完成,關於在此環境中搭...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.