Cobertura 커버리지 시각화
Cobertura 커버리지 시각화
Cobertura XML 리포트를 사용해서 MR diff에 라인별 커버리지 주석을 표시할 수 있어요. GitLab이 Cobertura XML 리포트를 읽고, 변경된 각 라인을 커버됨(초록), 커버되지 않음(빨강), 로드되었지만 실행되지 않음(주황)으로 주석 처리합니다.
출처: 문서
본문
- 티어(Tier): Free, Premium, Ultimate
- 제공 방식(Offering): GitLab.com, GitLab Self-Managed, GitLab Dedicated
커버리지 시각화는 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:
# The `visualize` stage does not exist by default.
# Define it first, or use an existing stage like `deploy`.
stage: visualize
image: registry.gitlab.com/haynes/jacoco2cobertura:1.0.11
script:
# Convert report from JaCoCo to Cobertura, using relative project path
- 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 must be configured to create an XML report
artifacts:
paths:
- build/jacoco/jacoco.xml
coverage-jdk11:
# The `visualize` stage does not exist by default.
# Define it first, or use an existing stage like `deploy`.
stage: visualize
image: registry.gitlab.com/haynes/jacoco2cobertura:1.0.11
script:
# Convert report from JaCoCo to Cobertura, using relative project path
- 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
Codeception도 PHPUnit을 통해 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 modules를 사용한다고 가정해요. -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를 사용하며Gemfile에rspec,simplecov,simplecov-cobertura가 추가되어 있음. 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
문제 해결 (Troubleshooting)
커버리지 시각화 문제 해결 — 경로 해석 실패, 파일 크기 제한, 주석 미표시 등 — 은 커버리지 시각화 문제 해결 문서를 참고하세요.