최근 회사에서 jacoco로 line coverage exclude를 할 일이 생겼다.
protobuf로 java file을 만들었는데, protobuf 생성 파일이 라인 수가 꽤 되서 line coverage를 떨어뜨리고 있었다.

exclude를 적용하는 방법에 애를 먹어 정리한다.

jacoco란?

먼저 jacoco가 뭔지 알아야겠다.
jacoco는 line coverage report를 만들어주는 툴이다.

jacoco가 직접 test를 한다고 생각하기 쉬운데 실제로 그렇지 않다.
jacoco는 surefire의 test coverage information을 가져다가 보고서를 작성하는 녀석이다.

jacoco goal

사실 jacoco를 쓸 때 알아야 할 것들이 많은 건 아니다.
내가 쓰면서 의문이 있었던 부분은 pom.xml에 추가되는 goal들이 무슨 의미였는지였다.

기본으로 추가하는 pom.xml plugin.

<plugin>
	<groupId>org.jacoco</groupId>
	<artifactId>jacoco-maven-plugin</artifactId>
	<version>0.8.2</version>
	<executions>
		<execution>
			<goals>
				<goal>prepare-agent</goal>
			</goals>
		</execution>
		<execution>
			<id>report</id>
			<phase>test</phase>
			<goals>
				<goal>report</goal>
			</goals>
		</execution>
	</executions>
</plugin>

prepare-agent

agent는 collect coverage information when maven-surefire-plugin runs the tests을 한다.
위에서 말했듯이 surefire의 결과를 가져오는 역할이다.

별도의 destFile이 set되어있지 않다면 target/jacoco.exec에 가져온다.

report

report는 creates code coverage reports for tests in HTML, XML, CSV formats을 한다.
agent에서 가져온 data로 coverage report를 작성하는 역할이다.

별도의 outputDirectory가 set되어있지 않다면 target/site/jacoco에 파일들을 생성한다.

check

check는 validates the coverage rules를 한다.
coverage가 통과 되었는지 등을 확인할 수 있는 goal인데, 필수적으로 필요한 건 아니다.
여기서 goal을 설정하면 build하다가 fail을 만들 수 있다.


check와 같이 사용되는 goal들이 더 있는데, document를 확인하면 될 것 같다.

jacoco command

이제 jacoco command를 확인해보자.
intellij에서 run configuration을 jacoco로 하고 line coverage test를 돌리면 intellij test가 돌더라..

  • 이 문제는 내 pc에서만 그럴지도 모르겠다.

그래서 jacoco 결과를 받지 못했고, command로 돌려서 결과를 확인하는 방법을 찾았다.

The command mvn clean test runs unit tests and creates the code coverage report for unit tests to the directory target/site/jacoco-ut.
The command mvn clean verify -P integration-test runs integration tests and creates the code coverage report for integration tests to the directory target/site/jacoco-it.
The command mvn clean verify -P all-tests runs unit and integration tests and creates code coverage reports for unit and integration tests.

기본적으로 unit test에 대한 coverage를 측정하니까 mvn clean test를 통해 coverage report를 확인할 수 있다.

주의사항
pom.xml에서 surefire.skipTests를 true로 설정했다면 surefire가 돌지 않고,
surefire가 돌지 않는다면 jacoco는 test report를 만들 수 없다.

jacoco.exec

위 agent에서 확인한 jacoco.exec 파일은 열어서 확인할 수 없었다.
jacoco.exec는 eclipse나 intellij에서 확인할 수 있었다.

intellij 기준

  1. Run > Show Code Coverage Data
  2. jacoco.exec 찾아서 선택

jacoco exclude

jacoco exclude가 이번 글의 목적이었다.
exclude를 확인하는데 굉장히 오랜 삽질을 했는데, 위에 적어둔 개념들이 부족했기 때문이다..

jacoco.exec가 jacoco report인 줄 알았고, intellij에서 이거만 주구장창 보면서 exclude를 확인했으니.. 절대 확인할 수 없었다.

  • 위를 다시 봐보자. jacoco.exec는 surefire의 coverage information이다.

그래서 결국 jacoco exclude를 확인하려면 report 결과물의 index.html 이나 jacoco.xml을 확인해야 한다.

jacoco exclude

jacoco plugin에서 exclude를 하면 test report에서 exclude를 시키는 것이다.
exclude rule은 굉장히 쉽다. 공식 docu를 보면 **, ?, * 등을 활용할 수 있음을 명시하고 있다.

<configuration>
    <excludes>
        <exclude>**/protobuf/*</exclude>
    </excludes>
</configuration>

surefire exclude

surefire를 계속 얘기하는건.. 삽질을 많이 했다는거다.
그만큼 명확하게 정리된 글이 없었기도 한 것 같다.

surefire에서도 exclude를 할 수 있는데,
surefire에서 exclude하면 test를 아예 돌려버리지 않는 것이다.

즉 jacoco는 exclude로 coverage 대상에서 제외시켜 coverage를 올리지만,
surefire에서 exclude하면 작성된 test를 제외시켜 coverage가 되려 떨어질 수 있다.

reference