ユーザーアイコン

mizuko

4日前

0
0

MakefileでGoテストカバレッジコマンドを自動化

Go
Makefile
テスト

Makefileにカバレッジ関連コマンドを追加して効率化する方法。

通常環境用:

test-coverage: go test -v -coverprofile=coverage.out ./... go tool cover -func=coverage.out test-coverage-html: go test -v -coverprofile=coverage.out ./... go tool cover -html=coverage.out -o coverage.html @echo "Coverage report generated: coverage.html"

Docker環境用:

test-coverage-local: docker exec -i paput-api ash -c "go test -v -coverprofile=coverage.out ./... && go tool cover -func=coverage.out" test-coverage-html-local: docker exec -i paput-api ash -c "go test -v -coverprofile=coverage.out ./... && go tool cover -html=coverage.out -o coverage.html"

OS別に自動でブラウザを開く処理:

@if [ "$$(uname)" = "Darwin" ]; then \ open coverage.html; \ elif [ "$$(uname)" = "Linux" ]; then \ xdg-open coverage.html 2>/dev/null || echo "Please open coverage.html manually"; \ elif [ "$$(expr substr $$(uname -s) 1 5)" = "MINGW" ]; then \ start coverage.html; \ else \ echo "Please open coverage.html manually"; \ fi

gitignoreに追加すべきファイル:

  • coverage.out
  • coverage.html