当测试位于其他包装中时,无测试范围 [英] No test coverage when tests are in a different package

查看:86
本文介绍了当测试位于其他包装中时,无测试范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有集成测试,它们位于单独的目录中.这些测试通过net/http/httptest在相同的进程中运行我的http服务器.我的测试正在运行,但是没有覆盖范围.

I have integration tests which are located in a separate directory. Those tests run my http server in the same process via net/http/httptest. My tests run but I get no coverage.

这是一个非常简化的示例,为简洁起见,不使用http. 目录布局:

Here is a very simplified example not using http for brevity. Directory layout:

$GOPATH/src/go-test
  hello
    hello.go
  itest
    integration_test.go

hello.go

package hello

func Hello() string {
    return "hello"
}

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)
    }
}

运行测试:

$ 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 ./...

另一种尝试:

$ go test -v -coverpkg all ./itest
=== RUN   TestHello
--- PASS: TestHello (0.00s)
PASS
coverage: 0.0% of statements in all
ok      go-test/itest   0.001s  coverage: 0.0% of statements in all

请注意,覆盖率是0%.

Notice that coverage is 0%.

根据go help testflag:

-coverpkg pattern1,pattern2,pattern3
    Apply coverage analysis in each test to packages matching the patterns.
    The default is for each test to analyze only the package being tested.
    See 'go help packages' for a description of package patterns.
    Sets -cover.

当我的测试在不同的程序包中时,如何获得真实的保障?

How can I get the real coverage when my tests are in a different package?

$ go version
go version go1.10 linux/amd64

推荐答案

go test -v -coverpkg ./... ./...

应该给您预期的结果

这篇关于当测试位于其他包装中时,无测试范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