golang集成Coveralls.io持续代码覆盖率跟踪系统插件goveralls的使用

golang集成Coveralls.io持续代码覆盖率跟踪系统插件goveralls的使用

goveralls是Go语言与Coveralls.io持续代码覆盖率跟踪系统的集成工具。

安装

goveralls需要Go 1.13或更高版本。

$ go install github.com/mattn/goveralls@latest

使用

首先你需要获取API token。这个token可以在登录Coveralls.io后,在仓库页面底部找到。每个仓库都有自己独立的token。

$ cd $GOPATH/src/github.com/yourusername/yourpackage
$ goveralls -repotoken your_repos_coveralls_token

你可以设置环境变量$COVERALLS_TOKEN来存储你的token,这样就不需要每次执行都指定token。

你也可以使用-parallel标志或设置环境变量COVERALLS_PARALLEL=true来并行运行这个报告器。

持续集成

不需要单独运行go test,因为goveralls会运行整个测试套件。

GitHub Actions

在GitHub Marketplace上有可用的shogo82148/actions-goveralls。它提供了GitHub Actions YAML配置的简写形式。

name: Quality
on: [push, pull_request]
jobs:
  test:
    name: Test with Coverage
    runs-on: ubuntu-latest
    steps:
    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: '1.16'
    - name: Check out code
      uses: actions/checkout@v2
    - name: Install dependencies
      run: |
        go mod download
    - name: Run Unit tests
      run: |
        go test -race -covermode atomic -coverprofile=covprofile ./...
    - name: Install goveralls
      run: go install github.com/mattn/goveralls@latest
    - name: Send coverage
      env:
        COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: goveralls -coverprofile=covprofile -service=github
    # 或者使用shogo82148/actions-goveralls
    # - name: Send coverage
    #   uses: shogo82148/actions-goveralls@v1
    #   with:
    #     path-to-profile: covprofile

Travis CI

GitHub集成

在GitHub仓库设置中启用Travis-CI。

对于公开的GitHub仓库,使用下面的.travis.yml

language: go
go:
  - tip
before_install:
  - go install github.com/mattn/goveralls@latest
script:
  - $GOPATH/bin/goveralls -service=travis-ci

对于私有的GitHub仓库,使用下面的.travis.yml。如果你使用travis pro,需要指定-service=travis-pro而不是-service=travis-ci

language: go
go:
  - tip
before_install:
  - go install github.com/mattn/goveralls@latest
script:
  - $GOPATH/bin/goveralls -service=travis-pro

Environment variables中存储你的Coveralls API token。

COVERALLS_TOKEN = your_token_goes_here

或者你可以使用travis加密密钥来存储token。

$ gem install travis
$ travis encrypt COVERALLS_TOKEN=your_token_goes_here --add env.global

travis会添加如下env块:

env:
  global:
    secure: xxxxxxxxxxxxx

其他CI系统

$ go install github.com/mattn/goveralls@latest
$ go test -covermode=count -coverprofile=profile.cov
$ goveralls -coverprofile=profile.cov -service=travis-ci

Drone.io

Environment Variables中存储你的Coveralls API token:

COVERALLS_TOKEN=your_token_goes_here

在你的Commands中替换go test行为以下内容:

$ go install github.com/mattn/goveralls@latest
$ goveralls -service drone.io

goveralls会自动使用环境变量COVERALLS_TOKEN作为-repotoken的默认值。

你可以使用-v标志查看测试套件的详细输出:

$ goveralls -v -service drone.io

CircleCI

将你的Coveralls API token存储为环境变量。

在你的circle.yml中的test部分添加以下命令:

test:
  pre:
    - go install github.com/mattn/goveralls@latest
  override:
    - go test -v -cover -race -coverprofile=/home/ubuntu/coverage.out
  post:
    - /home/ubuntu/.go_workspace/bin/goveralls -coverprofile=/home/ubuntu/coverage.out -service=circle-ci -repotoken=$COVERALLS_TOKEN

Semaphore

Environment Variables中存储你的Coveralls API token:

COVERALLS_TOKEN=your_token_goes_here

在你的Commands中替换go test行为以下内容:

$ go install github.com/mattn/goveralls@latest
$ goveralls -service semaphore

goveralls会自动使用环境变量COVERALLS_TOKEN作为-repotoken的默认值。

你可以使用-v标志查看测试套件的详细输出:

$ goveralls -v -service semaphore

Jenkins CI

在Jenkins中添加你的Coveralls API token作为凭证。

然后将其声明为环境变量COVERALLS_TOKEN

