この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
junit4 [2015/10/21 17:11] sou [メモ] |
junit4 [2015/12/04 13:16] (現在) sou |
||
---|---|---|---|
ライン 1: | ライン 1: | ||
- | ■注意点\\ | + | ===== 注意点 ===== |
・ビルドツール(build.gradle)のバージョンが1.0.0以上の必要がある。\\ | ・ビルドツール(build.gradle)のバージョンが1.0.0以上の必要がある。\\ | ||
・ビルドタイプが「debug」でしかテストされない。「release」でも行いたい場合は下記を追加する。\\ | ・ビルドタイプが「debug」でしかテストされない。「release」でも行いたい場合は下記を追加する。\\ | ||
ライン 8: | ライン 8: | ||
- | ■ビルドファイルの設定\\ | + | ===== ビルドファイルの設定 ===== |
・build.gradle(app)\\ | ・build.gradle(app)\\ | ||
android { | android { | ||
ライン 21: | ライン 21: | ||
// ... | // ... | ||
+ | // testing libs | ||
+ | androidTestCompile 'com.android.support:support-annotations:23.0.0'\\ | ||
// runner及びrulesはtestingライブラリより分割された (旧:testing-support-lib)\\ | // runner及びrulesはtestingライブラリより分割された (旧:testing-support-lib)\\ | ||
- | androidTestCompile 'com.android.support:support-annotations:23.0.0'\\ | ||
androidTestCompile 'com.android.support.test:runner:0.3'\\ | androidTestCompile 'com.android.support.test:runner:0.3'\\ | ||
androidTestCompile 'com.android.support.test:rules:0.3'\\ | androidTestCompile 'com.android.support.test:rules:0.3'\\ | ||
} | } | ||
- | + | ===== テストコードの作成 ===== | |
- | ■テストコードの作成\\ | + | |
パッケージ名(androidTest)ディレクトリにテストコードを作成します。\\ | パッケージ名(androidTest)ディレクトリにテストコードを作成します。\\ | ||
デフォルトでApplicationTestというクラスが生成されている場所です。\\ | デフォルトでApplicationTestというクラスが生成されている場所です。\\ | ||
ライン 35: | ライン 35: | ||
- setUp() を Override し、public に変更し @Before をつける\\ | - setUp() を Override し、public に変更し @Before をつける\\ | ||
- tearDown() を Override し、public に変更し @After をつける\\ | - tearDown() を Override し、public に変更し @After をつける\\ | ||
- | - 必要に応じて AndroidTestCase.setContext(InstrumentationRegistry.getContext()) や \\InstrumentationTestCase.injectInstrumentation(InstrumentationRegistry.getInstrumentation()) を setUp() で行う\\ | + | - 必要に応じて AndroidTestCase.setContext(InstrumentationRegistry.getContext()) や\\InstrumentationTestCase.injectInstrumentation(InstrumentationRegistry.getInstrumentation()) を setUp() で行う\\ |
- | - メソッド名の命名規則として先頭にtestをつける (例: testHoge())\\ | + | |
- | ・ちょっと裏ワザ\\ | ||
- | テストしたいクラス名を選択した状態でAlt + Enterで一括でテスト関数を生成出来る。\\ | ||
---------------------------------\\ | ---------------------------------\\ | ||
ライン 51: | ライン 49: | ||
setContext(InstrumentationRegistry.getContext()); | setContext(InstrumentationRegistry.getContext()); | ||
} | } | ||
- | | + | |
+ | @Test | ||
+ | public void something() throws Exception { | ||
+ | ... | ||
+ | } | ||
+ | | ||
@After | @After | ||
public void tearDown() throws Exception { | public void tearDown() throws Exception { | ||
ライン 58: | ライン 61: | ||
} | } | ||
- | ■メソッドについて\\ | + | ===== メソッドについて ===== |
// 予測, 実際\\ | // 予測, 実際\\ | ||
assertEquals(expected, actual);\\ | assertEquals(expected, actual);\\ | ||
ライン 68: | ライン 71: | ||
\\ | \\ | ||
\\ | \\ | ||
- | ■IDEで実行 | + | |
+ | ===== 実行 ===== | ||
run > setting (実行 > デバッグ設定)\\ | run > setting (実行 > デバッグ設定)\\ | ||
+を押下\\ | +を押下\\ | ||
ライン 106: | ライン 110: | ||
out:実装による\\ | out:実装による\\ | ||
- | ここをテストする場合は、inをモックに置き換える | + | ここをテストする場合は、inをモックに置き換える\\ |
- | \\ | + | |
+ | @Test | ||
+ | public void something() throws Exception { | ||
+ | /** | ||
+ | * モックの生成 | ||
+ | */ | ||
+ | SomeClass mockedSome = mock(SomeClass.class); | ||
+ | |||
+ | /** | ||
+ | * テストしたいビジネスロジックを記述する(対象のコードと同じコード) | ||
+ | * 但しそこで取り扱うオブジェクトは、上記のモックに置き換わる | ||
+ | * | ||
+ | * ※対象のコードを変更した場合は、テストコードも同じ用に修正する必要がある。 | ||
+ | * outがある関数を呼び出す場合は、outを検証するだけで良い | ||
+ | */ | ||
+ | } | ||
| | ||
ライン 117: | ライン 136: | ||
Building Instrumented Unit Tests\\ | Building Instrumented Unit Tests\\ | ||
https://developer.android.com/intl/ja/training/testing/unit-testing/instrumented-unit-tests.html\\ | https://developer.android.com/intl/ja/training/testing/unit-testing/instrumented-unit-tests.html\\ | ||
+ | テスト - Mixi AndroidTraining\\ | ||
http://mixi-inc.github.io/AndroidTraining/fundamentals/2.11.testing.html\\ | http://mixi-inc.github.io/AndroidTraining/fundamentals/2.11.testing.html\\ | ||