6

I am trying to show test coverage visualization in Gitlab for our monorepo as described here Test Coverage Visualization

We are using a self-managed gitlab runner with the docker+machine executor hosted on AWS EC2 instances. We are using Gitlab SaaS. The job from the gitlab-ci.yml is below

sdk:
  stage: test
  needs: []
  artifacts:
    when: always
    reports:
      cobertura: /builds/path/to/cobertura/coverage/cobertura-coverage.xml
  <<: *main
  <<: *tests
  <<: *rules
  <<: *tags

The line in the script that runs the tests and outputs code coverage...

- npm run test -- --watchAll=false --coverage --coverageReporters=cobertura

The artifact gets saved just fine and looks normal when I download it, but I don't get any visualization as described in the documentation linked above. I just updated the gitlab runner to V14.0.0 thinking that might be the problem, it's not.

I don't have any sort of regex pattern setup, as from my understanding that is only for printing the coverage to stdout.

I'm sure I am overlooking something trivial and I really need a sanity check here as I have already spent way more time on this than I can afford.

Benjamin
  • 526
  • 6
  • 16
  • Can you provide an extract of the coverage report? Often it's a class or source issue. It should match what's in the docs: https://docs.gitlab.com/ee/user/project/merge_requests/test_coverage_visualization.html#path-correction-example – Arty-chan Dec 05 '21 at 06:39

2 Answers2

0

Personally I was trying to get coverage information under the following conditions:

  • Python
  • Using pytest and pytest-cov

I found this worked for me:

test:
  # stage: test
  # image: python:3
  script:
    # [...] (building and installing my package)
    # install testing packages
    - pip install pytest pytest-cov
    # run coverage test
    - coverage run -m pytest tests/
    - coverage report
    - coverage xml
  # enable merge request test coverage results
  # see: https://docs.gitlab.com/ee/ci/pipelines/settings.html#add-test-coverage-results-using-coverage-keyword
  # see: https://docs.gitlab.com/ee/ci/testing/test_coverage_visualization.html#python-example
  coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/'
  artifacts:
    paths:
      - coverage.xml
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml

What finally helped me was seeing this python example which is surrounded by a bunch of examples for other languages too. I also get a feeling I may have broken things by initially missing the quotation marks around the regex in the coverage: bit

-1

The issue was that the regex pattern needed to be set in the repository settings. I had experimented with adding a regex pattern, but it hadn't worked by the time I posted this question because the regex pattern I was using was not correct.

Benjamin
  • 526
  • 6
  • 16