How to trigger Coverage badge and Test Coverage Visualization with cobertura coverage xml?

I’m trying to trigger proper coverage badge generation and test coverage visualization for one of my pet project.

This is my current config file.

I have successfully generated a Cobertura format xml for my coverage that stores in tests/_output/cobertura.xml. And I can also see my artifacts setting work there:

I’ve merged it into my main branch. Everything seems to work except the coverage badge is still
2022-01-15 23-27-51 的螢幕擷圖.

Also, the Cobertura xml file generated seems quite normal to me.

What did I miss?

@yookoala Thanks for the post and welcome to the forum!

I’m glad to see you got the text coverage visualization working!

To get the badge working you still need to setup parsing for the coverage value from the logs as described here in the documentation. Looking at your latest Merge Request adding ^\s*Lines:\s*\d+.\d+\% should work and then the badge will start to work once a pipeline has completed.

I hope this helps!

-James H, GitLab Product Manager, Verify:Pipeline Execution

Thanks! Did the following and the badge works:

  1. Added an extra grep to show the text coverage text without ANSI (I want colour for my test output).
  2. Add the coverage regex as mentioned.

Thanks for the help.

1 Like

@jheimbuck_gl I don’t see anything in that link about

parsing for the coverage value from the logs.

Can you be more specific?

@gabriel.co it looks like the docs changed since this post.

Searching the docs for “Code Coverage” leads me to this: Code coverage | GitLab

Hope that helps!

-James H, GitLab Product Manager, Analytics:Product Analytics

OK, I found it, and it works, but here’s some feedback:

  1. It’s quite confusing that you need to change two different sections of the job configuration to enable the code coverage features of GitLab. Some features are enabled by the coverage attribute and the corresponding regex, and others are enabled by the coverage report under artifacts.
  2. It also doesn’t make a lot of sense that only one format (Cobertura) is supported for the report and multiple are supported for the regex.
1 Like

Hi,
All links mentioned above have been moved not sure why. Can someone please help me understand what else I need to do to show up code coverage in pipeline as well as at project level. My unit test job is shared below

unit_test:
  stage: test
  when: manual
  interruptible: true
  allow_failure: false
  image: <image with gradle binary>
  script:
  - "./gradlew clean check test jacocoTestReport"
  - cat build/jacocoHtml/index.html | grep -o 'Total[^%]*%' | sed 's/<.*>/ /; s/Total/Jacoco Coverage Total:/'
  except:
    refs:
      - main
  only:
    variables:
      - $PIPELINE_TYPE == "ci_build"
  artifacts:
    name: test-report-$CI_COMMIT_REF_SLUG
    expire_in: 1 day
    reports:
      junit: build/test-results/*/TEST-*.xml
    paths:
    - build/reports/tests
    - build/reports/jacoco/test
    - build/jacocoHtml
    - build/*
    when: always
  coverage: '/TOTAL.*?([0-9]{1,3})%/'

I have gone through this youtube video and tried finding these settings in gitlab but can’t find ci/cd pipeline settinsg to add code coverage parsing. Could you please point me to docs with instructions
Pipeline view:

settings → ci/cd → general pipelines(Expand) → scroll down to ‘Test coverage parsing’

Add regex for java: Total.*?([0-9]{1,3})%

save changes

Project level view:

settings → general-> Badges → fill below

Name: Coverage
Badge Image URL: https://gitlab.com/%{project_path}/badges/{default-branch}/coverage.svg?style=flat

FTR. It is resolved. I didnt need to make any changes except adding badge in general settings.
Mistake was incorrect regex in job config.
I had added TOTAL(capital case) in regex which was not matching job output. After fixing regex to below fixed the issue.

coverage: '/Total.*?([0-9]{1,3})%/'