Android Test 문서

cluelin·2021년 12월 24일
0

참고
https://developer.android.com/training/testing/local-tests#kotlin

local test는 안드로이드 디바이스나 에뮬레이터가 아니라, 니 컴퓨터에서 바로 동작한다.
그래서 너의 local JVM을 사용한다.

Local Test는 너의 앱 로직을 빠르게 테스트 하게 해준다.
그러나 안드로이드 프레임워크와 상호작용하는것이 불가능하기때문에 니가 테스트할수있는 테스트의 종류에 제한이 있다.

Unit Test는 코드의 작은 섹션(유닛)을 검증해준다.
해당 코드를 실행시키고 결과를 체크함으로써 검증해준다.

Unit Test는 보통 심플하지만 테스트를 고려하지않고 설계되었을때 setup이 문제가 있을수있다.

  • 네가 검증하기 원하는 코드가 테스트로 부터 접근성을 원한다면??? . 예를들어 private method는 바로 테스트 할수가 없다.
  • 독립된 상태에서 unit test를 실행하기 위해서 테스트 아래에 있는 유닛의 dependencies 는 네가 컨트롤 할수있는 컴포넌트로 반드시 교체되어야한다. fake혹은 test doble과 같은 컴포넌트.
    네 코드가 안드로이드 프레임워크에 의존하고있다면 이부분은 특히 문제가 될수있다.

    dependencies

    To do so, open your app's module's build.gradle file and specify the following libraries as dependencies. Use the testImplementation function to indicate that they apply to the local test source set, and not the application:

app 모듈 레벨의 build.gradle파일에 아래의 dependencies를 추가해라.
testImplementation를 사용해서 이것들이 어플리케이션을 위해서가 아니라, local test 를 위해서 사용됨을 밝혀라.

Note: testImplementation adds dependencies for local tests and androidTestImplementation adds dependencies for Instrumented tests.

dependencies {
 // Required -- JUnit 4 framework
 testImplementation 'junit:junit:$jUnitVersion'
 // Optional -- Robolectric environment
 testImplementation 'androidx.test:core:$androidXTestVersion'
 // Optional -- Mockito framework
 testImplementation 'org.mockito:mockito-core:$mockitoVersion
}

To do so, create a class that contains one or more test methods, usually in module-name/src/test/. A test method begins with the @Test annotation and contains the code to exercise and verify a single aspect of the component that you want to test.

하나이상의 테스트 메소드를 포함한 클래스를 생선한다. 테스트 메소드는 @Test 어노테이션을 표시하고, 네가 테스트하기를 원하는 컴포넌트의 기능을 검증하기위한 코드를 포함한다.

The following example demonstrates how to implement a local unit test class. The test method emailValidator_correctEmailSimple_returnsTrue()attempts to verify isValidEmail(),which is a method within the app. The test function will return true ifisValidEmail() also returns true.

아래의 예시는 local unit test를 어떻게 구현할수있는지 보여준다.
emailValidator_correctEmailSimple_returnsTrue는 isValidEmail함수를 검증한다.

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class EmailValidatorTest {
  @Test fun emailValidator_CorrectEmailSimple_ReturnsTrue() {
    assertTrue(EmailValidator.isValidEmail("name@email.com"))
  }

}

You should create readable tests that evaluate whether the components in your app return the expected results. We recommend you use an assertions library such as junit.Assert, Hamcrest, or Truth. The snippet above is an example of how to use junit.Assert.

Mockable Android library

When you execute local unit tests, the Android Gradle Plug-in includes a library that contains all the APIs of the Android framework, correct to the version used in your project. The library holds all the public methods and classes of those APIs, but the code inside the methods has been removed. If any of the methods are accessed, the test throws an exception.

네가 local unit test를 실행할때, Android Gradle Plugin 은 안드로이드 프레임워크의 모든 API를 포함하는 라이브러리를 include한다.
그 라이브러리는 모든 public method와 API class를 가지고있다. 그러나 해당 코드의 내부는 제거된 상태이다.
만약 메소드에 엑세스하려고하면 테스트는 에러를 던진다.

0개의 댓글