在不同的包上进行测试覆盖

Go Test Coverage, over different packages

我一直在写一个休息服务我有以下结构

/controllers
/domain/dao
/services

在为每个单独的包编写大量测试并为每个阶段进行模拟之后,我认为只针对控制器编写测试并模拟数据库会更快,所以我知道 /domain/dao正在调用图层。

我现在的问题是,当我 运行 代码覆盖时,我没有得到我的 /service 或 /domain/dao 的覆盖,即使我知道代码正在被调用

关于如何让我的代码覆盖所有文件有什么想法吗?

你可以试试flag -coverpkg

go test --cover -coverpkg=./services  ./... -coverprofile=cover.out

要同时检查文件夹/services 中的所有包,您可以尝试:

go test --cover -coverpkg=./services/...  ./... -coverprofile=cover.out

只是一个旁注 - 我通常添加 -covermode=count

go test --cover -covermode=count -coverpkg=./services/...  ./... -coverprofile=cover.out

然后,你可以使用go tool查看为html:

go tool cover -html=cover.out