I want to get the test coverage and compare with user defined threshold. I tried below code in makefile I am referring this link. It is written in .yml file but I am trying to write it in a Makefile.
.PHONY: lint
testcoverage=$(go tool cover -func coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+')
echo ${testcoverage}
if (${testcoverage} -lt 50 ); then \
echo "Please add more unit tests or adjust threshold to a lower value."; \
echo "Failed"
exit 1
else \
echo "OK"; \
fi
It does not print anything on echo ${totaltestcoverage}
and gives answer OK even if my totaltestcoverage is 40.
Can anyone please help me with a better way to get the test coverage and compare with user defined threshold?
Thanks in advance.
You can try this
.PHONY: lint
testcoverage := $(shell go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+')
threshold = 50
test:
@go test -coverprofile=coverage.out -covermode=count ./...
check-coverage:
@echo "Test coverage: $(testcoverage)"
@echo "Test Threshold: $(threshold)"
@echo "-----------------------"
@if [ "$(shell echo "$(testcoverage) < $(threshold)" | bc -l)" -eq 1 ]; then \
echo "Please add more unit tests or adjust the threshold to a lower value."; \
echo "Failed"; \
exit 1; \
else \
echo "OK"; \
fi