woong's

android studio testing 환경 구축 하기(3) - Jacoco 사용하기 본문

Develop/Android

android studio testing 환경 구축 하기(3) - Jacoco 사용하기

dlsdnd345 2016. 2. 13. 22:27
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

android studio testing 환경 구축 하기(3) - Jacoco 사용하기


​두번째 장 junit 을 통해서 비지니스 로직 관련 테스트를 진행해 보았습니다 .  

테스트를 진행 했으나 , 자신의 코드가 어떤부분이 테스팅이 되었고 , 어떤부분이 안되었는지 모릅니다 .


그래서 code coverage 라는 툴 jacoco 를 이용하여 테스트 해보려 합니다 .



환경 구축


gradle:1.0.0 에는 testDebug 가 없어서 Robolectric 과 연동 작업이 필요 합니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
// Top-level build file where you can add configuration options common to all sub-projects/modules.
 
buildscript {
    repositories {
        jcenter()
        mavenCentral()
 
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath 'org.robolectric:robolectric-gradle-plugin:0.+'
 
 
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
 
allprojects {
    repositories {
        jcenter()
        mavenCentral()
 
    }
}
 
cs


위와 같이 메이븐센트럴을 추가 하고 , 로보렉트릭 classPath를 추가 합니다 .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
apply plugin: 'com.android.application'
apply plugin: 'robolectric'
 
android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
 
    defaultConfig {
        applicationId "com.wonng.androidtesting"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
 
 
 
}
 
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
 
}
 
cs



상단에 아래 코드를 추가 합니다 .


1
2
 
apply plugin: 'robolectric'
cs


SyncNow 를 눌러서 새로고침을 진행하면 그래들 테스크에 testDebug가 생겨 납니다 .




​여기 까지 진행하면 jacoco 를 붙일수 있습니다 . 


jacoco 에서 test 할때 사용할 junit 을 추가 합니다 .


1
2
3
4
5
6
7
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
 
    androidTestCompile 'junit:junit:4.10'
 
}
cs



1
apply plugin: 'jacoco'
cs


플러그인을 추가 하고


빌드 타입 내부에 테스트 커버리지를 사용하겠다는 코드를 넣습니다.


1
2
3
4
 
        debug{
            testCoverageEnabled = true
        }
cs


마지막으로  jacoco task 를 추가 합니다 .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
jacoco {
    toolVersion = "0.7.1.201405082137"
}
 
def coverageSourceDirs = [
        'src/main/java'
]
 
 
 
task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"
 
    description = "Generate Jacoco coverage reports"
 
    classDirectories = fileTree(
            dir: '../app/build/intermediates/classes/debug',
            excludes: ['**/R.class',
                       '**/R$*.class',
                       '**/*$ViewInjector*.*',
                       '**/BuildConfig.*',
                       '**/Manifest*.*']
    )
 
    additionalSourceDirs = files(coverageSourceDirs)
    sourceDirectories = files(coverageSourceDirs)
    executionData = files('../app/build/jacoco/testDebug.exec')
 
    reports {
        xml.enabled = true
        html.enabled = true
    }
 
}
cs



후에 터미널을 열어서 .gradlew jacocoTestReport 를 입력 합니다.




 


위와 같이 성공 여부가 나타나면 jacoco Report 가 생성된 것입니다 .


자신 프로젝트 / app / build / reports / jacoco / jacocoTestReport / html / index.xml 

이 있습니다.


아래와 같이 레포트가 생성 됩니다.






 

비지니스 로직 테스트코드가 있어서  100 % 가 나타 납니다 . 





커버가 되지 않은 코드는 빨간색으로 나타 납니다. 위 코드는 테스트 코드가 없어서 빨간색으로 나타 납니다 .


이와 같이 커버리지를 통해 어떤 부분이 코드가 안정성이 없는지 알아내고 알아내서 테스트 코드 작성을 통해서 

좀더 안정성 있는 테스트 코드를 작성 할 수 있습니다 .


Comments