반응형

Espresso란?

Espresso는 구글에서 제작한 안드로이드 UI를 자동 테스트하는 오픈 소스 프레임워크이다.

(에스프레소라고 명명한 이유는 테스트 하기에 간단하고, 효과적이고, 유연하기 때문이라고 한다.)

이러한 Espresso를 통해 tdd를 UI에서도 시도해볼 수 있는 좋은 기회가 될 것이고 안드로이드 개발에 있어 자동화 기술을 배우는 좋은 프레임워크가 될 것이다.

 

 

 

Espresso 환경 구성

Espresso는 안드로이드 SDK를 이용하여 안드로이드 앱을 개발한 것을 테스트 하기 위해 존재하는 User interface-testing 프레임워크이다. 결국 앱 개발한 것을 테스트하는 목적이기에 에스프레소는 오직 "Android Studio와 Android SDK"만 있으면 된다.  

 

Espresso framework를 시작하기 위해 필요한 사전 설정은 다음과 같다.

- 최신 JDK 그리고 JAVA_HOME 환경 변수

- 최신 Android studio (3.2 이상)

- 최신 Android SDK와 ANDROID_HOME 환경 변수

- 최신 Gradle build tool 그리고 GRADLE_HOME 환경 변수

 

 

 

 

 

우선 Espresso testing framework는 Android Support 라이브러리의 부분으로 제공된다.

하지만 이후 안드로이드 팀은 새로운 Android library인 AndroidX를 제공했고 최신 espresso testing framework를 해당 라이브러리로 옮겼다.

따라서 에스프레소는 이제 AndroidX library에 최신 버전이 존재하게 된다.

 

Espresso testing framework를 포함하는 프로젝트를 어떻게 gradle에 구성해야하는지 한번 확인해보자.

이는 프로젝트 최상단 gradle에 넣는게 아닌 app/build.gradle에 넣어주면 된다.

 

Android support library를 이용한 gradle

android {
   defaultConfig {
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
}
dependencies {
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espressocore:3.0.2'
}

 

 

AndroidX library를 이용한 gradle

android {
   defaultConfig {
      testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
   }
}
dependencies {
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.androidx.test:runner:1.0.2'
   androidTestImplementation 'com.androidx.espresso:espresso-core:3.0.2'
}

 

위의 gradle 내용은 아래와 같다.

 

android/defaultConfig에 있는 testInstrumentationRUnner는 AndroidJUnitRunner 클래스가 instrumentation tests를 동작하도록 설정한다.

 

dependency {

 첫번째는 JUnit testing framework를 include하고 있고

 두번째는 테스트 케이스를 실행하는 test runner library를 include하고 있고 

 세번째는 espresso testing framework를 include하고있다.

}

 

기본적으로 안드로이드 스튜디오는 새로운 프로젝트를 만들 때 gradle에 미리 JUnit과 Espresso를 설정하여 Maven repository에서 받아준다.

 

 

보통 프로젝트를 새로 만들고 build.gradle를 보면 아래와 같이 되어있다.

apply plugin: 'com.android.application'
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.tutorialspoint.espressosamples.helloworldapp"
      minSdkVersion 15
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'),    'proguard-rules.pro'
      }
   }
}
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espressocore:3.0.2'
}

이를 보면 junit, runner, espresso가 모두 포함되어있음을 알 수 있다.

 

마찬가지로 androidX로 마이그레이션하면 아래와 같이 되어있다.

apply plugin: 'com.android.application'
android {
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.tutorialspoint.espressosamples.helloworldapp"
      minSdkVersion 15
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
   }
}
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
   implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'androidx.test:runner:1.1.1'
   androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

 

이제 Espresso의 환경 설정은 끝이났다.

이를 통해 UI를 자동 테스트 하는 테스트 케이스를 만들어 보도록하자.

 

에스프레소를 이용하면 좋을 것 같다는 생각이 드는건 사실이지만

실제로 TDD 방식으로 프로그래밍을 한다면 실제 코드 / Test 코드 두개를 만들어야하고 UX 가이드가 변경되면 실제 코드와 Test 코드 둘다가 변경되어야 하기에 생산성이 떨어지는 것은 사실이다.

(하지만 유지 보수는 극대화 될 것이다.)

반응형