MakefileでGoテストカバレッジコマンドを自動化
Go
Makefile
テスト
知識
運用
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環境用:
makefile
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別に自動でブラウザを開く処理:
makefile
@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"; \
figitignoreに追加すべきファイル:
- coverage.out
- coverage.html