AndroidX 테스트용 프로젝트 설정

AndroidX 테스트는 Android 앱에 테스트를 실행할 수 있는 Jetpack 라이브러리 모음입니다. 또한 이러한 테스트를 작성하는 데 도움이 되는 일련의 도구도 제공합니다.

예를 들어 AndroidX 테스트는 JUnit4 테스트에서 활동을 시작하고 이와 상호작용하는 JUnit4 규칙을 제공합니다. 또한 Espresso, UI Automator, Robolectric 시뮬레이터와 같은 UI 테스트 프레임워크도 포함되어 있습니다.

AndroidX 테스트 라이브러리 추가

AndroidX 테스트를 사용하려면 개발 환경 내에서 앱 프로젝트의 종속 항목을 수정해야 합니다.

Gradle 종속 항목 추가

앱 프로젝트의 종속 항목을 수정하려면 다음 단계를 완료하세요.

  • 1단계: Gradle 모듈의 build.gradle 파일을 엽니다.
  • 2단계: repositories(저장소) 섹션에 Google의 Maven 저장소가 표시되는지 확인합니다.
  allprojects {
    repositories {
      jcenter()
      google()
    }
  }
  • 3단계: 사용하려는 각 AndroidX 테스트 패키지의 경우 패키지 이름을 종속 항목 섹션에 추가합니다. 예를 들어 espresso-core 패키지를 추가하려면 다음 줄을 추가합니다.

Groovy

dependencies {
        ...
        androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
    }

Kotlin

dependencies {
        ...
        androidTestImplementation('androidx.test.espresso:espresso-core:$espressoVersion')
    }

다음은 사용 가능한 가장 일반적인 AndroidX 테스트 종속 항목입니다.

Groovy

dependencies {
    // Core library
    androidTestImplementation "androidx.test:core:$androidXTestVersion0"

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation "androidx.test:runner:$testRunnerVersion"
    androidTestImplementation "androidx.test:rules:$testRulesVersion"

    // Assertions
    androidTestImplementation "androidx.test.ext:junit:$testJunitVersion"
    androidTestImplementation "androidx.test.ext:truth:$truthVersion"

    // Espresso dependencies
    androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-accessibility:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-web:$espressoVersion"
    androidTestImplementation "androidx.test.espresso.idling:idling-concurrent:$espressoVersion"

    // 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:$espressoVersion"
}

Kotlin

dependencies {
    // Core library
    androidTestImplementation("androidx.test:core:$androidXTestVersion")

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation("androidx.test:runner:$testRunnerVersion")
    androidTestImplementation("androidx.test:rules:$testRulesVersion")

    // Assertions
    androidTestImplementation("androidx.test.ext:junit:$testJunitVersion")
    androidTestImplementation("androidx.test.ext:truth:$truthVersion")

    // Espresso dependencies
    androidTestImplementation( "androidx.test.espresso:espresso-core:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-contrib:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-intents:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-accessibility:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso:espresso-web:$espressoVersion")
    androidTestImplementation( "androidx.test.espresso.idling:idling-concurrent:$espressoVersion")

    // 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:$espressoVersion")
}

출시 노트 페이지에는 아티팩트별 최신 버전이 나와 있는 표가 있습니다.

이러한 라이브러리에 관한 구체적인 참조 문서는 패키지 색인 또는 클래스 색인을 참고하세요.

지원 중단된 클래스를 사용하는 프로젝트

앱이 지원 중단된 JUnit3 기반 android.test 클래스(예: InstrumentationTestCaseTestSuiteLoader)를 사용하는 테스트를 사용하는 경우 파일의 android 섹션에 다음 줄을 추가합니다.

android {
    ...
    useLibrary 'android.test.runner'

    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'
  }

매니페스트 선언 추가

지원 중단된 JUnit3 기반 android.test 클래스를 사용하는 테스트를 실행하려면 필요한 <uses-library> 요소를 테스트 앱의 매니페스트에 추가하세요. 예를 들어 android.test.runner 라이브러리에 종속된 테스트를 추가하는 경우 앱의 매니페스트에 다음 요소를 추가합니다.

<!-- You don't need to include android:required="false" if your app's

   minSdkVersion is 28 or higher. -->

<uses-library android:name="android.test.runner"

       android:required="false" />

지정된 JUnit 기반 클래스를 포함하는 라이브러리를 확인하려면 JUnit 기반 라이브러리를 참조하세요.

지원 중단된 클래스를 사용하고 Android 9 또는

높음

이 섹션의 안내는 Android 9 (API 수준 28) 이상을 타겟팅하고 또한 앱의 최소 SDK 버전이 Android 9로 설정된 경우에만 적용됩니다.

android.test.runner 라이브러리는 android.test.baseandroid.test.mock 라이브러리에 암시적으로 종속됩니다. 앱에서 android.test.base 또는 android.test.mock의 클래스만 사용하는 경우 다음과 같이 자체적으로 라이브러리를 포함할 수 있습니다.

<!-- For both of these declarations, you don't need to include
   android:required="false" if your app's minSdkVersion is 28
   or higher. -->

<uses-library android:name="android.test.base"
       android:required="false" />
<uses-library android:name="android.test.mock"
       android:required="false" />