#1 ์ด์ ๊ธ
#1-1 Unit Testing ๊ฐ์
[Android] Unit Testing - ๊ฐ์์ ํ๊ฒฝ ์ค์
#1 ์๋๋ก์ด๋ ์ฑ ํ ์คํธ#1-1 ์๋๋ก์ด๋ ์ฑ ํ ์คํธ์ ์ข ๋ฅ๋จผ์ , ์ฌ๊ธฐ์ ์๋ ๊ตฌ๊ธ ๊ณต์ ๋ฌธ์์์ ์๋๋ก์ด๋ ์ฑ ํ ์คํธ์ ๋ํ ๊ฐ์๋ฅผ ์ฝ์ผ๋ฉด ์ข๋ค. ํด๋น ๊ตฌ๊ธ ๊ณต์ ๋ฌธ์์์ ๋ณต์ฌํด์จ ์์ ๊ทธ๋ฆผ
kenel.tistory.com
์ ๋งํฌ์ ์๋ ์ด์ ๊ฒ์๊ธ์ ์ด์ด์, ์ค์ ์๋๋ก์ด๋ ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์ด ๊ธฐ์ด์ ์ธ ์๋๋ก์ด๋ Unit Testing์ ์ํํด๋ณธ๋ค.
#1-2 ํ๊ฒฝ ์ค์ (build.gradle ๋ฑ)
์ด์ ๊ฒ์๊ธ์ #3์ ํ ๋๋ก ๋ณธ ๊ฒ์๊ธ์ ๋์ค๋ ์๋๋ก์ด๋ ํ๋ก์ ํธ์ Gradle, AGP, JDK์ ๋ฒ์ ์ค์ ๋ฐ build.gradle ์ค์ ์ ์งํํ๋ค. ์ด์ ๊ฒ์๊ธ์ build.gradle๊ณผ ๋ฌ๋ฆฌ, ๋ณธ ๊ฒ์๊ธ์์๋ Unit Testing์ ์ฌ์ฉ๋์ง ์๋ build.gradle์ plugins { ... }, buildFeatures { ... }, dependencies { ... }์ ์ผ๋ถ ์์๋ฅผ ์ ๊ฑฐํ๋ค. ์ด๋ ์ฝ๋ ๋ค์ด์ดํธ๋ฅผ ์ํ ๊ฐ์ธ์ ์ธ ์ ๊ฑฐ์ด๊ธฐ ๋๋ฌธ์, ์ด ๊ธ์ ๋ณด๋ ์ฌ๋์ ์ ๊ฑฐ ์์ด ๊ทธ๋ฅ ๋ณต์ฌ ๋ฐ ๋ถ์ฌ๋ฃ๊ธฐํด๋ ๋๋ค.
#2 Unit Testing์ ์ ์ฉํ ์ํ ์ฑ
์ ์ฒด ์์ค ์ฝ๋๋ ๋ณธ ๊ฒ์๊ธ์ #5์ ์๋ค.
#2-1 interface Calculation
interface Calculation {
fun sum(operand1: Int, operand2: Int): Int
fun multiply(operand1: Int, operand2: Int): Int
fun sumAndSquare(operand1: Int, operand2: Int): Int
}
Unit Test๋ฅผ ์งํํ ํด๋์ค๊ฐ ์์๋ฐ์ ์ธํฐํ์ด์ค๋ค.
#2-2 class MyCalculation : Calculation
class MyCalculation : Calculation {
override fun sum(operand1: Int, operand2: Int): Int {
return operand1 + operand2
}
override fun multiply(operand1: Int, operand2: Int): Int {
return operand1 * operand2
}
override fun sumAndSquare(operand1: Int, operand2: Int): Int {
val sum = sum(operand1, operand2)
return multiply(sum, sum)
}
}
Unit Test๋ฅผ ์งํํ ํด๋์ค๋ค.
#3 Unit Testing
#3-1 ํ ์คํธ ํด๋์ค ๊ฐํธํ๊ฒ ๋ง๋ค๊ธฐ
ํด๋์ค ์ด๋ฆ์ ์ฐํด๋ฆญ ํ [Generate...] ํด๋ฆญ
[Test...] ํด๋ฆญ
Create Test ์ฐฝ์์ Destination pacakge ์ ํ ๋ฒํผ ํด๋ฆญ
MyCalculation ํด๋์ค์ ํจ์๋ค์ ๋จ์ํ ์ํ์ ๊ณ์ฐ๋ง ์ํํ๋ฏ๋ก, ์ด๋ฌํ ํด๋์ค์ ํ ์คํธ๋ฅผ ์๋๋ก์ด๋ ์๋ฎฌ๋ ์ดํฐ์์ ์คํํ ํ์๊ฐ ์๋ค. ๋ฐ๋ผ์ Local unit test์ ์ํ๋ unitTest ๋๋ ํ ๋ฆฌ๋ฅผ ์ ํํ๋ค (Instrumented test๋ Unit Test๋ผ๋ ๋ฒ์ฃผ์ ์ํ์ง๋ง, ์๋๋ก์ด๋ ์๋ฎฌ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ๋ค๋ ์ ์์ ์จ์ ํ(?) Unit Test๋ ์๋๊ธฐ์ ์ด๋ ๊ฒ ๋๋ ๋์๋ค ๋ณด๋ค).
OK ๋ฒํผ ํด๋ฆญ
์ด๋ ๊ฒ ํ ์คํธ ํด๋์ค๊ฐ ์์ฑ๋๋ค. ๋ฌผ๋ก , ํจํค์ง ํด๋์ ์ฐํด๋ฆญํ์ฌ ์ง์ ํด๋์ค๋ฅผ ๋ง๋ค์ด๋ ์๊ด์๋ค.
#3-2 ํ ์คํธ ํด๋์ค์ ๋ด์ฉ ์์ฑ
import com.google.common.truth.Truth
import org.junit.Before
import org.junit.Test
class MyCalculationTest {
private lateinit var myCalculation: MyCalculation
@Before
fun setUp() {
myCalculation = MyCalculation()
}
@Test
fun sum_given77And777_return854() { // subjectUnderTest_actionOrInput_resultState ๊ท์น์ผ๋ก ํจ์ ๋ช
๋ช
ํจ
val result = myCalculation.sum(77, 777)
Truth.assertThat(result).isEqualTo(/* expected = */ 854)
}
@Test
fun multiply_given3And4_return12() {
val result = myCalculation.multiply(3, 4)
Truth.assertThat(result).isEqualTo(/* expected = */ 12)
}
@Test
fun sumAndSqaure_given9And5_return196() {
val result = myCalculation.sumAndSquare(9, 5)
Truth.assertThat(result).isEqualTo(/* expected = */ 196)
}
}
@Before์ init { ... }๊ณผ ๋น์ทํ ๊ฐ๋ ์ด๋ผ๊ณ ๋ณด๋ฉด ๋๋ค. @After๋ ์กด์ฌํ๋๋ฐ, ์ด๋ @Before๊ณผ ๋ฐ๋๋ก ํ ์คํธ ์ข ๋ฃ ์ง์ ์ ์ํ๋๋ ์ฝ๋์ ๋ชจ์์ด๋ค. ๊ฐ ํ ์คํธ์ ํญ๋ชฉ์ @Test ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ๋ฉ์๋์ ํํ๋ก ๊ตฌํ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฉ์๋์ ์ด๋ฆ์ subjectUnderTest_actionOrInput_resultState ์๋ช ๊ท์น์ผ๋ก ์ง๋ ๊ฒ์ด ๊ด๋ก๋ผ๊ณ ํ๋ค. assertThat() ํจ์๋ ํ ์คํธ์ ์ฑ๊ณต ํน์ ์คํจ๋ฅผ ๊ฒฐ์ ํ๋ ๋ฉ์๋๋ค. ์ด assertThat() ๋ฉ์๋๋ ์ฌ๋ฌ ํ ์คํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋์ผํ ์ด๋ฆ์ ๋ฉ์๋๋ก์ ์ง๋๊ณ ์๋ค. ๋๋ Truth ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ assertThat()๋ฅผ ์ฌ์ฉํ๋ค.
#3 ์๋ ํ์ธ
๋ฉ์๋ ์์ ๋จ๋ ์ด๋ก์ ์ฌ์ ๋ฒํผ ํด๋ฆญ
[Run] ํด๋ฆญ
์ฑ๊ณตํ๋ค. ๋ฉ์๋ ๋ฟ๋ง ์๋๋ผ ํด๋์ค ์ด๋ฆ ์์๋ ์ฌ์๋ฒํผ์ด ์๋ค. ์ด๋ ํด๋์ค ๋ด์ ๋ชจ๋ @Test ๋ฉ์๋์ ํ ์คํธ๋ฅผ ํ ๋ฒ์ ์ํํ๋ค.
#4 ์์ฑ๋ ์ฑ
android-practice/unit-test/UnitTestingBasics at master · Kanmanemone/android-practice
Contribute to Kanmanemone/android-practice development by creating an account on GitHub.
github.com
'๊นจ์ ๊ฐ๋ ๐ > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] Unit Testing - Room๊ณผ LiveData (1) | 2024.07.08 |
---|---|
[Android] Unit Testing - ViewModel (0) | 2024.07.08 |
[Android] Unit Testing - ๊ฐ์์ ํ๊ฒฝ ์ค์ (2) | 2024.07.03 |
[Android] Dagger2 - Application ํ์ฉํ๊ธฐ (0) | 2024.06.25 |
[Android] Dagger2 - Activity์ Dependency ์ฃผ์ (0) | 2024.06.25 |