이번 글에서는 안드로이드에서 어떻게 테스트를 진행하는지 공부하고자 한다!
저번글 👉🏻 안드로이드에서 TDD로 개발하기(1)
실제로 테스트를 작성하는 방법을 소개하기 전에 안드로이드 테스트 기본 요소 문서를 간단히 살펴 보고자 한다. 안드로이드 테스트 개요에는 다음과 같은 이미지를 보여주고 있다.
위로 올라갈 수록 테스트를 수행하는데 드는 비용이 증가한다. 따라서 안드로이드 문서에서 small부터 70, 20, 10의 비율로 테스트 코드를 작성하는 것을 권하고 있다.
안드로이드 프로젝트에는 이러한 테스트를 수행하는 두 개의 폴더가 있다.
위의 두개의 테스트를 번갈아 가며 애플리케이션을 개발한다. 안드로이드 테스트 문서에는 다음과 같이 테스트 기반 개발의 주기과 관련된 그림을 제공한다.
각 라이브러리에 대해서 공부해보는 것도 좋을 것 같다. 할..일.. 추가...
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'
}
나는 테스트 작성으로 프로젝트를 시작할 때 e2e테스트를 먼저 작성한다. 안드로이드에서 e2e테스트를 작성하기 위해서는 gradle에 다음과 같이 esspresso 라이브러리에 대한 종속성을 추가해주어야 한다. 또한 ui테스트를 실행하기 위해서 테스트 실행기를 포함한 라이브러리에 대한 종속성을 추가해주어야 한다.
//Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
👉🏻 AndroidJUnitRunner에 대해서
👉🏻 esspresso에 대해서
이제 진짜로 테스트 코드를 작성해보자.간단하게 Hello Test World!라는 텍스트가 화면에 보이는지 테스트하는 코드를 작성했다.
@LargeTest
@RunWith(AndroidJUnit4::class)
class HelloWorldTest {
@get:Rule
val activityRule = ActivityScenarioRule(MainActivity::class.java)
@Test
fun showHelloWorldText(){
onView(withText("Hello Test World!")).check(matches(isDisplayed()))
}
}
작성후 테스트 코드를 실행하면 테스트는 깨진다.
이유는 Hello Test World!가 화면에 보이지 않기 때문이다.(현재는 Hello World!가 출력된다.) 이제 xml로 돌아가 텍스트를 Hello Test World!로 수정하고 테스트를 다시 실행하면 테스트에 통과하게 될 것이다.
다음 글에서는 단순하지만 hello world보다는 복잡한 예제로 내가 안드로이드에서 TDD를 진행하는 방법을 설명하려고 한다. 또한 테스트를 작성할 때 가독성에 대한 부분을 고민한 내용도 다음 글에 넣고자 한다. (다다음 글이 될 수도 있지만...)