I was able to resolve this issue.

The Gitlab badge for test coverage will be scanning the latest successful pipeline logs for a specified branch (in my case a feature branch).

However, in my case, the Gitlab CI stage "test" had an only restriction in that it will only trigger on the master branch. So, I was previously creating a MR with this branch, and then manually triggering the pipeline. This will not classify as a pipeline running on a branch. Instead, Gitlab sees this as running on a merge request pipeline.

The solution to this was to either remove the restriction that the CI stage will only run on the master branch by removing that line of code within Gitlab CI. Or in my case, whitelisting the name of the feature branch to the list in the only field. Now, when you push a new change to the branch, the pipeline should automatically trigger (even without a MR open) and if you have the test coverage total amount printed out in the logs, along with the relevant regex, then the data should be able to be caught by the Gitlab Badge.

Answer from codingdictionary on Stack Overflow
Discussions

Coverage badge sourced from downstream job displays "unknown"
I have set up a pipeline that generates some gitlab-ci configuration and than triggers all jobs defined in it. There are currently four jobs triggered and all run successfully: Three of them provide coverage information which is correctly parsed and understood by the GitLab CI. More on forum.gitlab.com
🌐 forum.gitlab.com
5
0
December 6, 2021
Coverage badge unknown
Hi all. I have a Ruby repository on github, and I have set up the code coverage with Simplecov. I seem to be unable to show the coverage percentage on a badge though. I followed this tutorial here: So I have this set into my .gitlab-ci.yml rspec: stage: test script: - rspec artifacts: paths: ... More on forum.gitlab.com
🌐 forum.gitlab.com
3
0
April 27, 2018
Code coverage bage not from latest pipeline
Have the exact same problem and hoping for an answer. More on reddit.com
🌐 r/gitlab
2
5
May 30, 2022
repository - Coverage badge in gitlab is unknown - Stack Overflow
I am trying to setup a coverage badge for a python project on GitLab. I was following this question but it is still not working. Currently I see in "CI/CD"/jobs page this: But when I go to More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitLab
forum.gitlab.com › gitlab ci/cd
Coverage badge sourced from downstream job displays "unknown" - GitLab CI/CD - GitLab Forum
December 6, 2021 - I have set up a pipeline that generates some gitlab-ci configuration and than triggers all jobs defined in it. There are currently four jobs triggered and all run successfully: Three of them provide coverage informat…
🌐
GitLab
forum.gitlab.com › gitlab ci/cd
Coverage badge unknown - GitLab CI/CD - GitLab Forum
April 27, 2018 - Hi all. I have a Ruby repository on github, and I have set up the code coverage with Simplecov. I seem to be unable to show the coverage percentage on a badge though. I followed this tutorial here: So I have this s…
🌐
Reddit
reddit.com › r/gitlab › code coverage bage not from latest pipeline
r/gitlab on Reddit: Code coverage bage not from latest pipeline
May 30, 2022 -

I've got badge like

