Espresso Device API를 사용하여 화면 구성 변경에 대해 테스트

기기에 일반적인 구성 변경(예: 회전 및 화면 펼치기)이 발생할 때 Espresso Device API를 사용하여 앱을 테스트합니다. Espresso Device API를 사용하면 가상 기기에서 이러한 구성 변경을 시뮬레이션하고 테스트를 동기식으로 실행할 수 있으므로 한 번에 하나의 UI 작업 또는 어설션만 발생하므로 테스트 결과의 신뢰도가 향상됩니다. Espresso로 UI 테스트를 처음 작성하는 경우 문서를 참고하세요.

Espresso Device API를 사용하려면 다음이 필요합니다.

  • Android 스튜디오 Iguana 이상
  • Android Gradle 플러그인 8.3 이상
  • Android Emulator 33.1.10 이상
  • API 수준 24 이상을 실행하는 Android 가상 기기

Espresso Device API를 위한 프로젝트 설정

Espresso Device API를 지원하도록 프로젝트를 설정하려면 다음 단계를 따르세요.

  1. 테스트가 명령어를 테스트 기기로 전달하도록 하려면 androidTest 소스 세트의 매니페스트 파일에 INTERNETACCESS_NETWORK_STATE 권한을 추가합니다.

      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permissions.ACCESS_NETWORK_STATE" />
      
  2. gradle.properties 파일에서 enableEmulatorControl 실험용 플래그를 사용 설정합니다.

      android.experimental.androidTest.enableEmulatorControl=true
      
  3. 모듈 수준 빌드 스크립트에서 emulatorControl 옵션을 사용 설정합니다.

    Kotlin

      testOptions {
        emulatorControl {
          enable = true
        }
      }
      

    Groovy

      testOptions {
        emulatorControl {
          enable = true
        }
      }
      
  4. 모듈 수준의 빌드 스크립트에서 Espresso Device 라이브러리를 프로젝트로 가져옵니다.

    Kotlin

      dependencies {
        androidTestImplementation("androidx.test.espresso:espresso-device:3.5.1")
      }
      

    Groovy

      dependencies {
        androidTestImplementation 'androidx.test.espresso:espresso-device:3.5.1'
      }
      

일반적인 구성 변경에 대한 테스트

Espresso Device API에는 기기 구성 변경을 시뮬레이션하는 데 사용할 수 있는 여러 화면 방향과 폴더블 상태가 있습니다.

화면 회전 테스트

다음은 기기 화면이 회전할 때 앱이 어떻게 되는지 테스트하는 방법의 예입니다.

  1. 먼저 일관된 시작 상태를 위해 기기를 세로 모드로 설정합니다.

      import androidx.test.espresso.device.action.ScreenOrientation
      import androidx.test.espresso.device.rules.ScreenOrientationRule
      ...
      @get:Rule
      val screenOrientationRule: ScreenOrientationRule = ScreenOrientationRule(ScreenOrientation.PORTRAIT)
      
  2. 테스트 실행 중에 기기를 가로 방향으로 설정하는 테스트를 만듭니다.

      @Test
      fun myRotationTest() {
        ...
        // Sets the device to landscape orientation during test execution.
        onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
        ...
      }
      
  3. 화면이 회전되면 UI가 예상대로 새 레이아웃에 맞게 조정되는지 확인합니다.

      @Test
      fun myRotationTest() {
        ...
        // Sets the device to landscape orientation during test execution.
        onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
        composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed()
        composeTestRule.onNodeWithTag("BottomBar").assertDoesNotExist()
      }
      

화면 펼침 테스트

다음은 앱이 폴더블 기기에 있고 화면이 펼쳐질 때 앱이 어떻게 되는지 테스트하는 방법의 예입니다.

  1. 먼저 onDevice().setClosedMode()를 호출하여 접힌 상태의 기기를 테스트합니다. 앱 레이아웃이 좁은 화면 너비에 맞게 조정되는지 확인하세요.

      @Test
      fun myUnfoldedTest() {
        onDevice().setClosedMode()
        composeTestRule.onNodeWithTag("BottomBar").assetIsDisplayed()
        composeTestRule.onNodeWithTag("NavRail").assetDoesNotExist()
        ...
      }
      
  2. 완전히 펼쳐진 상태로 전환하려면 onDevice().setFlatMode()를 호출합니다. 앱의 레이아웃이 확장 크기 클래스에 맞게 조정되는지 확인합니다.

      @Test
      fun myUnfoldedTest() {
        onDevice().setClosedMode()
        ...
        onDevice().setFlatMode()
        composeTestRule.onNodeWithTag("NavRail").assertIsDisplayed()
        composeTestRule.onNodeWithTag("BottomBar").assetDoesNotExist()
      }
      

테스트에 필요한 기기 지정

폴더블이 아닌 기기에서 접기 작업을 실행하는 테스트를 실행하는 경우 테스트가 실패할 수 있습니다. 실행 중인 기기와 관련된 테스트만 실행하려면 @RequiresDeviceMode 주석을 사용하세요. 테스트 실행기는 테스트 중인 구성을 지원하지 않는 기기에서 실행 중인 테스트를 자동으로 건너뜁니다. 기기 요구사항 규칙을 각 테스트 또는 전체 테스트 클래스에 추가할 수 있습니다.

예를 들어 평평한 구성으로 펼치기를 지원하는 기기에서만 테스트를 실행하도록 지정하려면 다음 @RequiresDeviceMode 코드를 테스트에 추가합니다.

@Test
@RequiresDeviceMode(mode = FLAT)
fun myUnfoldedTest() {
  ...
}