Golang测试中t.Helper未定义问题(类型*testing.T无Helper字段或方法)
Golang测试中t.Helper未定义问题(类型*testing.T无Helper字段或方法) 我正在学习《通过测试学习Go》。
问题:尝试在 Visual Studio Code 中使用以下源代码:
package main
import "testing"
func TestHello(t *testing.T) {
assertCorrectMessage := func(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
t.Run("saying hello to people", func(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
assertCorrectMessage(t, got, want)
})
t.Run("empty string defaults to 'world'", func(t *testing.T) {
got := Hello("")
want := "Hello, World"
assertCorrectMessage(t, got, want)
})
}
收到以下错误:
t.Helper 未定义(类型 *testing.T 没有字段或方法 Helper) t.Run 未定义(类型 *testing.T 没有字段或方法 Run) t.Run 未定义(类型 *testing.T 没有字段或方法 Run)
我认为这可能与我在 VSCode 中的 Go 环境设置有关。在最初的模块中我成功运行了一个简短的测试,但在访问 t 的方法时遇到了错误。
非常感谢任何帮助,谢谢!
更多关于Golang测试中t.Helper未定义问题(类型*testing.T无Helper字段或方法)的实战教程也可以访问 https://www.itying.com/category-94-b0.html
hmmm…然后我意识到我在Linux上按照下载Go的说明是针对旧版本的。我已经卸载了那个版本,现在运行的是1.13.3版本,并具备了所需的功能。
不客气。你介意将合适的帖子标记为"已解决"吗?这是为了向其他人表明问题已解决,他们可以处理其他问题了。😁
随时可以再问其他问题。
更多关于Golang测试中t.Helper未定义问题(类型*testing.T无Helper字段或方法)的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
非常感谢您的反馈!
我查看了我的 /src/testing.go 文件,发现我缺少了 Helper 函数和 Run 函数。
嗯……然后我意识到,我遵循的在 Linux 上下载 Go 的教程是针对旧版本的。我已经卸载了那个版本,现在正在运行 1.13.3 版本,并且拥有了所需的正確函数。

引用 jpizza10:
我在想这可能与我在 VSCode 中设置 Go 的方式有关。我在最初的模块中成功运行了一个简短的测试,但在访问 ‘t’ 的方法时遇到了错误。
我在本地 Linux 系统上运行了你的代码。它运行正常。是的,这应该与 VS Code 的设置有关。VS Code 是否正确配置以访问 GOROOT 的内容?
如果你需要,我在这里附上了一份 VS Code 指南(在 Visual Studio Code 中使用 Go)。由于我身边没有 Windows 机器,我能提供的帮助也就这么多了。🤐
package main
import (
"testing"
)
func Hello(name string) string {
if name != "" {
return "Hello, 1" + name
}
return "Hello, World"
}
func TestHello(t *testing.T) {
assertCorrectMessage := func(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
t.Run("saying hello to people", func(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
assertCorrectMessage(t, got, want)
})
t.Run("empty string defaults to 'world'", func(t *testing.T) {
got := Hello("")
want := "Hello, World"
assertCorrectMessage(t, got, want)
})
}
\\ Test Output:
\\ --- FAIL: TestHello (0.00s)
\\ --- FAIL: TestHello/saying_hello_to_people (0.00s)
\\ main_test.go:25: got "Hello, 1 Chris" want "Hello, Chris"
\\ FAIL
\\ FAIL gosandbox 0.001s
\\ FAIL
这个问题通常是由于Go版本不兼容导致的。t.Helper()和t.Run()是在Go 1.9版本中引入的。请检查你的Go版本:
go version
如果你的Go版本低于1.9,需要升级到1.9或更高版本。对于当前开发,建议使用Go 1.20+。
如果版本正确但问题仍然存在,可能是VSCode的Go工具链配置问题。尝试以下步骤:
- 更新Go工具:
go get -u golang.org/x/tools/gopls
-
在VSCode中重新加载Go扩展,或重启VSCode。
-
确保你的
go.mod文件正确(如果使用模块):
module your-module-name
go 1.20 // 确保这里指定了足够高的版本
- 清理并重新构建:
go clean -testcache
go test ./...
如果问题仍然存在,可以尝试在命令行直接运行测试来确认是否是VSCode的问题:
go test -v
对于你的测试代码,确保Hello函数已正确定义。完整的示例应该如下:
package main
import "testing"
func Hello(name string) string {
if name == "" {
name = "World"
}
return "Hello, " + name
}
func TestHello(t *testing.T) {
assertCorrectMessage := func(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
t.Run("saying hello to people", func(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
assertCorrectMessage(t, got, want)
})
t.Run("empty string defaults to 'world'", func(t *testing.T) {
got := Hello("")
want := "Hello, World"
assertCorrectMessage(t, got, want)
})
}

