AndroidX 테스트용 프로젝트 설정

AndroidX 테스트를 사용하려면 몇 가지 테스트 관련 라이브러리를 포함해야 합니다. 이 가이드에서는 앱 프로젝트 파일에서 이러한 라이브러리를 선언하는 방법을 설명하고 AndroidX 테스트 라이브러리가 구성되는 방법을 보여줍니다.

AndroidX 테스트 라이브러리 추가

AndroidX 테스트를 사용하려면 다음 섹션에 표시된 것처럼 개발 환경 내에서 앱 프로젝트의 클래스 경로 종속성 및 manifest를 수정해야 합니다.

Gradle 종속성 추가

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

  1. 앱의 build.gradle 파일을 엽니다.
  2. repositories 섹션에서 Google Maven 저장소가 표시되는지 확인합니다.

        allprojects {
            repositories {
                jcenter()
                google()
            }
        }
  3. 사용하려는 각 AndroidX 테스트 패키지에 관해 패키지 이름을 dependencies 섹션에 추가합니다. 예를 들어 espresso-core 패키지를 추가하려면 다음 행을 추가합니다.

        dependencies {
            ...
            androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
        }

    주의: 동적 종속 항목(예: espresso-core:3.1.0+)을 사용하면 예기치 않은 버전 업데이트와 이전 버전과의 비호환성이 발생할 수 있습니다. 특정 버전 번호(예: espresso-core:3.1.0)를 명시적으로 지정하는 것이 좋습니다.

  4. 앱이 AssertTestSuiteLoader와 같은 JUnit 기반 클래스를 사용하는 테스트를 빌드한다면 파일의 android 섹션에 다음 행을 추가합니다.
        android {
            ...
    
            // Gradle automatically adds 'android.test.runner' as a dependency.
            useLibrary 'android.test.runner'
    
            useLibrary 'android.test.base'
            useLibrary 'android.test.mock'
        }

    참고: 앱에서 사용되는 클래스가 들어 있는 라이브러리만 포함해야 합니다. 각 라이브러리에 표시되는 클래스 목록은 JUnit 기반 라이브러리를 참조하세요.

매니페스트 선언 추가

JUnit 기반 클래스를 사용하는 테스트를 실행하려면 필요한 <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(API 레벨 28) 이상으로 설정되어 있는 경우에만 적용됩니다.

android.test.runner 라이브러리는 android.test.base android.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" />
    

AndroidX 테스트 종속성 목록

다음 섹션에 표시된 대로 AndroidX 테스트에는 여러 종속성이 포함되어 있습니다.

Gradle 종속성

다음 Gradle 기반 종속 항목이 AndroidX 테스트에서 제공됩니다.

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'
    }
    

Maven 저장소에서 Gradle 종속 항목 추가에 설명된 것처럼 모듈 수준 build.gradle 파일 내에 각 종속 항목을 추가하여 개발 환경에 이러한 종속 항목을 추가할 수 있습니다.

JUnit 기반 라이브러리

이 섹션의 목록은 플랫폼이 Android 9(API 레벨 28) 이상에서 JUnit 기반 클래스를 구성하는 방법을 보여줍니다.

android.test.base

이 라이브러리는 다음 클래스로 구성되며, 각 클래스는 junit.framework 패키지에 있습니다.

또한 이 라이브러리는 Android 9(API 수준 28)부터 지원 중단된 다음 클래스로 구성됩니다.

android.test 패키지

android.test.suitebuilder.annotation 패키지

com.android.internal.util 패키지

android.test.runner

이 라이브러리는 다음 클래스로 구성됩니다.

android.test 패키지

junit.runner 패키지

또한 이 라이브러리는 Android 9(API 수준 28)부터 지원 중단된 다음 클래스로 구성됩니다.

android.test 패키지

android.test.suitebuilder 패키지

android.test.mock

이 라이브러리는 다음 클래스로 구성되며, 각 클래스는 android.test.mock 패키지에 있습니다.

또한 이 라이브러리는 Android 9(API 수준 28)부터 지원 중단된 다음 클래스로 구성됩니다. 각 클래스는 android.test.mock 패키지에 있습니다.