我想要获得测试覆盖率,并与用户定义的阈值进行比较.我在Makefile中try 了下面的代码,我引用了这个link.它是在.yml文件中编写的,但我正在try 将其写入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

它不会在echo ${totaltestcoverage}上打印任何内容,即使我的总覆盖率是40,它的答案也是OK.

有谁能帮我找到一种更好的方法来获得测试覆盖率,并与用户定义的阈值进行比较?

先谢谢你.

推荐答案

你可以试试这个

.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

Go相关问答推荐

如何创建两个连接的io.ReadWriteClosers以用于测试目的

Golang ==错误:OCI运行时创建失败:无法启动容器进程:exec:./" bin:stat./" bin:没有这样的文件或目录:未知

如何预编译Golang标准库?

如何使用gopher-lua定义一个Lua函数,该函数有一个预定义的表作为param,Lua脚本可以在其中访问该函数中的表?

在nixos上找不到XInput2.h头文件的包

迭代字符串并用映射值替换原始字符串中的值的惯用方法

从带有嵌套括号的字符串中提取值

Go time.Parse 无效的 ISO 日期

在 Go 中解组编号的 XML 标签

如何将任何类型的数据值传递到 Golang 中的 GRPC Protobuf struct ?

判断一个区域内的纬度/经度点

Gorm 预加载给出了模糊的列错误

在 Gorm 的 AfterFind() 钩子中获取智能 Select struct 的值

每次有人进入我的网站时如何运行特定功能?

有没有办法判断值是否满足接口中定义的类型约束?

Go:等待多个通道的性能损失

函数调用中的类型参数panic

Go GCP 同时模拟两个服务帐户

Go lang - 惯用的默认后备

AWS EKS 上的 Golang REST API 部署因 CrashLoopBackOff 而失败