DEV Community

Emre YARTAŞI
Emre YARTAŞI

Posted on

Cobertura Coverage Reports for CI/CD Pipelines: @yartasdev/cobertura-collect

In CI/CD processes, test coverage is critical for measuring the quality of our software projects. However, in large projects where tests run in parallel across different modules or even on different machines, multiple Cobertura coverage files may be generated. In such cases, correctly collecting and merging these coverage reports into a single report becomes essential. This is where @yartasdev/cobertura-collect comes into play!

In this post, we'll explore how this npm package can be used in your CI/CD processes and how it merges multiple Cobertura reports into one file.

What is Cobertura?

Cobertura is a popular tool used to measure test coverage in software projects. Although it's commonly used for a lot of projects, it can be integrated with various programming languages and platforms. With Cobertura, you can see how much of your code is covered by each test, allowing you to analyze whether your tests sufficiently control your code. After the testing process, the overall test coverage results are stored in a file called cobertura-coverage.xml.

The Challenge of Merging Cobertura Coverage Files

In CI/CD pipelines, especially in microservice architectures or monorepo projects, tests are often run in parallel. This results in each test group generating its own independent coverage file. When you have multiple Cobertura coverage files, merging them manually into a comprehensive report is time-consuming and error-prone.

This is where @yartasdev/cobertura-collect comes in, merging all the Cobertura coverage files into one.

What is @yartasdev/cobertura-collect?

@yartasdev/cobertura-collect is an npm package that collects all Cobertura coverage files generated after running unit tests in a CI/CD pipeline and merges them into a single cobertura-coverage.xml file. This way, you can easily merge coverage reports from multiple modules into one file and present this combined report to CI/CD tools.

The package’s primary goals are:

  • To locate multiple cobertura-coverage.xml files.
  • To merge these files into one comprehensive coverage report.
  • To provide this merged report to CI/CD tools.

How Does the Package Work?
The package scans a specific directory or directories for Cobertura reports and merges them into a single file. For example, after running parallel test processes, you can use this package to merge all the cobertura-coverage.xml files into one.

Install the Package or use without Install

npm install @yartasdev/cobertura-collect -g

and run command

@yartasdev/cobertura-collect --print -o coverage/cobertura.xml -t coverage/**/cobertura-coverage.xml

or without install

npx @yartasdev/cobertura-collect --print -o coverage/cobertura.xml -t coverage/**/cobertura-coverage.xml

Example CI/CD Configuration

Unit-Test:
  stage: test
  image: node:${NODE_LTS}-alpine
  artifacts:
    when: always
    paths:
      - coverage/
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura.xml
  coverage: /Total Line Coverage:\s*(\d+(?:\.\d+)?)/
  before_script:
    - npm ci --cache .npm --prefer-offline --silent
  script:
    - npx nx run-many --target=test --configuration=ci --with-deps=false --projects=${PROJECTS} --parallel=6 --skip-nx-cache=false --verbose
  after_script:
    - npx @yartasdev/cobertura-collect --print -o coverage/cobertura.xml -t coverage/**/cobertura-coverage.xml
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
Enter fullscreen mode Exit fullscreen mode

Top comments (0)