Demystifying Coverage and cobertura.xml

I’ve been struggling for a while with trying to get coverage to show up. (We are running 16.8 / Premium)

I’m using golang

Here is my XML file after a variety of sed commands

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
<coverage line-rate="0.4534198" branch-rate="0" version="" timestamp="1708565970368" lines-covered="769" lines-valid="1696" branches-covered="0" branches-valid="0" complexity="0">
  <sources>
    <source>.</source>
  </sources>
  <packages>
    <package name="mypackage/cmd/echoServer" line-rate="0.7227723" branch-rate="0" complexity="0">
      <classes>
        <class name="-" filename="cmd/echoServer/echoServer.go" line-rate="0.7227723" branch-rate="0" complexity="0">
          <methods>
            <method name="customContextFunction" signature="" line-rate="1" branch-rate="0" complexity="0">

I’m not sure if the package name matters because according to the docs it going to combine

sources with the filename fields.

So my file is called: cmd/echoServer/echoServer.go and I’m assuming since source is . this should render correctly?

Am I correct here? (because its not working :slight_smile: )

Question 1: I’m on a ci branch - do I need to merge back into master for this to work?
Question 2: Is my <source> <filename> combo correct?
Question 3: Is there any way to view the gitlab logs to see if this is being processed correctly?
Question 4: If we are paid subscribers - is there a better way to get support?
Question 5: Is there a sample repo available to test out the coverage feature? This would be very helpful

Hi,

It would be nice if you could in the future use the predefined template when posting a question :slight_smile: E.g. it’s hard to tell anything because you didn’t post your .gitlab-ci.yml file (or relevant part of it).

Btw what exactly do you mean by “coverage” → is it a number (percentage) or you want code coverage visualization? For both you need separate configuration.

Q1: Depends on your config in .gitlab-ci.yml and how you want it to work. It does not need to be on master.
Q2: GitLab does not care about this. You have to parse (regex) your result file anyways additionally as described here and in the YAML definition
Q3: Not sure.
Q4: Yes. You have Customer support according to your tier and you can reach out by opening a support ticket: https://support.gitlab.com/hc/en-us
Q5: There is an example of code coverage visualization: GitLab.org / ci-sample-projects / Coverage Report · GitLab , for collecting coverage number I didn’t find, but it’s really just parsing the number from the file correctly using regex.

Hope this helps :slight_smile:

I didn’t post my .gitlab CI because I thought it would be irrelevant but here are the relevant pieces …

  script:
    - make dirs unit-test integrate merge cobertura
  coverage: '/^coverage: (\d+.\d+)% of statements$/'
  artifacts:
    when: always
    paths:
      - coverage/unit-int-coverage-count.xml
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage/unit-int-coverage-count.xml

The snippet I posted above was from my unit-int-coverage-count.xml file.

I’m confused because the docs seem to imply I can see inline code coverage as well as coverage in MR’s but I cant seem to get them to show up.

Okay, so you are solving the “visualization problem”? :slight_smile:

Note: It works (is visible) ONLY in MRs. Not when you push a file to a branch.

Generally, CI Config looks good to me. So I believe it’s just about your xml file.

In order to get visualization, you need to have information about all the lines in correct (cobertura) format. When this is correct, you should see visualization like on the sample project.

I just checked my coverage.xml (auto-generated by Python’s pytest), and it could be you should change your <source></source> path. What I have is:

<?xml version="1.0" ?>
<coverage version="7.2.7" timestamp="1691523510987" lines-valid="81" lines-covered="81" line-rate="1" branches-covered="0" branches-valid="0" branch-rate="0" complexity="0">
	<!-- Generated by coverage.py: https://coverage.readthedocs.io/en/7.2.7 -->
	<!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd -->
	<sources>
		<source>/builds/group_name/project_name/path_to_directory_from_which_I_execute_tests</source>
	</sources>
	<packages>
		<package name="app" line-rate="1" branch-rate="0" complexity="0">
			<classes>
				<class name="crud.py" filename="app/crud.py" complexity="0" line-rate="1" branch-rate="0">
					<methods/>
					<lines>
						<line number="1" hits="1"/>
						<line number="2" hits="1"/>
						<line number="4" hits="1"/>
						<line number="6" hits="1"/>
						<line number="7" hits="1"/>
						<line number="9" hits="1"/>
						<line number="10" hits="1"/>
					</lines>
				</class>
...

So I believe it depends on project structure. builds/group_name/project_name is normally default build location in CI (CI_BUILDS_DIR variable), so you could try setting that one. Then all the other package names should be relative to that.

Can you also link to documentation pages that you’ve been researching so far?

This is the blurb I’m trying to decode.

If my file element is:

        <class name="-" filename="cmd/echoServer/echoServer.go" line-rate="0.7227723" branch-rate="0" complexity="0">

And my sources are:

<source>.</source>

And my file is physically at

/cmd/echoServer/echoServer.go I should get line-coverage…

This is what I’m trying to understand …