Skip to content

Instantly share code, notes, and snippets.

@magickatt
Created November 8, 2024 15:45
Show Gist options
  • Save magickatt/e96accd2c55dcc44e07d404c8a1975a2 to your computer and use it in GitHub Desktop.
Save magickatt/e96accd2c55dcc44e07d404c8a1975a2 to your computer and use it in GitHub Desktop.
Merge Golang unit and integration test code coverage using GitHub Actions
name: CI
jobs:
unit-tests:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache-dependency-path: go.sum
- name: Run tests
run: go test -covermode count ./... -test.gocoverdir="$PWD/test/coverage/unit"
- name: Store test coverage
uses: actions/upload-artifact@v4
with:
name: unit-test-coverage
path: test/coverage/unit
retention-days: 1
integration-tests:
name: Integration Tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache-dependency-path: go.sum
- name: Run tests
run: go test -covermode count -tags=integration ./... -test.gocoverdir="$PWD/test/coverage/integration"
- name: Store test coverage
uses: actions/upload-artifact@v4
with:
name: integration-test-coverage
path: test/coverage/integration
retention-days: 1
code-coverage:
name: Code Coverage
runs-on: ubuntu-latest
needs: [unit-tests, integration-tests]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache-dependency-path: go.sum
- name: Download unit test coverage
uses: actions/download-artifact@v4
with:
name: unit-test-coverage
path: test/coverage/unit
run-id: ${{github.run_id}}
- name: Download integration test coverage
uses: actions/download-artifact@v4
with:
name: integration-test-coverage
path: test/coverage/integration
run-id: ${{github.run_id}}
- name: Make combined test coverage directory
run: mkdir test/coverage/combined
- name: Merge unit and integration test coverage directories
run: go tool covdata merge -i=test/coverage/unit,test/coverage/integration -o=test/coverage/combined
- name: Convert coverage directory into legacy text format
run: go tool covdata textfmt -i=test/coverage/combined -o=test/coverage/coverage.out
- name: Generate code coverage report and badge
if: ${{ github.ref == 'refs/heads/main' }}
uses: ncruces/go-coverage-report@v0
with:
coverage-file: test/coverage/coverage.out
report: true
amend: true
continue-on-error: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment