doutongya8378 2016-09-28 03:35
浏览 164
已采纳

Golang无法在同一程序包中进行测试

Having issue creating unit test of one of my source file ( commonutil.go )

package util

import "github.com/nu7hatch/gouuid"

// GenerateUUID Returns generated UUID sequence
func GenerateUniqueID(hostname string) (string, error) {
    var result, err = uuid.NewV4()
    return hostname + ":" + result.String(), err
}

For the above source, I created the test file "commonutil_test.go" ( in the same package )

package util

import "testing" 
func TestCommonUtil(t *testing.T) {
t.Run("Test generate UUID", func(t *testing.T) {

    var uuid, _ = GenerateUniqueID ("test")
    //fmt.Printf("UUID isa %v 
", uuid)
    if uuid == "" {
        t.Errorf("UUID expected, but result is empty ")
    }
})

However when trying executing "go test util/commonutil_test.go" it shows :

util\commonutil_test.go:8: undefined: GenerateUniqueID
FAIL command-line-arguments [build failed]

Changing to util.GenerateUniqueID in the test solve the problem, however when running coverage using Goconvey will cause build failure :

can't load package: import cycle not allowed package rudygunawan.com/MyProject/HFLC-Go/util imports rudygunawan.com/MyProject/HFLC-Go/util

Any idea to solve this issue? I am confused.

Go version is go1.7.1 windows/386

  • 写回答

2条回答 默认 最新

  • doukuangxiu1621 2016-09-28 03:53
    关注

    Just realize it is a silly mistake. The package for the test should be "util_test". Putting the test in the separate package ( but still in the same folder) help solve import cycle issue, yet still allow to solve the undefined error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度