Code Coverage

ChodingSense·2022년 3월 21일
0

Android

목록 보기
3/3

Jetpack Compose 프로젝트에 code coverage를 위해 Jacoco를 적용해 보기로 하였습니다.

dependencies 추가

jacocoVersion = '0.8.7'

dependencies {
	...
	classpath "org.jacoco:org.jacoco.core:$jacocoVersion"
}

plugins 추가

{
	...
    id 'jacoco'
}

task 추가

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) {
    reports {
        html.enabled(true)
        xml.enabled(true)
    }

    def fileFilter = ['**/R.class',
                      '**/R$*.class',
                      '**/BuildConfig.*',
                      '**/Manifest*.*',
                      '**/*Test*.*',
                      'android/**/*.*',
                      '**/data/models/*']
    def kotlinClasses = fileTree(dir: "${buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories.setFrom(files([mainSrc]))
    classDirectories.setFrom(files([kotlinClasses]))
    executionData.setFrom(fileTree(dir: "$buildDir", includes: [
            "jacoco/testDebugUnitTest.exec",
            "outputs/code-coverage/connected/*coverage.ec"
    ]))
}

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']
}

testOptions 추가

android {
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}

test를 위해 robolectric 추가

dependencies {
	testImplementation "org.robolectric:robolectric:4.5.1"
}

targetSdk 버전 이슈가 있음.

test code

@RunWith(RobolectricTestRunner::class)
class IntroScreenTest {

    @get: Rule
    val composeTestRule = createComposeRule()

    @Before
    fun setUp() {
        composeTestRule.setContent {
            IntroScreen(
                TITLE,
                CONTENT,
                DRAWABLE
            )
        }
    }

    @Test
    fun `text as expected`() {
        with(composeTestRule) {
            onNodeWithText(TITLE).assertIsDisplayed()
        }
    }

    companion object {
        private const val TITLE = "경찰청의 안전드림 앱과\n함께 사용을 권장드립니다."
        private const val CONTENT = "안전드립앱에서 사전 지문등록을 하시면\r\n전국 경찰서 및 관공서와 연계됩니다."
        private const val DRAWABLE = R.drawable.safety_dream
    }

}

gradle command

./gradlew clean JacocoTestReport

profile
물고기를 좋아합니다.

0개의 댓글