如何在Golang中引入测试模块_Golang测试包导入与使用实践
技术百科
P粉602998670
发布时间:2026-01-02
浏览: 次 Go测试文件必须以_test.go结尾且与被测代码同包;测试函数须为func TestXxx(testing.T)签名;go test支持多种运行方式;testing.T非并发安全,需谨慎使用。
Go 测试文件必须以 _test.go 结尾
Go 的 go test 命令只会自动识别和运行后缀为 _test.go 的文件。如果命名为 utils_test.go,它会被识别;但写成 test_utils.go 或 utils_test.go.bak 就完全不会执行。
- 测试文件需与被测代码在同一包内(通常同目录),才能直接访问未导出的函数和变量
- 若想测试私有逻辑,不要把测试文件放到新包里——否则无法调用
unexportedFunc() - 跨包测试(如集成测试)应新建独立包,用
import引入目标包,只测导出项
func TestXxx(*testing.T) 是唯一被识别的测试函数签名
Go 不支持自定义测试函数名或参数类型。只有形如 func TestSomething(t *testing.T) 的函数才会被 go test 扫描到。常见错误包括:
- 写成
func testSomething(t *testing.T)(首字母小写 → 忽略) - 漏掉
*testing.T参数,或改成*testing.B(那是基准测试,不是单元测试) - 多加一个参数,比如
func TestXxx(t *testing.T, ctx context.Context)(编译通过但不被识别)
func TestAdd(t *testing.T) {
got := Add(2, 3)
want := 5
if got != want {
t.Errorf("Add(2,3) = %d, want %d", got, want)
}
}使用 go test 运行时要注意工作目录和包路径
在模块根目录下执行 go test 默认跑当前包;加 -v 可看详细输出,加 -run 可匹配测试函数名。
-
go test:仅运行当前目录下的*_test.go -
go test ./...:递归运行所有子目录中的测试(推荐 CI 场景) -
go test -run=^TestAdd$:精确匹配函数名(^和$是正则锚点) - 若项目启用了 Go modules,确保
go.mod存在且GO111MODULE=on(默认已启用)
别忘了 testing.T 的并发安全限制
*testing.T 对象不是并发安全的——不能在 goroutine 中直接调用 t.Log 或 t.Error,否则可能 panic 或输出错乱。
- 需要并发验证时,先收集结果,主 goroutine 再断言
- 用
t.Parallel()标记测试可并行执行,但前提是测试间无共享状态 -
t.Fatal/t.Fatalf会终止当前测试函数,但不影响其他测试;而os.Exit(1)会直接退出整个go test进程,禁止使用
func TestConcurrentAdd(t *testing.T) {
t.Parallel() // 允许与其他 Parallel 测试并
发运行
results := make(chan int, 10)
for i := 0; i < 10; i++ {
go func() {
results <- Add(1, 1)
}()
}
for i := 0; i < 10; i++ {
if got := <-results; got != 2 {
t.Errorf("expected 2, got %d", got) // 在主 goroutine 中调用
}
}
}真正容易被忽略的是:测试文件里 import 的包,只要没被任何测试函数实际引用,Go 编译器会静默忽略——这意味着 _test.go 中写了 import "net/http" 却没用,不会报错,但后续加了 http 相关逻辑却忘记补 import,就会编译失败。检查方式很简单:go test -v -x 看实际执行的编译命令,或用 go list -f '{{.Imports}}' xxx_test.go 确认依赖是否完整。
# 的是
# 就会
# 能在
# 要把
# 才会
# 那是
# 自动识别
# 只会
# 很简单
# http
# go
# golang
# golang测试
# Error
# 递归
# 并发
# 对象
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- Windows10怎么备份注册表_Windows1
- 如何在 Go 后端安全获取并验证前端存储的 JWT
- Django 密码修改后会话失效的解决方案
- php怎么下载安装后无法解析php文件_服务器配置
- mac怎么安装字体_MAC添加第三方字体与字体册管
- php文件怎么变mp4保存_php输出视频流保存为
- 如何使用Golang sort排序切片_Golan
- Python迭代器生成器进阶教程_节省内存与懒加载
- 如何使用Golang搭建本地API测试环境_快速验
- Linux怎么实现内网穿透_Linux安装Frp客
- 如何使用Golang recover捕获panic
- c++中如何使用虚函数实现多态_c++多态性实现原
- Win11怎么更改计算机名_Windows11系统
- C#如何使用Channel C#通道实现异步通信
- Win10如何卸载WindowsDefender_
- Mac如何修改Hosts文件?(本地开发与屏蔽网站
- 如何用正则表达式精确匹配最多含一个换行符的起止片段
- 如何使用Golang实现聊天室消息存档_存储聊天记
- Windows 10自带杀毒软件在哪_Window
- 如何使用Golang实现云原生应用弹性伸缩_自动应
- Win11怎么关闭搜索历史_Win11清除任务栏搜
- Win11怎么设置任务栏大小_Windows11注
- Win11怎么设置ipv4地址_Windows 1
- 如何在Golang中使用闭包_封装变量与函数作用域
- php485支持哪些操作系统_php485跨系统支
- 如何在Golang中实现邮件发送功能_Golang
- Win11怎么关闭键盘按键音_Win11禁用打字声
- 如何在Golang中编写异步函数测试_Golang
- 如何使用Golang benchmark测量函数延
- 手机php怎么转mp4_手机端php文件转mp4a
- Win11怎么更改管理员名字 Win11修改账户名
- Mac上的iMovie如何剪辑视频?(新手入门教程
- Win10电脑怎么设置IP地址_Windows10
- Win11怎么查看局域网电脑_Windows 11
- Win11怎么查看已连接wifi密码 Win11查
- 如何在Golang中配置代码格式化工具_使用gof
- 如何使用Golang指针与接口结合_实现方法调用和
- 如何使用Golang配置安全开发环境_防止敏感信息
- php订单日志怎么记录发货_php记录订单发货操作
- Windows10系统更新错误0x80070002
- Win11如何设置系统声音_Win11系统声音调整
- php增删改查需要哪些扩展_开启mysqli或pd
- Win11怎么设置屏保时间_调整Win11屏幕保护
- Win11怎么关闭OneDrive同步_Win11
- 新手学PHP架构总混淆概念咋办_重点梳理【教程】
- C++如何使用std::async进行异步编程?(
- Python音视频处理高级项目教程_FFmpegP
- 如何从 Go 的 map[string]inter
- Windows10如何查看保存的WiFi密码_Wi
- Windows Defender扫描失败怎么办_安

发运行
results := make(chan int, 10)
for i := 0; i < 10; i++ {
go func() {
results <- Add(1, 1)
}()
}
for i := 0; i < 10; i++ {
if got := <-results; got != 2 {
t.Errorf("expected 2, got %d", got) // 在主 goroutine 中调用
}
}
}
QQ客服