Golang 无法在同一个包中进行测试
Golang not able to test in same package
在为我的源文件之一 (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
}
对于上面的源,我创建了测试文件"commonutil_test.go"(在同一个包中)
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 \n", uuid)
if uuid == "" {
t.Errorf("UUID expected, but result is empty ")
}
})
然而,当尝试执行 "go test util/commonutil_test.go" 时,它显示:
util\commonutil_test.go:8: undefined: GenerateUniqueID
FAIL command-line-arguments [build failed]
在测试中更改为 util.GenerateUniqueID 解决了问题,但是当 运行 覆盖使用 Goconvey 时将导致构建失败:
can't load package: import cycle not allowed
package rudygunawan.com/MyProject/HFLC-Go/util
imports rudygunawan.com/MyProject/HFLC-Go/util
有解决此问题的想法吗?我很困惑。
Go版本是go1.7.1windows/386
只是意识到这是一个愚蠢的错误。测试包应该是"util_test"。将测试放在单独的包中(但仍在同一文件夹中)有助于解决导入周期问题,但仍然可以解决未定义的错误。
我通常编写 Go 单元测试的方式是有一个(或多个)..._test.go
文件,与被测试的代码在同一个包中,每个广泛的集合有一个 Test...
函数要完成的测试。
package util
import "testing
func TestGenerateUniqueID(t *testing.T) {
var uuid1, uuid2 string
uuid1, err = GenerateUniqueID("test")
if err != nil {
t.Errorf("Expected no error, got %s", err) // Maybe Fatalf?
}
if uuid1 == "" {
t.Errorf("Expected non-empty string, got empty string (uuid1)")
}
uuid2, err = GenerateUniqueID("test")
if err != nil {
t.Errorf("Expected no error, got %s", err) // Maybe Fatalf?
}
if uuid2 == "" {
t.Errorf("Expected non-empty string, got empty string (uuid2)")
}
if uuid1 == uuid2 {
t.Errorf("Expected uuid1 and uuid2 to be different, both are %s", uuid1)
}
}
我倾向于进行白盒测试(我可以 "blackbox testing" 通过小心不访问包内部来进行测试)的原因之一是通常还有一大堆未导出的代码确实也应该进行测试.在这个特定的小示例中,没有大量的争论支持一个,因为所有可以测试的功能都已经导出。
当我尝试 运行 单个测试文件时,我 运行 遇到了类似的问题。
我想要那个,因为它是一种测试驱动的开发,我想 运行 只测试我目前正在处理的代码,而不是所有的 x 分钟 运行宁测试。
结果证明解决方案不是 运行 从文件进行测试,而是 运行 按名称(实际上是正则表达式)进行特定测试。所以在你的情况下我猜它会是:
go test ./util -run TestCommonUtil
另一种方法似乎是列出构建测试代码所需的所有文件:
go test util/commonutil_test.go util/commonutil.go
在为我的源文件之一 (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
}
对于上面的源,我创建了测试文件"commonutil_test.go"(在同一个包中)
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 \n", uuid)
if uuid == "" {
t.Errorf("UUID expected, but result is empty ")
}
})
然而,当尝试执行 "go test util/commonutil_test.go" 时,它显示:
util\commonutil_test.go:8: undefined: GenerateUniqueID
FAIL command-line-arguments [build failed]
在测试中更改为 util.GenerateUniqueID 解决了问题,但是当 运行 覆盖使用 Goconvey 时将导致构建失败:
can't load package: import cycle not allowed package rudygunawan.com/MyProject/HFLC-Go/util imports rudygunawan.com/MyProject/HFLC-Go/util
有解决此问题的想法吗?我很困惑。
Go版本是go1.7.1windows/386
只是意识到这是一个愚蠢的错误。测试包应该是"util_test"。将测试放在单独的包中(但仍在同一文件夹中)有助于解决导入周期问题,但仍然可以解决未定义的错误。
我通常编写 Go 单元测试的方式是有一个(或多个)..._test.go
文件,与被测试的代码在同一个包中,每个广泛的集合有一个 Test...
函数要完成的测试。
package util
import "testing
func TestGenerateUniqueID(t *testing.T) {
var uuid1, uuid2 string
uuid1, err = GenerateUniqueID("test")
if err != nil {
t.Errorf("Expected no error, got %s", err) // Maybe Fatalf?
}
if uuid1 == "" {
t.Errorf("Expected non-empty string, got empty string (uuid1)")
}
uuid2, err = GenerateUniqueID("test")
if err != nil {
t.Errorf("Expected no error, got %s", err) // Maybe Fatalf?
}
if uuid2 == "" {
t.Errorf("Expected non-empty string, got empty string (uuid2)")
}
if uuid1 == uuid2 {
t.Errorf("Expected uuid1 and uuid2 to be different, both are %s", uuid1)
}
}
我倾向于进行白盒测试(我可以 "blackbox testing" 通过小心不访问包内部来进行测试)的原因之一是通常还有一大堆未导出的代码确实也应该进行测试.在这个特定的小示例中,没有大量的争论支持一个,因为所有可以测试的功能都已经导出。
当我尝试 运行 单个测试文件时,我 运行 遇到了类似的问题。
我想要那个,因为它是一种测试驱动的开发,我想 运行 只测试我目前正在处理的代码,而不是所有的 x 分钟 运行宁测试。
结果证明解决方案不是 运行 从文件进行测试,而是 运行 按名称(实际上是正则表达式)进行特定测试。所以在你的情况下我猜它会是:
go test ./util -run TestCommonUtil
另一种方法似乎是列出构建测试代码所需的所有文件:
go test util/commonutil_test.go util/commonutil.go