【问题标题】:How to get a complete coverage in all package golang如何完全覆盖所有包 golang
【发布时间】:2021-11-04 04:36:46
【问题描述】:

我只有 24 小时使用 Go,所以我对此非常陌生。

我正在尝试进行完整的测试转换,但是当我运行时

go test -cover new-project/anto/... .

new-project/anto/mon 中显示我有 51.8% 的覆盖率,但实际上我有更多的覆盖率,因为在 new-project/anto 中测试了一些测试/integration 我有一个测试文件,我导入 new-project/anto/mon 进行测试。

问题是,当我阅读 Golang 时,仅在测试位于同一文件夹中时才进行测试,但具有单独的文件并不能正确检测到它。

我想知道是否有办法获得全面覆盖。

go test -cover new-project/anto/... .
?       new-project/anto/       [no test files]
ok      new-project/anto/conf   6.060s  coverage: 73.1% of statements
ok      new-project/anto/integration    43.309s coverage: [no statements]
?       new-project/anto/integration/conf   [no test files]
?       new-project/anto/integration/mocks  [no test files]
?       new-project/anto/integration/utils  [no test files]
ok      new-project/anto/logs   0.024s  coverage: 70.5% of statements
ok      new-project/anto/metric 0.012s  coverage: 59.0% of statements
ok      new-project/anto/mon    1.612s  coverage: 51.8% of statements
?       new-project/anto/anotherComponents  [no test files]

做了一些研究,我发现了一个 go-acc 插件,它可以验证所有的包,奇怪的是 mon 原来是 51.8%,现在变成了 29.8%。

go-acc new-project/anto/... .
?       new-project/anto/   [no test files]
ok      new-project/anto//conf  5.030s  coverage: 73.1
ok      new-project/anto//integration   43.117s coverage: 73.5
?       new-project/anto//integration/conf  [no test files]
?       new-project/anto//integration/mocks [no test files]
?       new-project/anto//integration/utils [no test files]
ok      new-project/anto//logs  0.008s  coverage: 22.3
ok      new-project/anto//metric    0.020s  coverage: 59.0
ok      new-project/anto//mon   1.460s  coverage: 29.8
?       new-project/anto//anotherComponents     [no test files]
?       new-project/anto/   [no test files]

【问题讨论】:

    标签: go testing integration-testing


    【解决方案1】:

    第一个命令将为所有包生成coverage.out。第二个解析coverage.out并输出结果:

    go test ./...  -coverpkg=./... -coverprofile ./coverage.out
    go tool cover -func ./coverage.out
    

    样本输出:

    golang.org/x/example/stringutil/reverse.go:21: Reverse      100.0%
    total:                                         (statements) 100.0%
    

    有关更多详细信息,请参阅各种 help 输出:

    • go help test
    • go help testflags
    • go help packages
    • go tool cover --help

    【讨论】:

    • 您好 Matthew Warman,非常感谢您的帮助!但是当我尝试这个选项时,我所有的测试都失败了 go test ./... -coverpkg=./... -coverprofile ./coverage.out ? new-project/anto [无测试文件] FAIL new-project/anto/conf [build failed] FAIL new-project/anto/integration 0.012s ?新项目/安托/集成/配置?新项目/安托/集成/模拟? new-project/anto/integration/utils 失败 new-project/anto/logv 失败 new-project/anto/metric 失败 new-project/anto/mon go tool cover -func ./coverage.out total: (statements) 0.0%
    猜你喜欢
    • 2017-12-22
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2021-08-12
    • 2013-05-01
    相关资源
    最近更新 更多