Github 集成 Codecov 做测试覆盖率

本文介绍如何使用Travis CI、Codecov和Maven自动收集并上传测试覆盖率报告。通过配置pom.xml中的jacoco插件和.travis.yml中的Codecov上传指令,实现每次构建后自动更新Codecov上的覆盖率信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先贴一下官网的快速开始

Getting Started

Basic Usage
  1. Sign up on codecov.io and link either your GitHub, GitLab, or Bitbucket account.

  2. Once linked, Codecov will automatically sync all the repositories to which you have access.

  3. You can click on an organization from your dashboard to access its repositories, or navigate directly to a specific repository using: https://codecov.io/<repo-provider>/<account-name>/<repo-name>. Example: https://codecov.io/gh/scrapy/scrapy.
    A repository that has no coverage reports uploaded to codecov. Note that the repository upload token is displayed here.
    A repository that has no coverage reports uploaded to codecov. Note that the repository upload token is displayed here.

  4. Use the bash uploader and a repository upload token, to upload a coverage report to Codecov.

  5. Navigate back to the repository on Codecov, and you should see coverage information.

可以看见原理很简单,主动把测试覆盖率的报告通过Codecov提供的脚本(bash uploader)上传就行了(bash <(curl -s https://codecov.io/bash)),如果是私有仓库,则上传的时候需要添加上图中的token。

下面说说Travis + Codecov + Maven

这里需要利用到一个覆盖率的包jacoco:

  1. pom.xml添加jacoco的maven插件:
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.9</version>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  1. .travis.yml增加codecov的上传配置:
language: java
after_success:
  - bash <(curl -s https://codecov.io/bash)
  1. 提交代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值