GoCov 获得测试覆盖率

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

我在我的应用程序中进行了测试,但我的测试覆盖率显示我的覆盖率为 0%。

下面是我正在使用的 make 命令:

# TODO: FIX this it works if I go into the src directory and manually run this command but not from makefile
coverage:
    cd src/test && \
    echo $(pwd) && \
    go test -mod=mod -coverprofile=coverage.out -coverpkg=./... ./... && go tool cover -func=coverage.out

如果我像这样使用 IntelliJ IDE 运行我的测试(见下图),我得到 2.2% 的覆盖率。

我应该如何处理我的 makefile 命令让我的 gocov 识别我的测试用例?

go testing makefile gnu-make test-coverage
1个回答
0
投票

修复非常简单,我所要做的就是在斜杠前面添加另一个

.
,所以我不得不使用
-coverpkg=./... ./...
而不是
-coverpkg=./... ./...
。下面是我的 make 命令,我还添加了 gocov 报告。

coverage: coverage-tools
    cd src/test && \
    go test -mod=mod -coverprofile=coverage.out -coverpkg=../... ../... && \
    mkdir -p $(REPORT_DIR) && \
    cp coverage.out $(REPORT_DIR)/ && \
    $(GOPATH)/bin/gocov convert $(REPORT_DIR)/coverage.out | \
    $(GOPATH)/bin/gocov-html > $(REPORT_DIR)/coverage.html
    rm ./src/test/coverage.out
© www.soinside.com 2019 - 2024. All rights reserved.