https://www.youtube.com/watch?v=jdlBu2vFv58
왜?
배포할때마다 불안해지는 내 자신을 발견하고, 이유를 찾게 되었다. 내가 찾은 이유는 다음과 같다.
- 테스트 커버리지를 수치적으로 확인한적이 없다.
- 테스트 커버리지가 낮을거라는 생각이 머리 어딘가에 떠돌고 있었다.
위 2가지를 어떻게 해결할 것인가?
현재 백엔드는 지금까지 기획했던 기능은 모두 완료한 상태였다. 따라서 이제는 위 2가지 문제를 해결하기 위해 테스트 커버리지를 100%
로 유지하고자한다.
JaCoCo로 테스트 커버리지 측정하기
build.gradle 수정
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.1'
id 'io.spring.dependency-management' version '1.1.4'
id 'jacoco' // 테스트 커버리지 측정을 위한 JaCoCo 플러그인 추가
}
test {
finalizedBy jacocoTestReport
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
finalizedBy jacocoTestReport
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
finalizedBy jacocoTestReport // 테스트 완료 후 JaCoCo 리포트 생성을 위함
}
jacoco {
toolVersion = "0.8.7" // 사용할 JaCoCo 버전 지정을 위함
}
jacocoTestReport {
dependsOn test // 테스트 실행 후 리포트 생성을 보장하기 위함
reports {
xml.required = true // CI/CD 도구와의 연동을 위한 XML 리포트 생성을 위함
html.required = true // 사람이 보기 쉬운 HTML 리포트 생성을 위함
}
}
jacocoTestCoverageVerification {
violationRules {
rule {
element = 'CLASS' // 클래스 단위로 커버리지 체크를 위함
limit {
counter = 'BRANCH' // 분기 커버리지 체크를 위함
value = 'COVEREDRATIO'
minimum = 0.80 // 브랜치 커버리지 최소 80% 보장을 위함
}
limit {
counter = 'LINE' // 라인 커버리지 체크를 위함
value = 'COVEREDRATIO'
minimum = 0.80 // 라인 커버리지 최소 80% 보장을 위함
}
}
}
}
check {
dependsOn jacocoTestCoverageVerification // 빌드 전 커버리지 체크를 위함
}
./gradlew test
위의 build.gradle 설정 후에 ./gradlew test를 터미널에 입력하면 다음과 같은 로그가 생성된다.
[Incubating] Problems report is available at: file:///Users/gwon-yonghyeon/Downloads/Modulo/build/reports/problems/problems-report.html
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.11.1/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 576ms
5 actionable tasks: 5 up-to-date
이제 build > reports > jacoco > test > html > index.html을 열어보면, 다음과 같은 화면이 나오게 된다.
코드 커버리지가 처참한 것을 볼 수 있다. Domain부터 시작해서 전체적인 커버리지를 80 이상으로 올리는 것이 목표다.
PS) 우선은 테스트 커버리지 감을 잡기 전까지는 제외하는 클래스 없이 모든 클래스에 대한 테스트 코드를 작성할 예정이다. 실제로 동영상에서도 테스트 가능하지 않은 코드는 없다라고 말을 하셨기도 하고, 혼자 프로젝트를 하는데 하고 싶은 것은 다 해볼 예정이다.
'[프로젝트] Modulo' 카테고리의 다른 글
운영환경, 개발환경 분리하기 (0) | 2025.01.07 |
---|---|
MAU 측정하기(비관락, 낙관락, Write back) (0) | 2025.01.05 |
[MONITOR] Prometheus+Grafana로 모니터링 환경 구축 (2) | 2024.12.18 |
[TEST] Domain 계층의 테스트를 작성하며 (2) | 2024.12.17 |
Modulo 기획을 시작하다 (1) | 2024.12.12 |