網頁

2014年1月15日 星期三

Wordpress開發筆記-執行WordPress的Unit test的第一步

想寫WordPress plugin的unit test,還是先看看他們的範例好了,最好的當然是Wordpress開發時的Test case。查了下發現這篇Automated Testing照著做。

Step1: 先安裝好PHPUnit

可參考這篇PHPUnit學習筆記-PHPUnit安裝(使用PEAR)

Step2: 下載Wordpress正在測試的svn repository

下載的link: http://develop.svn.wordpress.org/trunk/
在Mac上svn是安裝好的,直接底下的指令即可
$cd (你要放置的目錄位置)
$svn co http://develop.svn.wordpress.org/trunk/ wordpress-develop
$cd wordpress-develop

Step3: 建個資料庫讓測試程式使用

這部分我用phpMyAdmin,隨便建個資料庫及它的名字,字集比對用utf8_general_ci

Step4: 修改wp-test-config.php

$cp wp-tests-config-sample.php wp-tests-config.php
$vim wp-tests-config.php

修改底下的資料
define( 'DB_NAME', '剛建好的資料庫' );
define( 'DB_USER', '使用者號' );
define( 'DB_PASSWORD', '密碼' );
define( 'DB_HOST', 'localhost' );
define( 'DB_CHARSET', 'utf8' );
define( 'DB_COLLATE', 'utf8_general_ci' );

Step5: 執行PHPUnit Test cases啦~!

$phpunit
執行結果如下:

Installing...

Running as single site... To run multisite, use -c tests/phpunit/multisite.xml

Not running ajax tests... To execute these, use --group ajax.

PHPUnit 3.7.28 by Sebastian Bergmann.

Configuration read from /wordpress-develop/phpunit.xml.dist

.............................................................   61 / 1907 (  3%)
............SS...SSSSSS.............S...SS............SS.....  122 / 1907 (  6%)
...........S............S....................................  183 / 1907 (  9%)
........SSS.......SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS..S.......  244 / 1907 ( 12%)
.................S............................S.............S  305 / 1907 ( 15%)
...........................SSSSSSSSSS........................  366 / 1907 ( 19%)
..........S..................S.......I.......................  427 / 1907 ( 22%)
.................................SSSS.S..SSSSS...............  488 / 1907 ( 25%)
.............................................................  549 / 1907 ( 28%)
.............................................................  610 / 1907 ( 31%)
.............................................................  671 / 1907 ( 35%)
.............................................................  732 / 1907 ( 38%)
.............................................................  793 / 1907 ( 41%)
...........S.................................................  854 / 1907 ( 44%)
.............................................................  915 / 1907 ( 47%)
...................................SSSSSSSSSSSS..............  976 / 1907 ( 51%)
...............SSSSSS.........S.....S........................ 1037 / 1907 ( 54%)
................................S............................ 1098 / 1907 ( 57%)
.....................S................SSS.......S....SSSS.... 1159 / 1907 ( 60%)
.SS.......................................................... 1220 / 1907 ( 63%)
............................................................. 1281 / 1907 ( 67%)
............................................................. 1342 / 1907 ( 70%)
.....S.........S............................................. 1403 / 1907 ( 73%)
..............S.........................SS................... 1464 / 1907 ( 76%)
..............S..S........................................... 1525 / 1907 ( 79%)
...S...................SSS................................... 1586 / 1907 ( 83%)
............................................................. 1647 / 1907 ( 86%)
............................................................. 1708 / 1907 ( 89%)
............................................................. 1769 / 1907 ( 92%)
............................................................. 1830 / 1907 ( 95%)
.......SS...............SSSSSSSS

Time: 1.71 minutes, Memory: 101.00Mb

OK, but incomplete or skipped tests!
Tests: 1861, Assertions: 8619, Incomplete: 1, Skipped: 131.

看起來,WordPress是參考phpunit.xml.dist這個檔案,不過這檔案非必要。查了下Appendix C. The XML Configuration File,針對WordPress的phpunit.xml.dist稍微記錄一下。

phpunit.xml.dist的內容如下:

<phpunit

        bootstrap="tests/phpunit/includes/bootstrap.php"
        backupGlobals="false"
        colors="true"
        >
    <testsuites>
        <!-- Default test suite to run all tests -->
        <testsuite>
            <directory suffix=".php">tests/phpunit/tests</directory>
            <exclude>tests/phpunit/tests/actions/closures.php</exclude>
            <exclude>tests/phpunit/tests/image/editor.php</exclude>
            <exclude>tests/phpunit/tests/image/editor_gd.php</exclude>
            <exclude>tests/phpunit/tests/image/editor_imagick.php</exclude>
            <file phpVersion="5.3.0">tests/phpunit/tests/actions/closures.php</file>
            <file phpVersion="5.3.0">tests/phpunit/tests/image/editor.php</file>
            <file phpVersion="5.3.0">tests/phpunit/tests/image/editor_gd.php</file>
            <file phpVersion="5.3.0">tests/phpunit/tests/image/editor_imagick.php</file>
        </testsuite>
    </testsuites>
    <groups>
        <exclude>
            <group>ajax</group>
        </exclude>
    </groups>
    <logging>
        <log type="junit" target="tests/phpunit/build/logs/junit.xml" logIncompleteSkipped="false"/>
    </logging>

</phpunit>


<phpunit>
是phpunit主要的根節點,記載可能的function。v是就指向phpunit的bootstrap.php的路徑,可以不用。

<testsuites>
就眾多的<testsuit>的集合,<testsuit>要放在裡頭

<testsuite>
實際的測試單元,會有name、directory、file等,例如這例子中唯一個testsuite

  • directory: 指定此testsuite的目錄路徑。
  • suffix: 例子中是指所有在此目錄下的php檔。
  • file: 指定某個php要執行,例如此例中closures.php,是php 5.3.0下才執行。
  • exclude: 指定某個php不執行
  • phpVersion是指定php的版本。
  • 其實還有phpVersionOperator,若設定phpVersion="5.3.0" phpVersionOperator=">=",表示php的版本要大於等於5.3.0


不過這篇主要是記錄怎樣執行WordPress的unit test的第一步,細節就再看Automated Testing吧。

PS. 也許可以幫忙寫寫WordPress的unit test。哈哈


參考網址:
Automated Testing
PHPUnit
Writing WordPress Plugin Unit Tests

Theme Unit Test
How To Join WPTRT
WP Test: Unit Testing Data for WordPress Themes and Plugins
phpunit.xml.dist設定說明:Appendix C. The XML Configuration File

沒有留言:

張貼留言