相关文章推荐
听话的牛肉面  ·  Import and migrate ...·  3 天前    · 
打篮球的蛋挞  ·  Maui - how to use ...·  3 周前    · 
成熟的饭盒  ·  js findIndex ...·  8 月前    · 
How test coverage visualization works
  • Example test coverage configurations
  • Troubleshooting
  • Test coverage visualization

    Version history Introduced in GitLab 12.9.
  • Feature flag removed in GitLab 13.5.
  • With the help of GitLab CI/CD , you can collect the test coverage information of your favorite testing or coverage-analysis tool, and visualize this information inside the file diff view of your merge requests (MRs). This allows you to see which lines are covered by tests, and which lines still require coverage, before the MR is merged.

    Test Coverage Visualization Diff View

    How test coverage visualization works

    Collecting the coverage information is done via GitLab CI/CD’s artifacts reports feature . You can specify one or more coverage reports to collect, including wildcard paths. GitLab then takes the coverage information in all the files and combines it together. Coverage files are parsed in a background job so there can be a delay between pipeline completion and the visualization loading on the page.

    For the coverage analysis to work, you have to provide a properly formatted Cobertura XML report to artifacts:reports:coverage_report . This format was originally developed for Java, but most coverage analysis frameworks for other languages have plugins to add support for it, like:

    Other coverage analysis frameworks support the format out of the box, for example:

    Once configured, if you create a merge request that triggers a pipeline which collects coverage reports, the coverage is shown in the diff view. This includes reports from any job in any stage in the pipeline. The coverage displays for each line:

      covered (green): lines which have been checked at least once by tests
    • no test coverage (orange): lines which are loaded but never executed
    • no coverage information: lines which are non-instrumented or not loaded

    Hovering over the coverage bar provides further information, such as the number of times the line was checked by tests.

    Uploading a test coverage report does not enable:

    You must configure these separately.

    Limits

    A limit of 100 <source> nodes for Cobertura format XML files applies. If your Cobertura report exceeds 100 nodes, there can be mismatches or no matches in the merge request diff view.

    A single Cobertura XML file can be no more than 10 MiB. For large projects, split the Cobertura XML into smaller files. See this issue for more details. When submitting many files, it can take a few minutes for coverage to show on a merge request.

    The visualization only displays after the pipeline is complete. If the pipeline has a blocking manual job , the pipeline waits for the manual job before continuing and is not considered complete. The visualization cannot be displayed if the blocking manual job did not run.

    Artifact expiration

    By default, the pipeline artifact used to draw the visualization on the merge request expires one week after creation.

    Coverage report from child pipeline

    Version history Introduced in GitLab 15.1 with a flag named ci_child_pipeline_coverage_reports . Disabled by default.
  • Enabled on GitLab.com and self-managed and feature flag ci_child_pipeline_coverage_reports removed in GitLab 15.2.
  • If a job in a child pipeline creates a coverage report, the report is included in the parent pipeline’s coverage report.

    child_test_pipeline:
      trigger:
        include:
          - local: path/to/child_pipeline_with_coverage.yml
    

    Automatic class path correction

    The coverage report properly matches changed files only if the filename of a class element contains the full path relative to the project root. However, in some coverage analysis frameworks, the generated Cobertura XML has the filename path relative to the class package directory instead.

    To make an intelligent guess on the project root relative class path, the Cobertura XML parser attempts to build the full path by:

    • Extracting a portion of the source paths from the sources element and combining them with the class filename path.
    • Checking if the candidate path exists in the project.
    • Using the first candidate that matches as the class full path.

    Path correction example

    As an example, a C# project with:

    The parser:

    Automatic class path correction also works for a Java project with: