Jacoco 테스트 커버리지 excludes 안되는 문제

스브코·2022년 2월 4일
0

Jacoco plugin을 사용하여 작성된 테스트 케이스의 코드 커버리지를 확인해 보기로 했다.

테스트 케이스를 154개나 작성하여 80프로 이상은 기대하였다.

https://techblog.woowahan.com/2661/ <- 우아한 형제들 기술 블로그

https://bottom-to-top.tistory.com/36 <- 우아한 형제들 기술 블로그를 참고하여 작성한것 같은 블로그

위 두개의 블로그를 참고하여 Jacoco를 적용하고 코드 커버리지를 확인해보니,,

엥 75프로?!?!

하나하나 살펴 보니

뭐 이런 dto들의 getter, setter 메소드들도 테스트 케이스에 포함이 되어 있어서 위 블로그를 보니 제외 시키는 방법이 상세히 잘 나와 있었다.

이렇게 lombok.config 파일을 만들어서 스크린샷과 같이 property를 적용해주면 lombok.@Generated 주석을 Lombok 주석으로 주석 처리된 모든 클래스의 관련 메서드, 클래스 및 필드에 추가한다. 결과적으로 JaCoCo는 이 주석으로 주석이 달린 모든 구성을 무시하고 jacoco 보고서에 표시하지 않는다.

위와 같이 적용을 하고 다시 jacoco 테스트 보고서를 뽑아보면 아래와 같이 lombok이 generate한 모든 메서드는 테스트 케이스에서 제외된다.

제외를 해주니 79프로까지 올라갔다.

그리고 이제 아래와 같이 테스트 하기 까다로운 카카오 OAuth 로그인과 Application 클래스 등을 제외 시키려고 했다.

우아한 형제들 기술블로그에 나와 있는데로 적용을 시켜주었다..

jacocoTestCoverageVerification {
	violationRules {
		rule {
			enabled = true
			element = 'CLASS'

			limit {
				counter = 'LINE'
				value = 'COVEREDRATIO'
			}
			excludes = ['*.thandbag*', '*.KakaoUserService']
		}
	}
}

위에서 참고한 다른 기술 블로그에 나와 있는데로 적용시켜주었다.

jacocoTestReport {
	reports {
		html.enabled true
		xml.enabled false
		csv.enabled true
	}

	afterEvaluate {

		classDirectories.setFrom(files(classDirectories.files.collect {
			fileTree(dir: it,
					exclude: ['*.thandbag*', '*.KakaoUserService'])
		}))
	}

	finalizedBy 'jacocoTestCoverageVerification'
}

저렇게 하면 excludes가 그냥 안된다. 별짓 다해봤는데 안된다....

intellij에서 현재 gradle 버전을 확인해보니 7.3.2 였다.

그래서 gradle 공식 홈페이지에 들어가 보았다.

https://docs.gradle.org/current/userguide/jacoco_plugin.html

7.3.3 버전에 대한 공식 문서인데 excludes에 대한 내용은 없고

includes = ['org.gradle.*']

이렇게 되있는거 보니 바뀐게 없나보다..

배달의 민족 기술 블로그가 2020년도 2월에 작성된거라 확실히 오래 되긴 한것 같아서 최근 게시물로 다시 구글링을 했다.

https://www.baeldung.com/jacoco-report-exclude

신뢰할 만한 글들이 많은 Baeldung에서 게시한 글인데 2021년도 8월 게시글이라 나름 최신글이라 확인을 해보니,

약간 문법이 달랐다.

jacocoTestReport {
    dependsOn test // tests are required to run before generating the report
    
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: [
                "com/baeldung/**/ExcludedPOJO.class",
                "com/baeldung/**/*DTO.*",
                "**/config/*"
            ])
        }))
    }
}

그리고 스택오버플로우를 보니 아예 test단에서 제외 시켜버리는게 낫다는 글이 있어서 build.gradle 안에 이렇게 위 문법 대로 적용해 주었다.

test {
	finalizedBy 'jacocoTestReport'
	useJUnitPlatform()

	jacoco {
		excludes += ["com/example/thandbag/ThandbagApplication.class",
					 "com/example/thandbag/exceptionHandler/**",
					 "com/example/thandbag/repository/**",
					 "com/example/thandbag/service/KakaoUserService.class"]
	}

}
	afterEvaluate {

		classDirectories.setFrom(files(classDirectories.files.collect {
			fileTree(dir: it,
					exclude: ["com/example/thandbag/ThandbagApplication.class",
							  "com/example/thandbag/exceptionHandler/**",
							  "com/example/thandbag/repository/**",
							  "com/example/thandbag/service/KakaoUserService.class"])
		}))
	}

결과는!?!?!?

87프로! 제외가 잘된다.

공식문서나 기술블로그를 참고하는 것도 좋지만 최근 문서를 참고하는 것이 정말 중요한것 같다..

profile
익히는 속도가 까먹는 속도를 추월하는 그날까지...

0개의 댓글