如何在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; ?>
】
相关推荐
- Win11怎么设置默认邮件应用_Windows11
- 如何使用正则表达式提取以编号开头、后接多个注解的逻
- Win11怎么查看已连接wifi密码 Win11查
- 如何用::实现单例模式_php静态方法与作用域操作
- MySQL 中使用 IF 和 CASE 实现查询字
- 新手学PHP架构总混淆概念咋办_重点梳理【教程】
- mac怎么看硬盘大小_MAC查看磁盘存储空间与文件
- 如何使用正则表达式批量替换重复的 *- 模式为固定
- c++输入输出流 c++ cin与cout格式化输
- Win11如何添加/删除输入法 Win11切换中英
- 如何使用 Selenium 正确获取篮球参考网站球
- 用Python构建微服务架构实践_FastAPI与
- php转exe用什么工具打包快_高效打包软件推荐【
- Linux怎么查找死循环进程_Linux系统负载分
- Mac的“预览”如何合并多个PDF_Mac文件处理
- 如何使用Golang读取日志文件_Golang b
- XAMPP 启动失败(Apache 突然停止)的终
- Windows10如何查看保存的WiFi密码_Wi
- php查询数据怎么分组_groupby分组查询配合
- 如何理解Go指针和内存分配关系_Go Pointe
- Windows资源管理器总是卡顿或重启怎么办?(修
- VSC怎么快速定位PHP错误行_错误追踪设置法【方
- Win10怎样清理C盘阿里旺旺缓存_Win10清理
- 短链接怎么用php递归还原_多层加密链接的处理法【
- Drupal 中 HTML 链接被双重转义导致渲染
- 如何使用Golang匿名函数_快速定义临时函数逻辑
- 如何使用Golang实现错误包装与传递_Golan
- C++中的std::shared_from_thi
- Win11怎么关闭专注助手 Win11关闭免打扰模
- windows如何备份注册表_windows导出和
- Win11怎么关闭防火墙通知_屏蔽Win11安全中
- php中self::能调用子类重写的方法吗_静态绑
- 如何在Golang中使用time处理时间_Gola
- 短链接怎么用php还原_从基础原理到代码实现教学【
- c++如何利用doxygen生成开发文档_c++
- php能跑在stm32上吗_php在stm32微控
- c++ std::future和std::prom
- PHP的FastAdmin架构适合二次开发吗_特点
- php订单日志怎么按金额排序_php按订单金额排序
- Windows 10怎么录屏_Windows 10
- 如何使用Golang实现云原生应用弹性伸缩_自动应
- Windows笔记本无法进入睡眠模式怎么办?(电源
- Win10系统怎么查看显卡温度_Win10任务管理
- php转mp4怎么设置帧率_调整php生成mp4视
- Win11文件夹预览图不显示怎么办_Win11缩略
- c++如何打印函数堆栈信息_c++ backtra
- 如何在Golang中处理JSON字段缺失_Gola
- C++ static_cast和dynamic_c
- Win11怎样安装钉钉客户端_Win11安装钉钉教
- Win11如何连接Xbox手柄 Win11蓝牙连接

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