PHPUnit入門篇____PHP

來源:互聯網
上載者:User

PHPUnit是什麼。

它是一款輕量級的php測試架構

為什麼要用PHPUnit。

1. facebook在用

2. 可以通過命令操控測試指令碼

3. 可以測試效能

4. 可以測試程式碼涵蓋範圍

5. 可以自動化的更新測試案例的參數資料

6. 各種格式的日誌

6. 最最重要的是,功能如此炫,使用起來還特別簡單

PHPUnit的安裝

pear channel-discover pear.phpunit.depear install phpunit/PHPUnit

快速入門

<?phprequire_once 'PHPUnit/Framework.php'; class ArrayTest extends PHPUnit_Framework_TestCase{    public function testNewArrayIsEmpty()    {        // 建立數組fixture。        $fixture = array();         // 斷言數組fixture的尺寸是0。        $this->assertEquals(0, sizeof($fixture));    }}?>
1. ArrayTest為測試類別

2. ArrayTest 繼承於PHPUnit_Framework_TestCase

3.測試方法testNewArrayIsEmpty(),測試方法必須為public許可權,一般以test開頭,或者你也可以選擇給其加註釋@test來表明該函數為測試函數

/*** @test*/public function testNewArrayIsEmpty(){     $fixture = array();     $this->assertEquals(0, sizeof($fixture));}

命令列啟動測試

phpunit  測試檔案名,此處為要測試ArrayTest.php檔案

phpunit ArrayTestPHPUnit 3.2.10 by Sebastian Bergmann...Time: 0 secondsOK (2 tests)

命令列參數

phpunit --helpPHPUnit 3.2.10 by Sebastian Bergmann.Usage: phpunit [switches] UnitTest [UnitTest.php]  --log-graphviz <file>  Log test execution in GraphViz markup.  --log-json <file>      Log test execution in JSON format.  --log-tap <file>       Log test execution in TAP format to file.  --log-xml <file>       Log test execution in XML format to file.  --log-metrics <file>   Write metrics report in XML format.  --log-pmd <file>       Write violations report in PMD XML format.  --coverage-html <dir>  Generate code coverage report in HTML format.  --coverage-xml <file>  Write code coverage information in XML format.  --test-db-dsn <dsn>    DSN for the test database.  --test-db-log-rev <r>  Revision information for database logging.  --test-db-prefix ...   Prefix that should be stripped from filenames.  --test-db-log-info ... Additional information for database logging.  --testdox-html <file>  Write agile documentation in HTML format to file.  --testdox-text <file>  Write agile documentation in Text format to file.  --filter <pattern>     Filter which tests to run.  --group ...            Only runs tests from the specified group(s).  --exclude-group ...    Exclude tests from the specified group(s).  --loader <loader>      TestSuiteLoader implementation to use.  --repeat <times>       Runs the test(s) repeatedly.  --tap                  Report test execution progress in TAP format.  --testdox              Report test execution progress in TestDox format.  --no-syntax-check      Disable syntax check of test source files.  --stop-on-failure      Stop execution upon first error or failure.  --verbose              Output more verbose information.  --wait                 Waits for a keystroke after each test.  --skeleton             Generate skeleton UnitTest class for Unit in Unit.php.  --help                 Prints this usage information.  --version              Prints the version and exits.  --configuration <file> Read configuration from XML file.  -d key[=value]         Sets a php.ini value.

進階功能

你是否已經厭煩了在每一個測試方法命名前面加一個test,是否因為只是調用的參數不同,卻要寫多個測試案例而糾結。我最喜歡的進階功能,現在隆重推薦給你,叫做架構產生器

<?phpclass Calculator{    public function add($a, $b)    {        return $a + $b;    }}?>

命令列啟動測試案例

phpunit --skeleton CalculatorPHPUnit 3.2.10 by Sebastian Bergmann.Wrote test class skeleton for Calculator to CalculatorTest.php.

簡單麼。簡單,但是它其實沒有什麼意義,因為沒有測試資料,怎樣加資料,哦哦哦,重頭戲來了

<?phpclass Calculator{    /**     * @assert (0, 0) == 0     * @assert (0, 1) == 1     * @assert (1, 0) == 1     * @assert (1, 1) == 2     */    public function add($a, $b)    {        return $a + $b;    }}?>

原始類中的每個方法都進行@assert註解的檢測。這些被轉變為測試代碼,像這樣
    /**
     * Generated from @assert (0, 0) == 0.
     */
    public function testAdd() {
        $o = new Calculator;
        $this->assertEquals(0, $o->add(0, 0));
    }
下面是運行產生的測試案例類的輸出。

phpunit CalculatorTestPHPUnit 3.2.10 by Sebastian Bergmann.....Time: 0 secondsOK (4 tests)






聯繫我們

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