ref _ https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests
디바이스 또는 에뮬레이터 위에서 작동
실제 디바이스에 대한 동작을 관찰할 때만 사용하는걸 추천
AndroidX Test는 여러 unit test에 대한 라이브러리를 제공한다.
예를 들어 Android Builder 클래스는 안드로이드의 데이터 오브젝트를 쉽게 생성하게 도와준다.
module-name/src/androidTest/java/이 디렉터리는 이미 생성되어있고 instrumented test는 여기에 작성하는 것이 좋다.
시작 전 AndroidX Test APIs를 추가하자. instrumented test를 빠르게 빌드하고 실행하게 해 줌. JUnit4 test runner인 AndroidJUnitRunner와 UI테스트 기능인 Espresso와 UI Automator등등이 포함되어 있다.
Hamcrest는 assertions를 더욱 유용하게 도와주는 matcher APIs이다.
구성 디펜던시 -
dependencies {
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
// Optional -- Hamcrest library
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with Espresso
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
// Optional -- UI testing with UI Automator
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}
JUnit4 테스트 클래스를 사용하려면 AndroidJUnitRunner가 defult test instrumentation runner로 되어있는지 build.gradle에서 확인하자
이런 식 일 것이다.
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
무엇인지 감을 잡았으니 이제 https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests 에서 나와있는 방법으로 사용해보도록 하자