使用 Goland 运行集成测试时如何覆盖我的服务器应用程序(包含 main.go)?仅测试容器一侧看起来被分析

问题描述 投票:0回答:1

我有一个带有

main.go
的 Go 项目以及管理器、dao、域类型...这是我的服务器应用程序。
我用“Run go build main.go with Coverage”启动它。

我还有一个

integration_test.go
文件 通过 OpenApi 与启动的应用程序进行交互,以挑战它。

如果我使用“

运行integration_test.go with Coverage
”来运行此integration_test.go
最后,它只记录程序在哪里传入
integration_test.go
及其子代码。

但是我要求涵盖的主要项目并未涵盖,
所有东西(管理器、dao...)都标记为 0% 文件、0% 覆盖率。

如果我在覆盖范围内运行

main.go
但在没有覆盖范围的情况下运行测试,情况是一样的。

我应该怎样做才能覆盖我的服务器代码?

go testing code-coverage goland
1个回答
1
投票

从 go 1.20 开始,这是可能的,GoLang 团队就这个主题写了一篇很棒的博客post,我建议阅读,但这就是要点。

# Set GOCOVERDIR env variable  
export GOCOVERDIR=cov

# create directory for results
mkdir -p $GOCOVERDIR

# run main.go with coverage enabled
go run -cover main.go

# call integration tests from another terminal
go run integration_test.go

# When integration_test.go finishes kill main.go's process ctrl+c

# display the results.  
go tool covdata percent -i=cov
    some-git.provider/some-org/some-repo/cmd/some-service    coverage: 81.2% of statements
© www.soinside.com 2019 - 2024. All rights reserved.