go-test-coverage

command module
v2.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

README

go-test-coverage

test action-test lint coverage Go Report Card Release

go-test-coverage is tool which reports issues when test coverage is below set threshold.

Why?

These are the most important features and benefits of go-test-coverage:

  • easy installation - ready in 5 minutes
  • server-less
    • no registration, no permissions needed
    • check never fails due to connectivity/server issues
  • protects privacy - will never leak any information about your private projects to third parties
  • supports usage in local and CI environments
  • comprehensive configuration options
  • fancy badges

Usage

go-test-coverage can be used in two ways:

  • as local tool, and/or
  • as step of github workflow

It is recommended to have both options in go repositories.

Local tool

Example of Makefile which has check-coverage command that runs go-test-coverage locally:

GOBIN ?= $$(go env GOPATH)/bin

.PHONY: install-go-test-coverage
install-go-test-coverage:
	go install github.com/vladopajic/go-test-coverage/v2@latest

.PHONY: check-coverage
check-coverage: install-go-test-coverage
	go test ./... -coverprofile=./cover.out -covermode=atomic
	${GOBIN}/go-test-coverage --config=./.testcoverage.yml
Github Workflow

Example to run go-test-coverage as step of workflow:

name: Go test coverage check
runs-on: ubuntu-latest
steps:
  - uses: actions/checkout@v3
  - uses: actions/setup-go@v3
  
  - name: generate test coverage
    run: go test ./... -coverprofile=./cover.out

  - name: check test coverage
    uses: vladopajic/go-test-coverage@v2
    with:
      # Configure action using config file (option 1)
      config: ./.testcoverage.yml
      
      # Configure action by specifying input parameters individually (option 2)
      profile: cover.out
      local-prefix: github.com/org/project
      threshold-file: 80
      threshold-package: 80
      threshold-total: 95
Config

Example of .testcoverage.yml config file:

# (mandatory) 
# Path to coverprofile file (output of `go test -coverprofile` command)
profile: cover.out

# (optional) 
# When specified reported file paths will not contain local prefix in the output
local-prefix: "github.com/org/project"

# Holds coverage thresholds percentages, values should be in range [0-100]
threshold:
  # (optional; default 0) 
  # The minimum coverage that each file should have
  file: 80

  # (optional; default 0) 
  # The minimum coverage that each package should have
  package: 80

  # (optional; default 0) 
  # The minimum total coverage project should have
  total: 95

# Holds regexp rules which will override thresholds for matched files or packages
override:
  # Increase coverage threshold to 100% for `foo` package (default is 80, as configured above)
  - threshold: 100
    path: ^pkg/lib/foo

# Holds regexp rules which will exclude matched files or packages from coverage statistics
exclude:
  # Exclude files or packages matching their paths
  paths:
    - \.pb\.go$    # excludes all protobuf generated files
    - ^pkg/bar     # exclude package `pkg/bar`
 
# NOTES:
# - symbol `/` in all path regexps will be replaced by
#   current OS file path separator to properly work on Windows

Coverage Badge

Repositories which use go-test-coverage action in their workflows could easily create beautiful coverage badge and embed them in markdown files (eg. coverage).

Read instructions on creating coverage badge here.

Contribution

All contributions are useful, whether it is a simple typo, a more complex change, or just pointing out an issue. We welcome any contribution so feel free to open PR or issue.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL