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

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