WordPressの単体テスト : WordPress

Pocket

「サイトの拡張性を飛躍的に高める WordPressプラグイン開発のバイブル」の単体テストを読んだメモです。

WordPressの単体テストについて下記記事にもまとめています。
案件テーマ開発環境(その2 単体テスト) : WordPress

単体テスト

サイトの拡張性を飛躍的に高める WordPressプラグイン開発のバイブル

PHPUnitを使います。

プラグインテスト

  1. プラグイン雛形作成
  2. テスト環境構築
    テスト用データベース作成およびテスト用WordPressをインストールするシェルスクリプを実行します。

プラグイン雛形作成

WP_CLIでテストを含むプラグイン雛形を作成します。

// wp-config.phpが配置されているフォルダへ移動します。
$ cd /path/to/wordpress-root

// プラグインの雛形を作成します。
$ wp scaffold plugin my-plugin

// 作成される雛形  
my-plugin
    |
    |-- bin
        |
        |-- install-wp-tests.sh
    |
    |-- tests
        |
        |-- bootstrap.php
        |
        |-- test-sample.php
        |
        |-- test-my-plugin.php
    |
    |-- .travis.yml
    |
    |-- phpunit.xml
    |
    |-- readme.txt
    |
    |-- my-plugin.php

テスト環境構築

雛形のbin/install-wp-tests.shを実行しテスト用データベース(wordpress_test)を作成し/tmpへテスト用wordpressをインストールします。

$ cd $(wp plugin path --dir my-plugin)
$ bash bin/install-wp-tests.sh wordpress_test root wordpress localhost latest

テスト用データベースwordpress_testをホストlocalhost、ユーザーroot、パスワードwordpressで作成し、WordPressの最新バージョンを/tmpへインストールします。 既存のデータベースがあるとinstall-wp-tests.shはエラーで終わります。

既存データベース(と既存テスト用wordpress環境)削除を行った後でinstall-wp-test.shを実行するスクリプトが下記で公開されています。
miya0001/install-wp-test-env

phpunit.xml

下記のphpunit.xmlはtestsディレクトリのtest-で始まるPHPファイルをテスト対象へ追加する設定です。

<phpunit
    bootstrap="tests/bootstrap.php"
    backupGlobals="false"
    colors="true"
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    >
    <testsuites>
        <testsuite>
            <directory prefix="test-" suffix=".php">./tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>

tests/bootstrap.php

<?php

$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
    $_tests_dir = '/tmp/wordpress-tests-lib';
}

require_once $_tests_dir . '/includes/functions.php';

function _manually_load_plugin() {
    require dirname( dirname( __FILE__ ) ) . '/my-plugin.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

require $_tests_dir . '/includes/bootstrap.php';

テストケース作成

テストケースはWP_UnitTestCaseを継承します。

class My_Example_Test extends WP_UnitTestCase {
}

テーマテスト

テーマはwpコマンドでテストのscaffoldを作成できません。
pluginで作成したテスト環境をテーマディレクトリへコピーし必要なファイルを編集します1

テーマ用bootstrap.php

<?php
$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
    $_tests_dir = '/tmp/wordpress-tests-lib';
}

require_once $_tests_dir . '/includes/functions.php';

function _manually_load_plugin() {
    register_theme_directory( dirname( __FILE__ ) . '/../../' );
    switch_theme( 'my-theme' );
}

tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

require $_tests_dir . '/includes/bootstrap.php';

テストファイルの中でテストディレクトリ取得

テーマディレクトリのinc/class.my_example.phpでテスト対象クラスMy_Exampleを定義している例を記載します。

<?php
/**
 * My_Exampleテストクラス
 *
 * @package    InfoTown
 * @subpackage My_Example_Tests
 * @author     Hiroshi Sawai <info@info-town.jp>
 * @copyright  Hiroshi Sawai
 * @version    1.0.0
 * @since      1.0.0
 */
class My_Example extends WP_UnitTestCase {

    private $my_example;

    public function setUp() {
        parent::setUp();
        require_once( dirname( dirname( __FILE__ ) ) . '/inc/class.my_example.php' );
        $this->my_example = new My_Example();
    }

    public function test_do() {
        $this->assertTrue( this->my_example->do() );
    }
}

WP_UnitTestCaseで使えるメソッド例

$post_id = $this->factory->post->create();
$user_id = $this->factory->user->create();

カテゴリーを追加するサンプルです。

// カテゴリー追加サンプル1
$this->factory->category->create();
// スラッグを指定しカテゴリー追加(指定可能な引数はwp_insert_termの第3引数で指定可能な値です。
$this->factory->category->create( [ 'slug' => 'foo' ] );
// カテゴリーを追加しカテゴリーオブジェクト取得
$cat = $this->factory->category->create_and_get( [ 'slug' => 'bar' ] );

[class-wp-unittest-factory-for-term.php in trunk/tests/phpunit/includes/factory – WordPress Trac]
wp_insert_term() | Function | WordPress Developer Resources


  1. テーマディレクトリで下記コマンドを実行するとエラーが発生します。
    $ bash bin/install-wp-tests.sh wordpress_test root wordpress localhost latest 

WordPressの単体テストに関連する記事を書きました。
案件テーマ開発環境(その2 単体テスト) : WordPress

コメント

No comments yet.

コメントの投稿

改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。