はじめに
ユニットテスト良いですよね。
プラグインなどのロジックはテストを作っておくと変更が捗りますね。
ユニットテスト周りはあまり得意ではなくて、過去に何度か試していたのですがローカルのテスト環境構築に失敗することが何度かあったので、その辺りについて纏めます。
環境はMac OS X自体に構築しています。
大枠
WP-CLI+PHPUnitを使ったWordPressプラグインのユニットテスト(2)
https://firegoby.jp/archives/5511
非常に参考になります。ありがたいです。
PHPUnitのインストール
$ brew install phpunit
以下のようにバージョンが表示できればインストール成功です。
$ phpunit --version
PHPUnit 5.2.10 by Sebastian Bergmann and contributors.
以前にhome brewからインストールしようとしたのですが、3系しか入らなくて手こずった覚えがあります。
下記のコマンドでもインストール可能です。
$ curl https://phar.phpunit.de/phpunit.phar -L -o phpunit.phar
$ chmod +x phpunit.phar
$ mv phpunit.phar /usr/local/bin/phpunit
http://stackoverflow.com/questions/3301300/setting-up-phpunit-on-osx
ちなみに公式ではPHPUnitは4.8.x系しかサポートしていない、とのことでした。
4.8.x required, 5.x not supported
https://wp-cli.org/docs/plugin-unit-tests/
今回は5系で試しましたがうまく行きました。
動作用WordPressのインストール
開発用にローカル環境のWordPressをインストールします。
Zipファイルを落としてきたり、WP-CLIでインストールします。
今回は http://ex1.dev というURLで構築しました。
プラグインのひな形作成、テスト用WordPressのインストール
$ wp scaffold plugin twitter-shortcode
これで /wp-content/plugins/
に twitter-shortcode というディレクトリが出来ました。
移動してテスト用のWordPressをインストールします。
$ cd $(wp plugin path --dir my-plugin)
$ bash bin/install-wp-tests.sh wordpress_test root 'wordpress' localhost latest
install-wp-tests.sh の引数は、データベース名、ユーザ、パスワード、DBホスト、WordPressバージョンの順です。
後ろ二つは省略可能です。
ここで何度か躓きました。データベースが既に存在する場合にはうまく行きません。
データベースが既に存在するエラー
テスト用じゃないデータベースと共用していたためにエラーがでてしまいました。
手動でこのデータベースをドロップするとうまくいきます。
$ mysqladmin -h mysql.dev -u root -p drop wordpress_test
Enter password:
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
Do you really want to drop the 'wordpress_test' database [y/N] y
Database "wordpress_test" dropped
この後に再度通します。
この作業を手順をスクリプト化したものが公開されています。(後述)
phpunit
$ phpunit

通りました!
その他
他のプラグインのユニットテストを通してみる
テストケースが豊富なCustom Post Type Permalinksを通してみました。

ちゃんと通りました。テストたくさんだとカッコいいですね。
No such file or directory で通らなくなった
数日後、再度テストを行おうとしたところエラーになりました。
/tmpを利用しているためか、/tmp/wordpress 関連のファイルが消えていました。
再度インストールを行うと通りました。
このエラーを何度か経験しているので注意が必要です。
$ phpunit
PHP Warning: require_once(/tmp/wordpress-tests-lib/includes/functions.php): failed to open stream: No such file or directory in /Users/yousan/Sites/ex1.aramaki.l2tp.org/wp-content/plugins/twitter-shortcode/tests/bootstrap.php on line 8
Warning: require_once(/tmp/wordpress-tests-lib/includes/functions.php): failed to open stream: No such file or directory in /Users/yousan/Sites/ex1.aramaki.l2tp.org/wp-content/plugins/twitter-shortcode/tests/bootstrap.php on line 8
PHP Fatal error: require_once(): Failed opening required '/tmp/wordpress-tests-lib/includes/functions.php' (include_path='.:/usr/local/Cellar/php56/5.6.2/lib/php/SebastianBergmann/') in /Users/yousan/Sites/ex1.aramaki.l2tp.org/wp-content/plugins/twitter-shortcode/tests/bootstrap.php on line 8
Fatal error: require_once(): Failed opening required '/tmp/wordpress-tests-lib/includes/functions.php' (include_path='.:/usr/local/Cellar/php56/5.6.2/lib/php/SebastianBergmann/') in /Users/yousan/Sites/ex1.aramaki.l2tp.org/wp-content/plugins/twitter-shortcode/tests/bootstrap.php on line 8
もう一度インストールするとうまく行きます。
$ bash bin/install-wp-tests.sh wordpress_test root 'wordpress' localhost latest
$ phpunit
テスト用データベース、テスト用ライブラリの再インストール
上述の「MySQLのデータベースが既に存在するエラー」や「/tmpに入れたテスト用WordPressライブラリが消えてしまう」件については @miya0001 さんが手順をスクリプトで公開してくれています。
基本的にはこのシェルスクリプトに引数を与えて叩けばいいのですが、頻繁につくったり捨てたりしないといけない環境なので、一度捨ててもっかい作り直すまでをだーって処理するためのシェルスクリプトを用意しました。
https://github.com/miya0001/install-wp-test-env
curl -L https://raw.githubusercontent.com/miya0001/install-wp-test-env/master/install.sh | bash
「よーしこれからテスト書いてロジック書くぞー」という時にリセットすると良さそうです。
WP_TESTS_DIRの設定は通らなかった
/tmpだから消されているのかな、と思いインストール先を変更しようとしたのですがうまくいきませんでした。
$ export WP_TESTS_DIR=/Users/yousan/wp-tests-lib
テスト環境インストールスクリプトの更新
テスト環境を構築してくれる install-wp-test.sh も細かく更新されています。
GitHubから最新版を拾ってきて更新しました。
$ curl -o bin/install-wp-tests.sh https://raw.githubusercontent.com/wp-cli/wp-cli/master/templates/install-wp-tests.sh