使用gitlab-ci.yml文件的代码覆盖率报告

问题描述 投票:27回答:4

我需要在Gitlab中看到java maven项目的代码覆盖率报告。根据thisthis和其他一些消息来源:

  1. 我将jacoco添加到pom.xml的插件列表中。
  2. 在我的.gitlab-ci.yml文件中添加了页面作业。
  3. 添加Total.*?([0-9]{1,3})%以在项目设置中进行代码覆盖解析。

但是没有任何报道报道,或者至少我看不到它。没有覆盖百分比或覆盖率报告页面。

.gitlab-ci.yml文件的内容:

image: maven:latest

variables:
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"

cache:
  paths:
    - .m2/repository/

build:
  stage: build
  script:
    - mvn $MAVEN_CLI_OPTS compile

test:
  stage: test
  script:
    - mvn $MAVEN_CLI_OPTS test
  artifacts:
    paths:
      - target/site/jacoco/
pages:
  stage: deploy
  dependencies:
    - test
  script:
   - mkdir public
   - mv target/site/jacoco/index.html public
  artifacts:
    paths:
      - public

deploy:
  stage: deploy
  script:
    - mvn $MAVEN_CLI_OPTS verify
  only:
    - master

jacoco中的pom.xml插件:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.5.201505241946</version>
    <executions>
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

我的项目是gitlab.com的一个私人项目。

管道及其所有4个作业成功通过。

我怎样才能看到报道报道?

maven gitlab code-coverage gitlab-ci jacoco
4个回答
22
投票

您似乎忘记在cat文件中添加对.gitlab-ci.yml的调用。

你应该有类似的东西:

script:
    - mvn $MAVEN_CLI_OPTS test
    - cat target/site/jacoco/index.html

话虽这么说,我认为这不是最好的方法,因为你需要用原始HTML污染你的输出,以便检索所需的覆盖值。

我建议使用此拉取请求中描述的方法:https://github.com/jacoco/jacoco/pull/488

  • 将jacoco部件放在build.xml
  • 使用此awk指令打印正确的代码覆盖率总计: awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print covered, "/", instructions, "instructions covered"; print 100*covered/instructions, "% covered" }' target/site/jacoco/jacoco.csv
  • 将Gitlab CI regexp替换为指令返回的内容:\d+.\d+ \% covered

编辑:

从Gitlab 8.17开始,您可以直接在.gitlab-ci.yml文件中定义正则表达式,如documentation中所述。

这看起来似乎是多余的,但如果此正则表达式现在是存储库历史记录的一部分,则可以将其与用于计算它的其他工具一起更改。


10
投票

GitLab员工在这里。

如果您的管理员设置了GitLab页面,您可以通过(在您的项目上)到Settings - > Pages来查看工件部署到的URL。

你应该看到:

恭喜!您的页面在https://your-namespace.example.com/your-project下提供

点击该链接,你应该好好去!我们还扩展了对HTML工件的支持。 This issue及其相关问题讨论现有和即将推出的功能,这些功能可能会扩展到您在此处构建的内容。


1
投票

添加目标prepare-agent的配置

 <configuration>
 <!-- Sets the path to the file which contains the execution data. -->
 <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
 <!--Sets the name of the property containing the settings
                            for JaCoCo runtime agent.-->
 <propertyName>surefireArgLine</propertyName>
 </configuration>

并且在插件maven-surefire-plugin中也在配置下添加以下属性

<argLine>surefireArgLine</argLine>

关于执行测试目标。报告将生成。生成的jacoco-ut.exec只能使用IDE查看。

这是示例项目https://github.com/r-sreesaran/http-patch-jax-rs

有关更多信息,请参阅https://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/

在.gitlab-ci.yml文件的“artifacts”部分下,根据pom.xml中的路径配置对其进行修改

artifacts:
   paths:
     - target/coverage-reports/

1
投票

除了@SKBo所说的我想添加一个小调整。

cat target / site / jacoco / index.html

将污染你建立输出使得难以阅读重要的东西。

我建议它:

cat your/path/to/jacoco/report/index.html | grep -o '<tfoot>.*</tfoot>'

这将大大减少噪音

© www.soinside.com 2019 - 2024. All rights reserved.