LoginSignup
13
13

Cordova Pluginのテストフレームワーク(cordova-plugin-test-framework)の使い方

Last updated at Posted at 2015-05-18

Cordova Plugin Test Framework

何?

  • Cordova Pluginのテストフレームワーク
  • PluginのJSAPIを叩いて動作を確認することができる
  • できるJasmine2を利用

試してみる

device pluginのテストを動かしてみる。
まずはプロジェクトにcordova-plugin-test-frameworkを入れます。

$ cordova plugin add http://git-wip-us.apache.org/repos/asf/cordova-plugin-test-framework.git

次にdeviceプラグインとそのテストを入れます。
テストはpluginのtestsディレクトリに入っています。

$ cordova plugin add add http://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git
$ cordova plugin add http://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git#:/tests

でconfig.xmlのタグを

<content src="cdvtests/index.html" />

に書き換えて、アプリを動かす。

以下の様な画面が表示されます。(下記はiOS)
cordova-test1.png

で、自動テストはAutoTestsの方を選択
cordova-test2.png

Jasminでユニットテストが動作できているのが確認できます。
Manual Testsの方は

cordova-test3.png

となり、手動で操作するテストの画面が出てきます。

テストの書き方

https://github.com/apache/cordova-plugin-device/blob/master/tests/tests.js
https://github.com/apache/cordova-plugin-camera/blob/master/tests/tests.js
を参照してマネして書いて欲しいのですが、

自動テストのほうは、

tests.js
exports.defineAutoTests = function() {
    describe('test1', function() {
		it("should exist", function() {
			expect(window.foo).toBeDefined();
		})
    }
};

のようにdefineAutoTests内でJasmineの形式にのっとってテストを書いていけばよいです。

手動テストは

tests.js
exports.defineManualTests = function(contentEl, createActionButton) {
	contentEl.innerHTML = '<div id="test"></div>';
	createActionButton('test', function() {
		//test contents
	}, "test");
};

のようにManualTestsにテストを書き加えていきます。

contentElのinnerHTMLでコンテンツを挿入し、
createActionButton にボタンのおしたときの振る舞いを記述できます。
createActionButtonの第3引数に指定したidの場所にボタンが表示されます。

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13