JUnit5과 Spring boot 테스트 코드 작성-테스트 커버리지와 Jacoco (5)

taehee kim·2023년 4월 6일
0
post-thumbnail

1. 테스트 커버리지

1-1. 테스트 커버리지란?

  • 작성한 코드에서 얼마만큼 테스트 코드를 통해서 실행돼었는지를 의미하는 지표이다.
  • 테스트 커버리지를 통해 전반적으로 테스트가 부족한지 여부를 알 수 있다.

1-2. 테스트 커버리지가 테스트를 잘 작성한 것일까?

  • 테스트 커버리지는 일정 기준 이하(대략 60%정도로 판단한다고 함)이면 문제가 된다고 판단할 수 있따.
  • 하지만 테스트 커버리지가 높다고 해서 테스트가 회귀 내성을 잘 가지고 있다고 볼 수는 없기 때문에 테스트 커버리지를 높이는 것을 목표로 테스트 코드를 작성해서는 안된다.

2. Jacoco

2-1. Jacoco란?

  • Jvm계열 언어에서 활용하는 대표적인 테스트 커버리지 도구이다.

2-2. Gradle로 Jacoco설정하는 법

Jacoco 설정 공식 문서

plugins {
	id 'jacoco'
}
jacoco {
	toolVersion = "0.8.8"
	reportsDirectory = layout.buildDirectory.dir('customJacocoReportDir')
}
test {
	// finalizedBy : 이(test) 작업에 대해 주어진 종료자 작업을 추가
	finalizedBy jacocoTestReport // test 작업이 끝나고 jacocoTestReport를 실행
}
jacocoTestReport {
	reports {
		xml.required = false
		csv.required = false
		html.outputLocation = layout.buildDirectory.dir('jacocoHTML')
	}
	// dependsOn : 이 작업에 지정된 종속성을 추가
	dependsOn test // jacocoTestReport 에 test라는 종속성을 추가
	finalizedBy 'jacocoTestCoverageVerification'
}

jacocoTestCoverageVerification {
	violationRules {

		rule {
			enabled = true
			//코드 버커리지 체크 기준
			element = 'CLASS'

			limit {
				counter = 'METHOD'
				value = 'COVEREDRATIO'
				minimum = 0.5
			}
		}
	}
}

plugin

id 'jacoco'
  • jacoco 플러그인를 사용할 것을 명시해준다.

test

finalizedBy jacocoTestReport 
  • Test 작업이후에 실행될 작업을 명시해준다.

jacocoTestReport

  • test 결과를 report로 생성한다.
  • report형식을 지정할 수 있고 디렉토리를 지정할 수 있다.

jacocoTestCoverageVerification

  • jacocoTestReport결과를 바탕으로 coverage가 만족되었는지를 체크한다.
  • rule내에서 element, counter, value 옵션을 지정할 수 있습니다. 해당옵션의 내용은 다음과 같습니다.

커버리지 체크 기준이 되는 element는 다음 중에 하나를 선택할 수 있습니다. 위의 샘플처럼 CLASS를 선택하면 클래스 단위로 브랜치와 라인 커버리지를 체크합니다.

BUNDLE (default): 패키지 번들
PACKAGE: 패키지
CLASS: 클래스
SOURCEFILE: 소스파일
METHOD: 메소드
설정 가능한 counter는 아래와 같습니다. (JacocoLimit ‘counter’ 참고)

LINE: 빈 줄을 제외한 실제 코드의 라인 수
BRANCH: 조건문 등의 분기 수
CLASS: 클래스 수
METHOD: 메소드 수
INSTRUCTION (default): Java 바이트코드 명령 수. Java bytecode instruction listings
COMPLEXITY: 복잡도. 자세한 복잡도 계산은 Coverage Counters – JaCoCo docs 참고
그리고 value는 아래 중에서 선택할 수 있습니다. (JacocoLimit ‘value’ 참고)

TOTALCOUNT: 전체 개수
MISSEDCOUNT: 커버되지 않은 개수
COVEREDCOUNT: 커버된 개수
MISSEDRATIO: 커버되지 않은 비율. 0부터 1 사이의 숫자로, 1이 100%입니다.
COVEREDRATIO (default): 커버된 비율. 0부터 1 사이의 숫자로, 1이 100%입니다.

2-3. Coverage 결과

Execution failed for task ':jacocoTestCoverageVerification'.
> Rule violated for class partner42.modulecommon.repository.random.RandomMatchSearch: methods covered ratio is 0.4, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.sse.SseRepositoryKeyRule: methods covered ratio is 0.4, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.user.QRole: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.user.User: methods covered ratio is 0.4, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.user.Authority: methods covered ratio is 0.2, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.user.User.UserBuilder: methods covered ratio is 0.4, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.user.QUserRole: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.user.Authority.AuthorityBuilder: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.user.QAuthority: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.utils.slack.SlackBotService: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.utils.slack.SlackBotApi: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.activity.ActivityType: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.activity.ActivityMatchScore: methods covered ratio is 0.4, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.matchcondition.QMatchConditionMatch: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.matchcondition.QArticleMatchCondition: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.matchcondition.TypeOfStudy: methods covered ratio is 0.2, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.matchcondition.TimeOfEating: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.matchcondition.QMatchCondition: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.matchcondition.WayOfEating: methods covered ratio is 0.3, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.matchcondition.MatchConditionMatch: methods covered ratio is 0.1, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.matchcondition.MatchConditionMatch.MatchConditionMatchBuilder: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.random.RandomMatch.MatchConditionComparator: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.random.RandomMatchCondition: methods covered ratio is 0.4, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.producer.AlarmProducer: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.BaseEntity: methods covered ratio is 0.3, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.BaseTimeEntity: methods covered ratio is 0.3, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.subscriber.RedisMessageSubscriber: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.member.Member.MemberBuilder: methods covered ratio is 0.3, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.config.kafka.KafkaConsumerConfig: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.config.kafka.KafkaProducerConfig: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.config.kafka.AlarmEvent: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.config.kafka.KafkaTopicConfig: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.config.redis.LettuceConnectionConfig: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.config.BootstrapDataLoader: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.utils.redis.RedisTransactionUtil.1: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.utils.redis.RedisTransactionUtil.2: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.utils.redis.RedisTransactionUtil: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.article.QArticleMember: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.article.Article.ArticleBuilder: methods covered ratio is 0.4, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.opinion.QOpinion: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.utils.RandomUtils: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.exception.NotAuthorException: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.exception.InfraException: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.exception.NoEntityException: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.exception.SlackException: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.exception.SseException: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.exception.BusinessException: methods covered ratio is 0.3, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.exception.RandomMatchAlreadyExistException: methods covered ratio is 0.0, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.alarm.Alarm.AlarmBuilder: methods covered ratio is 0.4, but expected minimum is 0.5
  Rule violated for class partner42.modulecommon.domain.model.alarm.AlarmArgs.AlarmArgsBuilder: methods covered ratio is 0.0, but expected minimum is 0.5
  • coverage 부족한 모듈들을 보여준다. 현재 테스트 작성 중이기 때문에 어떤 모듈에 테스트가 부족한지 한눈에 볼 수 있다.
  • 테스트 커버리지를 무조건 높이는 것에 집중할 필요는 없지만 너무 낮은 경우 테스트가 부족함을 알 수 있다.
profile
Fail Fast

0개의 댓글