티라미수 코딩생활
[Android] App Test 시작해보기 본문
내가 앱 테스트를 공부하게 된 이유
모든 공부가 그렇지만 안드로이드를 개발하고 공부해오면서 매번 느끼는 것은 공부할 게 참 많다라는 것이었다. 그러다보니 공부 할 것도 당장 필요한 것만 하게 되면서 테스트에 대한 공부는 항상 개인적인 우선순위가 밀렸었다.
이말은 즉, 지금까지 앱을 배포하기 전에 직접 테스트해보고 QA도 요청해서 해보고 다 수작업으로 테스트 해왔다는 것이다.
When you implemented the first feature of your first app, you likely ran the code to verify that it worked as expected. You performed a test, albeit a manual test. As you continued to add and update features, you probably also continued to run your code and verify it works. But doing this manually every time is tiring, prone to mistakes, and does not scale.
Computers are great at scaling and automation! So developers at companies large and small write automated tests, which are tests that are run by software and do not require you to manually operate the app to verify the code works.
Android Developer 에서 제공하는 CodeLaps 에서 위와 같은 말이 있었다.
직접 테스트를 하는 것은 피곤하고, 실수하기 쉽고, 확장성이 좋지 않다.
이 말이 정말 공감 됐던 것이, 담당하는 앱 중 200MB 가 넘는 앱을 직접 테스트 하려고 하면 빌드 하는데만 5~7분 이상이 소요되어 테스트에 상당한 시간을 소모했던 적이 있었기 때문이다. 처음부터 해왔다면 소요를 줄일 수 있었겠지만, 이제라도 테스트에 대해서 공부하고 적용을 해나가려고 한다.
시작을 위한 gradle 세팅
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
dependencies {
// Test
testImplementation 'junit:junit:4.13.2'
testImplementation "androidx.arch.core:core-testing:2.1.0"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4"
testImplementation "com.google.truth:truth:1.1.3"
testImplementation 'androidx.test.ext:junit:1.1.3'
testImplementation 'androidx.test:runner:1.4.0'
testImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "junit:junit:4.13.2"
androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4"
androidTestImplementation "com.google.truth:truth:1.1.3"
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
Dependency 를 추가하보니 testImplementation 하고 androidTestImplementation 이 두개가 굉장히 생소했다. (처음써봐서..)
일단 차이점을 써보면 아래와 같다.
- implementation : 해당 gradle 전 범위에 적용
- testImplementation : Local test 할 떄 적용
- androidTestImplementation : instrumented test 할 때 적용
여기서 한가지 궁금해진 것은 Local test 와 instrumented test 의 차이점이다.
Local test 와 instrumented test 는 무슨 차이가 있을까?
위에서 dependency 를 추가할 때 제목에 있는 두 테스트가 무언가 차이가 있다는 것을 확인했다.
둘 사이에 어떤 차이가 있을까 알아보기 전에, 우리가 프로젝트를 처음 생성하면 자동적으로 만들어지는 패키지들을 봐보자.
main
우리가 일반적으로 작성하는 코드가 있는 곳이다. 이곳의 코드는 우리가 빌드 할 수 있는 모든 버전에서 공유되는 코드이다.
test
Local tests 라는 테스트 코드가 있는 곳이다. Local test 는 테스트 구동(run)을 JVM 에서 하기 때문에 별도의 에뮬이나 디바이스가 필요하지 않다. 그렇기 때문에 속도는 빠르지만, 실제 기기로 테스트하는 것보다는 정확성이 떨어진다.
androidTest
instrument tests 라는 테스트 코드가 있다. 여기에 있는 테스트들은 실제 기기나 에뮬레이터에서 돌아간다. local test 보다는 역시 속도는 느리지만, 우리가 실사용하면서 일어나는 것들을 테스트에 반영할 수 있다.
이 테스트는 거의 대부분 Android OS 나 Android 프레임워크를 사용하기 때문에 Context 가 필요한데, 이때 InstrumentationRegistry 라는 것을 사용한다.
Coroutine 에서 flow 를 공부하고 아키텍쳐를 공부하면서 항상 나오는 것은 '테스트에 용이하다' 라는 것이었다.
테스트에 대한 공부를 조금 늦게 시작한 감이 있지만 이후부터 실제 구동되는 것부터 LiveData-ViewModel 에서는 어떻게 사용해야하는지, Compose 까지 어떤 식으로 활용하면 좋을지 공부하면서 작성해나가고자 한다.
'Programming > Android' 카테고리의 다른 글
[Android] 갑자기 startActivityForResult 가 돌아오지 않는다? (0) | 2022.12.02 |
---|---|
[Android] LiveData 에서 쓰이는 setValue() & postValue() (0) | 2022.12.01 |
[Android] App Test 간단하게 구현해보기! (feat. TDD) (0) | 2022.11.09 |
[Kotlin] Kotlin Flows in practice 보면서 이해 해보기 (1) (0) | 2022.11.03 |
[Kotlin] Data Class 에서의 var & val (0) | 2022.10.31 |