IT/Tool
[IntelliJ] 테스트 커버리지, 내 테스트 코드는 얼마나 잘 만들었을까
으어으어
2023. 2. 11. 16:39
IntelliJ는 유료인만큼 강력하고 편리한 기능을 많이 제공한다.
그 중 내가 만든 테스트코드가 얼마나 잘 테스트되고 있는지 수치적으로 표기해주는 기능이 있다.
test code Coverage 기능에 대해 사용방법을 알아보자.
1. 테스트코드 작성
테스트코드는 본 블로그의 아래 코드를 테스트하는 코드이다.
[JAVA] 특수문자 > 숫자(0123~순, 소수점 포함) > 영어(대문자 먼저, abc~순) > 한글(ㄱㄴㄷ~순) 순서 Cust
해당 util은 아래 블로그를 참조하여 만들었습니다. 이미 잘 만들어진 util에 제가 필요로하는 기능을 위한 추가, 수정만 진행하였습니다. 출처는 아래에서 확인 가능합니다. 한글-영어-특수문자
kangyb.tistory.com
package com.practice.practice.traing;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import com.practice.practice.traing.dto.ComparatorTestDto;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@Slf4j
class StringComparatorTest {
public static List<ComparatorTestDto> resultDtos = new ArrayList<>();
public static List<String> testArray = new ArrayList<>();
public static List<String> resultArray = new ArrayList<>();
@BeforeAll
static void injectionDto() {
// given
testArray = Arrays.asList("1234", "832", "123.123", "832.523", "한글", "가나다라", "hello",
"check", "ch_ck", "[중괄호]", "[eng]", "{대괄호}", "1623", "넘버아님", "/특수/", "832", "가나다라1",
"Aad", "Check", "634.123", "634.31123", "01234", "08561", "<eng>", "^test^", "&대괄호&",
"#shap]", "#java]", "#대괄호}");
resultArray = Arrays.asList("#java]", "#shap]", "#대괄호}", "&대괄호&", "/특수/", "01234", "08561",
"123.123", "1234", "1623", "634.123", "634.31123", "832", "832", "832.523", "<eng>",
"Aad", "Check", "[eng]", "[중괄호]", "^test^", "ch_ck", "check", "hello", "{대괄호}", "가나다라",
"가나다라1", "넘버아님", "한글");
resultDtos = IntStream.range(0, resultArray.size())
.mapToObj(index -> ComparatorTestDto.builder()
.num(index)
.name(resultArray.get(index))
.info("testArray index : " + index)
.build())
.collect(Collectors.toList());
}
@Test
void comparatorTest() {
// given
List<ComparatorTestDto> testDtos = IntStream.range(0, testArray.size())
.mapToObj(index -> ComparatorTestDto.builder()
.num(index)
.name(testArray.get(index))
.info("testArray index : " + index)
.build())
.collect(Collectors.toList());
// when
List<String> testNameDtos = testDtos.stream()
.sorted((o1, o2) -> new StringComparator().compare(o1.getName(), o2.getName()))
.map(ComparatorTestDto::getName)
.collect(Collectors.toList());
List<String> resultNameDtos = resultDtos.stream()
.sorted((o1, o2) -> new StringComparator().compare(o1.getName(), o2.getName()))
.map(ComparatorTestDto::getName)
.collect(Collectors.toList());
//then
assertIterableEquals(testNameDtos, resultNameDtos);
}
}
2. 테스트 커버리지 실행
두가지 실행방법을 안내한다.
1. 해당 테스트 클래스명을 우클릭 -> More Run/Debug -> run StringComparatorTest with Covarege
2. 상단 메뉴바 -> Run -> run StringComparatorTest with Covarege
3. 결과 확인
해당 테스트는 Dto를 100% 테스트하지 못하고 있습니다.
그렇다면 테스트가 제대로 이루어지지 않은 코드에서 오류가 발생할 수 있겠죠
테스트가 부족한 클래스를 더블클릭하면 어떤 코드가 테스트되지 않는지 확인 가능하다.
상단에 보이는 버튼 4개중 가장 우측에 있는 버튼은 리포트로 만들어주기 때문에 회사에서 보고용으로 사용 가능하다.