Cobertura 커버리지 시각화
GitLab v19.2Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
Cobertura XML 보고서를 사용하여 머지 리퀘스트 diff에 라인별 커버리지 주석을 표시합니다. 커버리지 시각화는 artifacts:reports:coverage_report 키워드를 사용합니다. Cobertura XML 형식은 원래 Java용으로 개발되었지만, 대부분의 커버리지 프레임워크에서 플러그인이나 내장 익스포터를 통해 지원합니다:
Cobertura XML 보고서를 사용하여 머지 리퀘스트 diff에 라인별 커버리지 주석을 표시합니다. GitLab은 Cobertura XML 보고서를 읽고 변경된 각 라인을 커버됨(녹색), 커버되지 않음(빨간색), 또는 로드되었지만 한 번도 실행되지 않음(주황색)으로 주석 표시합니다. GitLab은 파이프라인의 모든 Stage에서 모든 job의 보고서를 포함합니다.
커버리지 시각화는 artifacts:reports:coverage_report
키워드를 사용합니다. MR 위젯에 커버리지 비율을 표시하거나 커버리지 히스토리 그래프를 채우지는 않습니다.
커버리지 비율을 표시하려면
coverage 키워드를 별도로 구성하세요.
Cobertura XML 형식은 원래 Java용으로 개발되었지만, 대부분의 커버리지 프레임워크에서 플러그인이나 내장 익스포터를 통해 지원합니다:
-
simplecov-cobertura (Ruby)
-
gocover-cobertura (Go)
-
cobertura (Node.js)
-
Istanbul (JavaScript)
-
Coverage.py (Python)
-
PHPUnit (PHP)
CI/CD 구성 예시#
다음 예시들은 다양한 프로그래밍 언어에 대한 CI/CD job 구성 방법을 보여줍니다.
coverage-report
데모 프로젝트에서 작동하는 예시도 확인할 수 있습니다.
JavaScript 예시#
다음 .gitlab-ci.yml 예시는 Mocha와
nyc를 사용하여 커버리지 아티팩트를 생성합니다:
test:
script:
- npm install
- npx nyc --reporter cobertura mocha
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage/cobertura-coverage.xml
Java 및 Kotlin 예시#
GitLab 17.6 이상에서는 JaCoCo 형식을 기본으로 지원합니다. 새 프로젝트에는 기본 JaCoCo 보고서를 사용하세요.
다음 예시는 jacoco2cobertura Docker 이미지를 사용하여 JaCoCo 보고서를 Cobertura 형식으로 변환합니다.
Maven 예시#
test-jdk11 job은 Maven을 사용하여 JaCoCo XML
아티팩트를 생성합니다. coverage-jdk11 job은 이를 Cobertura 형식으로 변환합니다:
test-jdk11:
stage: test
image: maven:3.6.3-jdk-11
script:
- mvn $MAVEN_CLI_OPTS clean org.jacoco:jacoco-maven-plugin:prepare-agent test jacoco:report
artifacts:
paths:
- target/site/jacoco/jacoco.xml
coverage-jdk11:
# `visualize` stage는 기본적으로 존재하지 않습니다.
# 먼저 정의하거나 `deploy`와 같은 기존 stage를 사용하세요.
stage: visualize
image: registry.gitlab.com/haynes/jacoco2cobertura:1.0.11
script:
# 상대 프로젝트 경로를 사용하여 JaCoCo에서 Cobertura로 보고서 변환
- python /opt/cover2cover.py target/site/jacoco/jacoco.xml $CI_PROJECT_DIR/src/main/java/
> target/site/cobertura.xml
needs: ["test-jdk11"]
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: target/site/cobertura.xml
Gradle 예시#
test-jdk11 job은 Gradle을 사용하여 JaCoCo XML 아티팩트를 생성합니다.
coverage-jdk11 job은 이를 Cobertura 형식으로 변환합니다:
test-jdk11:
stage: test
image: gradle:6.6.1-jdk11
script:
- gradle test jacocoTestReport # JaCoCo는 XML 보고서를 생성하도록 구성되어야 합니다
artifacts:
paths:
- build/jacoco/jacoco.xml
coverage-jdk11:
# `visualize` stage는 기본적으로 존재하지 않습니다.
# 먼저 정의하거나 `deploy`와 같은 기존 stage를 사용하세요.
stage: visualize
image: registry.gitlab.com/haynes/jacoco2cobertura:1.0.11
script:
# 상대 프로젝트 경로를 사용하여 JaCoCo에서 Cobertura로 보고서 변환
- python /opt/cover2cover.py build/jacoco/jacoco.xml $CI_PROJECT_DIR/src/main/java/
> build/cobertura.xml
needs: ["test-jdk11"]
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: build/cobertura.xml
Python 예시#
다음 .gitlab-ci.yml 예시는 pytest-cov를
사용하여 테스트 커버리지 데이터를 수집합니다:
run tests:
stage: test
image: python:3
script:
- pip install pytest pytest-cov
- pytest --cov --cov-report term --cov-report xml:coverage.xml
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.xml
PHP 예시#
다음 .gitlab-ci.yml 예시는 PHPUnit을
사용하여 테스트 커버리지 데이터를 수집하고 보고서를 생성합니다.
최소한의 phpunit.xml 파일로
(이 예시 리포지터리 참조 가능),
테스트를 실행하고 coverage.xml을 생성할 수 있습니다:
run tests:
stage: test
image: php:latest
variables:
XDEBUG_MODE: coverage
before_script:
- apt-get update && apt-get -yq install git unzip zip libzip-dev zlib1g-dev
- docker-php-ext-install zip
- pecl install xdebug && docker-php-ext-enable xdebug
- php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
- php composer-setup.php --install-dir=/usr/local/bin --filename=composer
- composer install
- composer require --dev phpunit/phpunit phpunit/php-code-coverage
script:
- php ./vendor/bin/phpunit --coverage-text --coverage-cobertura=coverage.cobertura.xml
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.cobertura.xml
PHPUnit을 통한 Codeception도 run을 사용하여
Cobertura 보고서 생성을 지원합니다. 생성된 파일의 경로는 --coverage-cobertura 옵션과
단위 테스트 스위트의 paths 구성에
따라 달라집니다. 적절한 경로에서 Cobertura를 찾을 수 있도록
.gitlab-ci.yml을 구성하세요.
C/C++ 예시#
다음 gcc 또는 g++를 사용하는 C/C++용 .gitlab-ci.yml 예시는
gcovr을 사용하여
Cobertura XML 형식으로 커버리지 출력 파일을 생성합니다.
이 예시는 다음을 가정합니다:
-
Makefile이 이전 단계의 다른 job에서build디렉터리의cmake에 의해 생성됩니다.automake를 사용하여Makefile을 생성하는 경우make test대신make check를 호출하세요. -
cmake(또는automake)에서 컴파일러 옵션--coverage를 설정했습니다.
run tests:
stage: test
script:
- cd build
- make test
- gcovr --xml-pretty --exclude-unreachable-branches --print-summary -o coverage.xml --root ${CI_PROJECT_DIR}
artifacts:
name: ${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA}
expire_in: 2 days
reports:
coverage_report:
coverage_format: cobertura
path: build/coverage.xml
Go 예시#
다음 .gitlab-ci.yml 예시는 다음을 사용합니다:
-
go test로 테스트를 실행합니다. -
gocover-cobertura로 Go의 커버리지 프로파일을 Cobertura XML 형식으로 변환합니다.
이 예시는 Go 모듈이 사용되고 있다고 가정합니다.
-covermode count 옵션은 -race 플래그와 함께 작동하지 않습니다.
-race 플래그를 사용하면서 코드 커버리지를 생성하려면 더 느린 -covermode atomic으로 전환하세요.
run tests:
stage: test
image: golang:1.17
script:
- go install
- go test ./... -coverprofile=coverage.txt -covermode count
- go get github.com/boumenot/gocover-cobertura
- go run github.com/boumenot/gocover-cobertura < coverage.txt > coverage.xml
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.xml
Ruby 예시#
다음 .gitlab-ci.yml 예시는 다음을 사용합니다:
-
rspec으로 테스트를 실행합니다. -
simplecov와simplecov-cobertura로 커버리지 프로파일을 기록하고 Cobertura XML 형식으로 보고서를 생성합니다.
이 예시는 다음을 가정합니다:
-
종속성 관리를 위해
bundler가 사용됩니다.rspec,simplecov,simplecov-cobertura가Gemfile에 추가되었습니다. -
spec_helper.rb파일의SimpleCov.formatters구성에CoberturaFormatter가 추가되었습니다.
run tests:
stage: test
image: ruby:3.1
script:
- bundle install
- bundle exec rspec
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage/coverage.xml
문제 해결#
커버리지 시각화 문제 해결(경로 해석 실패, 파일 크기 제한, 주석이 표시되지 않는 경우 등)은 커버리지 시각화 문제 해결을 참조하세요.