如何在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; ?>
】
相关推荐
- php打包exe如何加密代码_防反编译保护方法【技
- Win11怎么开启游戏模式_Windows11优化
- Win11系统更新后黑屏怎么办 Win11更新黑屏
- Win11怎么更改系统语言_Win11中文语言包下
- Win11怎么设置虚拟桌面 Win11新建多桌面切
- 如何高效删除 NumPy 二维数组中所有元素相同的
- 如何使用Golang实现容器健康检查_监控和自动重
- 如何在Golang中实现服务熔断与限流_Golan
- Win11怎么更改电脑名称_Windows 11修
- 如何将竖排文本文件转换为横排字符串
- Win11右键反应慢怎么办 Win11优化右键菜单
- 如何使用 Python 合并文件夹内多个 Exce
- 如何在Windows上设置闹钟和计时器_系统自带的
- c++ std::atomic如何保证原子性 c+
- Win10如何更改用户账户控制_Windows10
- Win11如何设置环境变量 Win11添加和修改系
- Win11此电脑不在桌面上_Windows 11桌
- 如何使用正则表达式批量替换重复的“-”模式为固定字
- Windows 10怎么录屏_Windows 10
- Win11怎么设置按流量计费_Win11限制后台流
- 如何减少Golang内存碎片化_Golang内存分
- 如何在 Go 中创建包含 map 的 slice(
- 如何快速验证Golang安装是否成功_运行go v
- php删除数据怎么加限制_带where条件删除避免
- php串口通信波特率怎么选_根据硬件手册设置正确波
- 如何在Golang中使用log包输出不同级别日志_
- c# F# 的 MailboxProcessor
- Windows任务计划服务异常原因_任务调度失败的
- Windows10电脑怎么设置自动连接WiFi_W
- VSC怎么快速定位PHP错误行_错误追踪设置法【方
- 如何在Golang中实现CI/CD流水线自动化测试
- Win11怎么更改任务栏颜色_Windows11个
- 如何在Golang中使用encoding/gob序
- Windows11怎样开启游戏模式_Windows
- Win11怎样安装企业微信_Win11安装企业微信
- 如何解决Windows字体显示模糊的问题?(Cle
- Python音视频处理高级项目教程_FFmpegP
- MAC如何安装Git版本控制工具_MAC开发环境配
- Python 中将 ISO 8601 时间戳转换为
- XML的“混合内容”是什么 怎么用DTD或XSD定
- Windows如何拦截腾讯视频广告_Windows
- Mac的“调度中心”与“空间”怎么用_Mac多桌面
- 如何在JavaScript中动态拼接PHP的bas
- Win11怎么设置右键刷新选项_Windows11
- Mac如何解压zip和rar文件?(推荐免费工具)
- Linux如何使用grep搜索文件内容_Linux
- Windows10系统怎么查看运行时间_Win10
- 如何从 Go 的 map[string]inter
- Python包结构设计_大型项目组织解析【指导】
- 如何优化Golang程序CPU性能_Golang

发运行
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客服