ユーザ用ツール

サイト用ツール


junit4

差分

この文書の現在のバージョンと選択したバージョンの差分を表示します。

この比較画面にリンクする

次のリビジョン
前のリビジョン
junit4 [2015/10/12 21:31]
sou 作成
junit4 [2015/12/04 13:16] (現在)
sou
ライン 1: ライン 1:
-注意点\\+===== 注意点 ​=====
 ・ビルドツール(build.gradle)のバージョンが1.0.0以上の必要がある。\\ ・ビルドツール(build.gradle)のバージョンが1.0.0以上の必要がある。\\
 ・ビルドタイプが「debug」でしかテストされない。「release」でも行いたい場合は下記を追加する。\\ ・ビルドタイプが「debug」でしかテストされない。「release」でも行いたい場合は下記を追加する。\\
-android {+  ​android {
     // ...     // ...
     testBuildType "​release"​     testBuildType "​release"​
-}+  ​}
  
  
-ビルドファイルの設定\\+===== ビルドファイルの設定 ​=====
 ・build.gradle(app)\\ ・build.gradle(app)\\
-android {\\ +  ​android { 
-    defaultConfig {\\ +    defaultConfig { 
-        // ...\\ +        // ... 
-\\+  
         testInstrumentationRunner "​android.support.test.runner.AndroidJUnitRunner"​\\         testInstrumentationRunner "​android.support.test.runner.AndroidJUnitRunner"​\\
-    }\\ +    } 
-}\\+  }
  
-dependencies {\\ +  ​dependencies { 
-     // ...\\ +     // ... 
-     \\ +      
-     // ​runner及びrulesはtestingライブラリより分割された (旧:​testing-support-lib)\\+     // testing ​libs
      ​androidTestCompile '​com.android.support:​support-annotations:​23.0.0'​\\      ​androidTestCompile '​com.android.support:​support-annotations:​23.0.0'​\\
 +     // runner及びrulesはtestingライブラリより分割された (旧:​testing-support-lib)\\
      ​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で一括でテスト関数を生成出来る。\\ 
 ---------------------------------\\ ---------------------------------\\
  
-@RunWith(AndroidJUnit4.class)\\ +  ​@RunWith(AndroidJUnit4.class) 
-public class MetricsTest extends AndroidTestCase {\\+  public class MetricsTest extends AndroidTestCase { 
 +   
 +    @Before 
 +    public void setUp() throws Exception { 
 +        super.setUp();​ 
 +        // instrumentation のパッケージContext 
 +        setContext(InstrumentationRegistry.getContext());​ 
 +    } 
 +     
 +    @Test 
 +    public void something() throws Exception { 
 +       ... 
 +    } 
 +     
 +    @After 
 +    public void tearDown() throws Exception { 
 +        super.tearDown();​ 
 +    } 
 +  } 
 + 
 +===== メソッドについて ===== 
 +// 予測, 実際\\ 
 +assertEquals(expected,​ actual);​\\ 
 +\\ 
 +// 実際, 予測\\ 
 +assertThat(actual,​ is(expected));​\\ 
 +上記は「import static org.hamcrest.CoreMatchers.*;​」が必要。\\ 
 +Junit4はこっちを推奨してる?\\
 \\ \\
-    @Before\\ 
-    public void setUp() throws Exception {\\ 
-        super.setUp();​\\ 
-        // instrumentation のパッケージContext\\ 
-        setContext(InstrumentationRegistry.getContext());​\\ 
-    }\\ 
 \\ \\
-    @After\\ 
-    public void tearDown() throws Exception {\\ 
-        super.tearDown();​\\ 
-    }\\ 
-}\\ 
- 
-■メソッドについて\\ 
-// 予想, 実際\\ 
-assertEquals(expected,​ actual);\\ 
- 
  
-■ run > setting (実行 > デバッグ設定)\\+===== 実行 ===== 
 + run > setting (実行 > デバッグ設定)\\
 +を押下\\ +を押下\\
 Android Tests\\ Android Tests\\
ライン 70: ライン 80:
 USBデバイスでテストを実行する場合は、USB Deviceにチェックを入れる\\ USBデバイスでテストを実行する場合は、USB Deviceにチェックを入れる\\
 適用 > OK\\ 適用 > OK\\
 +いつもどおり実行。\\
 +\\
 +\\
 +
 +===== メモ =====
 +APIから値を取得し、その結果によって何らかの振る舞いを行う場合の流れの例\\
 +\\
 +1~5の流れをひとまとめにしてテストするのは難しそう。\\
 +別々に考えれば可能だが、どれをテスト対象とするのか決めておく必要がある。\\
 +\\
 +1.リクエスト関数\\
 +in:​APIの引数など\\
 +out:​なし\\
 +\\
 +2.実際に接続処理を行うHttpClientやURLConnection. (ライブラリの場合もある)\\
 +in:​request\\
 +out:​response\\
 +\\
 +3.コールバック待ちの無名クラス,​ メンバ変数など\\
 +in:​APIからの戻り\\
 +out:​APIからの戻り\\
 +\\
 +4.JSONをパースする関数\\
 +in:​APIからの戻り\\
 +out:​なんらかのオブジェクト\\
 +\\
 +5.得た結果を処理するビジネスロジック関数\\
 +in:​なんらかのオブジェクト\\
 +out:​実装による\\
 +
 +ここをテストする場合は、inをモックに置き換える\\
  
 +    @Test
 +    public void something() throws Exception {
 +        /**
 +         * モックの生成
 +         */
 +        SomeClass mockedSome = mock(SomeClass.class);​
 +  ​
 +        /**
 +         * テストしたいビジネスロジックを記述する(対象のコードと同じコード)
 +         * 但しそこで取り扱うオブジェクトは、上記のモックに置き換わる
 +         *
 +         * ※対象のコードを変更した場合は、テストコードも同じ用に修正する必要がある。
 +         * outがある関数を呼び出す場合は、outを検証するだけで良い
 +         */
 +    }
 +    ​
  
-参考URL\\+===== 参考URL ​=====
 Android JUnit 4 形式のテストにする\\ Android JUnit 4 形式のテストにする\\
 http://​y-anz-m.blogspot.jp/​2015/​08/​androidjunit-4.html\\ http://​y-anz-m.blogspot.jp/​2015/​08/​androidjunit-4.html\\
ライン 79: ライン 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\\
  
junit4.1444653079.txt.gz · 最終更新: 2015/10/12 21:31 by sou