AndroidStudio:Kotlin Espresso使用。Button,IntentのUIテストコードメモ

まずはたくさんのEspressoライブラリをdependenciesに入れる。公式のやつをそのままぶちこんだ。

https://developer.android.com/training/testing/set-up-project#android-test-dependencies

dependencies {
      // Core library
      androidTestImplementation 'androidx.test:core:1.0.0'

      // AndroidJUnitRunner and JUnit Rules
      androidTestImplementation 'androidx.test:runner:1.1.0'
      androidTestImplementation 'androidx.test:rules:1.1.0'

      // Assertions
      androidTestImplementation 'androidx.test.ext:junit:1.0.0'
      androidTestImplementation 'androidx.test.ext:truth:1.0.0'
      androidTestImplementation 'com.google.truth:truth:0.42'

      // Espresso dependencies
      androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
      androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
      androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
      androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.1.0'
      androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0'
      androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.0'

      // The following Espresso dependency can be either "implementation"
      // or "androidTestImplementation", depending on whether you want the
      // dependency to appear on your APK's compile classpath or the test APK
      // classpath.
      androidTestImplementation 'androidx.test.espresso:espresso-idling-resource:3.1.0'
    }

次にテストしたいクラスのスクリプトで右クリックしてGoto->Testでテストを作る。

class MainActivity : AppCompatActivity(){}
上のMainActivity部分を選択するとGotoがでてくる。

作る場所はandroidTestの方。

hasComponent(hasShortClassName())でIntentでの画面遷移先のActivityを指定している。
toPackage()はたぶん画面遷移先のActivityのパッケージの場所かな。
hasExtraではIntentでputExtraしたKeyとValueをテストできる。

package work.prgrm.sample
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.intent.Intents.intended
import androidx.test.espresso.intent.VerificationMode
import androidx.test.espresso.intent.matcher.ComponentNameMatchers.*
import androidx.test.espresso.intent.matcher.IntentMatchers.*
import androidx.test.espresso.intent.rule.IntentsTestRule
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.ActivityTestRule
import org.hamcrest.core.AllOf.allOf

import org.junit.After
import org.junit.Before
import org.junit.Test

import org.junit.Assert.*
import org.junit.Rule
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class MainActivityTest {

    @get:Rule
    var activityRule:IntentsTestRule = IntentsTestRule(MainActivity::class.java)
    @Before
    fun setUp() {
    }

    @Test
    fun testIntent(){
        val button = onView(withId(R.id.button))
        button.perform(click())
        intended(
            allOf(
                hasComponent(hasShortClassName(".Sub")),
                toPackage("work.prgrm.sample"),
                hasExtra("position",10000)
            )

        )
    }
    @Test
    fun onClickButtonInRecyclerView(){

    }
    @Test
    fun onCreate() {
    }

    @Test
    fun onStart() {
    }
}

allOfは三つくらい種類があったけどどれでもいけるのかわからない。
注意しないといけないのは

@get:Rule
var activityRule:IntentsTestRule = IntentsTestRule(MainActivity::class.java)

上のコードでIntentsTestRuleをActivityTestRuleにしていると以下のエラーがでてくるみたい。

attempt to invoke virtual method

似てるしよく間違いそう。(小並感)

あとテスト実行中に仮想デバイスを落としたりするとエラー出るみたい。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA