code for kosen meetup #3
Post on 28-Jul-2015
621 Views
Preview:
TRANSCRIPT
残念なお知らせ
この発表には以下の点が含まれます
・Android 未経験者ガン無視・身内要素・noob・ライオン
「知らねぇよ!!!」って方もこの機会に Android開発に興味を持っていただければ(?)
テスト用ライブラリ ?
ググるとなんか出てくる
・JUnit4・Espresso・Roborectric・Robotiumとか
2014/12 Android SDK でサポート
2014/12 Support Library に追加
テスト用ライブラリ ?
(標準で使えそうなので…)
・JUnit4・Espresso
2014/12 Android SDK でサポート
2014/12 Support Library に追加
を使っていきます
導入
build.gradle
android { ・・・
defaultConfig { ・・・ testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } packagingOptions { exclude 'LICENSE.txt' } ・・・}
LICENSE.txt が重複するので追記
導入
build.gradle
dependencies { ・・・ compile 'com.android.support:support-annotations:22.1.1'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'}
ユニットテスト
・app/src/androidTest/java/ApplicationID
内にテストクラスを入れる・AndroidTestCase を継承・@RunWith(AndroidJUnit4.class) をつける・テストメソッドには @Test をつける
ユニットテスト
サンプル
@RunWith(AndroidJUnit4.class)public class TestSample extends AndroidTestCase{
@Test public void sample() throws Exception{ asserEquals(334, 330+4); //なんでや! }}
UI テスト
・app/src/androidTest/java/ApplicationID
内にテストクラスを入れる・ActivityInstrumentationTestCase2 を継承・@RunWith(AndroidJUnit4.class) をつける・テストメソッドには @Test をつける・setUp(),tearDown() をつける
UI テスト
・ViewMatcher で View を指定して・ViewAction で View を操作して・ViewAssertion で View の状態を確認する
Espresso による UI テスト?
UI テスト
サンプル
@RunWith(AndroidJUnit4.class)@LargeTestpublic class MyJunit4ActivityInstrumentationTest extends ActivityInstrumentationTestCase2<MyActivity> {
private MyActivity mActivity;
public MyJunit4ActivityInstrumentationTest() { super(MyActivity.class); }
UI テスト
サンプル(続)
@Before public void setUp() throws Exception { super.setUp(); injectInstrumentation(InstrumentationRegistry .getInstrumentation()); mActivity = getActivity(); }
@After public void tearDown() throws Exception { super.tearDown(); }
UI テスト
サンプル(続)
@Before public void setUp() throws Exception { super.setUp(); injectInstrumentation(InstrumentationRegistry .getInstrumentation()); mActivity = getActivity(); }
@After public void tearDown() throws Exception { super.tearDown(); }
オブジェクトのリークを回避
UI テスト
サンプル(続)
@Testpublic void testHoge(){ Espresso.onView(ViewMatchers.withId(R.id.text_hoge)) .check(ViewAssertions .matches(ViewMatchers.withText("Test")));}
テストの実行
Android Studio の場合
・Run > Edit Configuration・+ ボタンから” Android Tests” を追加・module 等を指定して OK で完了・アプリの実行と同様にテストを走らせる
おわりに
・僕はテストがかけません(ちょっと勉強しただけ)・Google 様が UI テストは Espresso 使えって 言ってた・工数が増えるけど精神の安寧を保つために テスト書けるようになろうな
top related