pipeline {
    agent any
    stages {
        stage('Test with coverage') {
            steps {
                sh 'go test ./... -coverprofile=coverage.txt -covermode=atomic'
            }
        }
        stage('Upload to coveralls.io') {
            environment {
                COVERALLS_TOKEN     = credentials('coveralls-token')
            }
            steps {
                sh 'goveralls -coverprofile=coverage.txt'
            }
        }
    }
}

也可以让goveralls自行运行代码覆盖率测试,而不提供覆盖率配置文件。

TeamCity

Environment Variables中存储你的Coveralls API token:

COVERALLS_TOKEN=your_token_goes_here

设置构建步骤:

$ go install github.com/mattn/goveralls@latest
$ export PULL_REQUEST_NUMBER=%teamcity.build.branch%
$ goveralls -service teamcity -jobid %teamcity.build.id% -jobnumber %build.number%

goveralls会自动使用环境变量COVERALLS_TOKEN作为-repotoken的默认值。

你可以使用-v标志查看详细输出。

Gitlab CI

将你的Coveralls API token存储为名为COVERALLS_TOKEN的环境变量。

test:
  timeout: 30m
  stage: test
  artifacts:
    paths:
      - coverage.txt
  dependencies:
    - build:env
  when: always
  script:
    - go test -covermode atomic -coverprofile=coverage.txt ./...
    - go install github.com/mattn/goveralls@latest
    - goveralls -service=gitlab -coverprofile=coverage.txt

Coveralls Enterprise

如果你使用Coveralls Enterprise并有自签名证书,需要跳过证书验证:

$ goveralls -insecure

作者

  • Yasuhiro Matsumoto (a.k.a. mattn)
  • haya14busa

许可证

MIT许可证


更多关于golang集成Coveralls.io持续代码覆盖率跟踪系统插件goveralls的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang集成Coveralls.io持续代码覆盖率跟踪系统插件goveralls的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


使用goveralls集成Coveralls.io进行Golang代码覆盖率跟踪

Coveralls.io是一个流行的代码覆盖率跟踪服务,可以帮助团队监控项目的测试覆盖率情况。对于Golang项目,我们可以使用goveralls工具将测试覆盖率数据发送到Coveralls.io

安装goveralls

首先需要安装goveralls工具:

go install github.com/mattn/goveralls@latest

基本使用流程

1. 本地运行测试并生成覆盖率报告

go test -coverprofile=coverage.out ./...

2. 将报告上传到Coveralls.io

goveralls -coverprofile=coverage.out -repotoken=$COVERALLS_TOKEN

集成到CI/CD流程

以下是几种常见CI系统的集成方式:

GitHub Actions集成示例

name: Test and Coverage

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    
    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: '1.20'
    
    - name: Run tests
      run: go test -coverprofile=coverage.out -covermode=count ./...
    
    - name: Upload coverage to Coveralls
      run: |
        go install github.com/mattn/goveralls@latest
        goveralls -coverprofile=coverage.out -service=github
      env:
        COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }}

Travis CI集成示例

language: go

go:
  - "1.20"

script:
  - go test -coverprofile=coverage.out -covermode=count ./...

after_success:
  - goveralls -coverprofile=coverage.out -service=travis-ci

CircleCI集成示例

version: 2
jobs:
  build:
    docker:
      - image: circleci/golang:1.20
    steps:
      - checkout
      - run: go test -coverprofile=coverage.out -covermode=count ./...
      - run: |
          go install github.com/mattn/goveralls@latest
          goveralls -coverprofile=coverage.out -service=circle-ci

高级配置选项

goveralls支持多种配置选项:

# 指定覆盖率文件
goveralls -coverprofile=coverage.out

# 指定服务类型 (github, travis-ci, circle-ci等)
goveralls -service=github

# 指定并行构建的job ID
goveralls -parallel -job=2

# 指定repo token (通常通过环境变量设置)
goveralls -repotoken=$COVERALLS_TOKEN

# 忽略某些目录
goveralls -ignore="vendor/*,examples/*"

在代码中添加Coveralls徽章

获取项目在Coveralls.io上的徽章Markdown代码,添加到README.md中:

Coverage Status


## 常见问题解决

1. **覆盖率报告为0%**:
   - 确保测试文件以`_test.go`结尾
   - 检查`-coverprofile`参数是否正确生成
   - 确保测试实际运行(可以添加`-v`参数查看详细输出)

2. **认证失败**:
   - 确认`COVERALLS_TOKEN`环境变量设置正确
   - 对于公开仓库,Travis CI和GitHub Actions通常不需要手动设置token

3. **并行构建问题**:
   - 对于并行测试,使用`-parallel`和`-job`参数
   - 确保所有job完成后合并覆盖率报告

通过以上步骤,你可以轻松地将Coveralls.io集成到Golang项目中,持续跟踪代码覆盖率变化,提高代码质量。
回到顶部