![coverage](https://gitlab.com/gitlab-org/gitlab/badges/main/coverage.svg?job=test:coverage)

As you can see, I've got job named "test:coverage", which has coverage report.

Problem is, this job doesn't appear in every pipeline. This job only runs once a day in scheduled pipeline. When this scheduled pipeline IS the latest pipeline, badge works just fine. But when it can't find this "test:coverage" job in the latest pipeline, it shows "unknown".

Does anybody knows some way to force Gitlab to look for job not only in latest pipeline?

🌐
GitLab
gitlab.gnome.org › librsvg › #847
Coverage badge says "unknown" (#847) · Issues · GNOME / librsvg · GitLab
February 18, 2022 - Coverage badge says "unknown" The coverage badge ![coverage badge image](https://gitlab.gnome.org/GNOME/librsvg/badges/main/coverage.svg) says "unknown", even though coverage jobs report the value correctly; the job page itself actually picks up the value correctly.
Find elsewhere
🌐
GitLab
forum.gitlab.com › how to use gitlab
Build shows test coverage but Badge says Coverage unknown - How to Use GitLab - GitLab Forum
August 4, 2019 - Hi, i have setup showing Test Coverage for a Gitlab Project build with Maven and Junit. I did it like described in this article https://medium.com/@kaiwinter/javafx-and-code-coverage-on-gitlab-ci-29c690e03fd6. The build shows the test coverage but the link to the badge says the coverage is unknown. ...
🌐
GitLab
gitlab.com › gitlab.org › #368355
GitLab Code coverage statistics data is empty and Coverage report shows `coverage: unknown` badges (#368355) · Issues · GitLab.org / GitLab · GitLab
July 19, 2022 - I'm using Jacoco for code coverage and visualizing it, visualizing is working well but Code coverage statistics data is empty and the coverage badge is unknown. The gitlab-ci...
🌐
Frankysnotes
frankysnotes.com › 2025 › 07 › why-your-net-code-coverage-badge-is.html
Franky's Notes: Why Your .NET Code Coverage Badge is 'Unknown' in GitLab (And How to Fix It)
One thing I initially thought was that the regex used to extract the coverage was incorrect. The regex used in the pipeline was: ... That regex came directly from the GitLab documentation, so I thought it should work correctly.
Top answer
1 of 5
20

I finally got the coverage badge displaying a percentage instead of unknown today for my python project. Here's the relevant content from my .gitlab-ci.yml:

job:
 script:
  - 'python -m venv venv'
  - '.\venv\Scripts\activate'
  - 'python -m pip install -r requirements.txt'
  - 'coverage run --source=python_project -m unittest discover ./tests'
  - 'coverage report --omit=things_that_arent_mine/*'
  - 'coverage xml'
 artifacts:
  reports:
   cobertura: 'coverage.xml'

I'm also using the regex for gcovr listed in the repo CI/CD Settings > General Pipelines > Test coverage parsing which I found after reading this as well as the second to last comment on this:

^TOTAL.*\s+(\d+\%)$

In the repo General Settings > Badges, my badge link is:

http://gitlab-server/%{project_path}/-/jobs

and my badge image url is:

http://gitlab-server/%{project_path}/badges/%{default_branch}/coverage.svg

I don't quite know what the Cobertura report artifact is for (I think it specifically has to do with merge requests) but I have it in there because the tutorials took me down that road. I did confirm that removing the following from the .gitlab-ci.yml doesn't break the badge or coverage number on the jobs page:

  - 'coverage xml'
 artifacts:
  reports:
   cobertura: 'coverage.xml'

While removing or commenting:

- 'coverage report --omit=things_that_arent_mine/*'

does break the badge as well as the coverage number displayed on the CI/CD jobs page of the repo. I also tried some regex variations that I tested in rubular but the only one that didn't cause gitlab to barf was the gcovr one.

Hopefully this helps out, it was kind of difficult and arduous to piece together what was needed for this badge to work on a python project.

EDIT: Also just figured out how to add some sexy precision to the coverage percentage number. If you change the coverage report line of the .gitlab-ci.yml to:

- 'coverage report --omit=things_that_arent_mine/* --precision=2'

And the regex in CI/CD Settings > General Pipelines > Test coverage parsing to:

^TOTAL.+?(\d+.\d+\%)$

That should give you a very precise coverage number that effectively no one but you and I will care about. But by gosh we'll know for sure if coverage is 99.99% or 100%.

2 of 5
6

A solution that worked for me:

In the .gitlab-ci.yml file, in the job that runs the coverage report, I added the following lines:

  script:
    # some lines omitted for brevity
    - coverage report --omit=venv/*
  coverage: '/TOTAL.*\s+(\d+\%)/'

In particular, I couldn't get the badge to show anything but unknown until I added the coverage directive to my test job. Bear in mind, different tools may print different output and so you will likely have to change the regular expression, as I did.

Top answer
1 of 3
20

I was struggling with this too for a python project and found that the test coverage results is now deprecated (https://docs.gitlab.com/ee/ci/pipelines/settings.html#add-test-coverage-results-to-a-merge-request-deprecated)

The answer for me was to use the coverage keyword in my .gitlab-ci.yml with the required regex to extract the percentage from the job log

e.g.

test:
  stage: test
  extends: .python-reports
  script:
    - pipenv run pytest --cov --cov-report term-missing --cov-report xml:./coverage_out_report.xml --junitxml=./test_out_report.xml
  coverage: '/TOTAL.*\s+(\d+%)$/'

And my badge defined as

Link https://gitlab.com/%{project_path}/-/commits/%{default_branch}

Badge image URL https://gitlab.com/%{project_path}/badges/%{default_branch}/coverage.svg

Now I see the coverage as 100% on main (or master) and a nice green badge

2 of 3
12

I had the same issue in a different stack (Java, Gradle, and Jacoco). I've already solved the issue, and I will share what I've done. I hope my solution gives you some idea.

First, In your project, go to Settings > CI/CD and expand the General pipelines section. Fill the Test coverage parsing textbox with an appropriate regular expression based on the tool you are using. You can find the sample list of these regular expressions on this page: Test coverage parsing samples

Gitlab expects that the test runner tool will print the test coverage percent based on the given regular expression somewhere on the CI job's log. If the test runner engine doesn't print it to the console, you can do it manually. For instance:

test:
  stage: test
  script:
    - gradle test jacocoTestReport --info
    # Print test coverage to console
    - cat build/reports/jacoco/test/html/index.html | grep -o 'Total[^%]*%'
  artifacts:
    paths:
      - build/reports/jacoco/test/*

After making sure that the test coverage exists in the log, the badge will work perfectly.

pipeline and coverage badge sample

Top answer
1 of 1
1

Gitlab CIs Test coverage report badge shows unknown when no coverage information was obtained.

This is certainly the error condition, e.g. you expect to have at least some different match here (good / acceptable / medium / low).

For it to work, the job log must contain at least one line (the last would be taken) that matches the regular expression form the job keywords coverage key word.

You coverage regular expression is:

/^\s*Lines:\s*\d+.\d+\%/

This regular expression has to match the coverage percentage number (incl. zero) and the match may contain surrounding text.

The actual number for the badge is matched by \d+(\.\d+)? on your regular expression match.

The most detailed description about how this regular expression works can be obtained from the coverage key word documentation.

As you have not shared any job log that you expect your regular expression to match against not much specifically can be said.

It appears as a configuration issue to me, and it is either that the expectation is wrong that it would match against the job log output while it does not (you can easily validate this with a regular expression test tool either on your box or online at https://regex101.com/) - or - there is a match but the matched number can not be parsed by Gitlab CI (which from my own feeling is less likely).

If you have a match of a line of your job log, then verify that match as the subject against the match of another regular expression, \d+(\.\d+)?, as that is the one Gitlab uses to obtain the actual number.

🌐
Patricksoftwareblog
patricksoftwareblog.com › add_badges_to_a_gitlab_project.html
Add Badges to a Python Project in GitLab | Patrick's Software Blog
October 26, 2021 - Start with a Python project in GitLab that doesn’t have any badges: In the left-hand navigation pane, scroll down to the bottom and select ‘Settings’ –> ‘General’: Scroll down to the ‘Badges’ sections and click on ‘Expand’: ... However, the ‘Test Coverage’ badge is still reporting an "unknown" coverage...
🌐
GitLab
forum.gitlab.com › gitlab ci/cd
Coverage Badge shows "unknown", but artefact available - GitLab CI/CD - GitLab Forum
September 14, 2018 - Hi, first of all excuse my bad English - I’m not a native English speaker. I’m new to gitlab and new to CI. I’m running a pipeline on a nodejs project using nyc Istanbul for code-coverage. Works fine and the job code quality job creates a json artefact (coverage) which I can download.