我有一个这样的项目:
app/
api/
foo.go
test_foo.go
src/
db/
bar.go
Run Code Online (Sandbox Code Playgroud)
在 中foo.go,对 中的函数进行调用bar.go。但是,在运行覆盖率报告时,它显示 bar.go 的 0 行被覆盖。
有没有办法让覆盖范围包括对其他包的函数调用?
就我而言,我并不想为单独的测试db和api,因为通过所有的呼叫db将总是通过api,这将是多余写两个测试。
我正在运行这样的覆盖范围:
go clean -testcache
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
Run Code Online (Sandbox Code Playgroud) 我有集成测试,位于单独的目录中。这些测试通过net / http / httptest在同一进程中运行我的http服务器。我的测试在运行,但没有覆盖范围。
这是一个非常简化的示例,为了简洁起见,不使用http。目录布局:
$GOPATH/src/go-test
hello
hello.go
itest
integration_test.go
Run Code Online (Sandbox Code Playgroud)
你好
package hello
func Hello() string {
return "hello"
}
Run Code Online (Sandbox Code Playgroud)
integration_test.go
package itest
import (
"go-test/hello"
"testing"
)
func TestHello(t *testing.T) {
s := hello.Hello()
if s != "hello" {
t.Errorf("Hello says %s", s)
}
}
Run Code Online (Sandbox Code Playgroud)
运行测试:
$ go test -v -coverpkg ./... ./itest
=== RUN TestHello
--- PASS: TestHello (0.00s)
PASS
coverage: 0.0% of statements in ./...
ok go-test/itest 0.001s coverage: 0.0% of statements in ./...
Run Code Online (Sandbox Code Playgroud)
另一尝试:
$ …Run Code Online (Sandbox Code Playgroud)