JetBrains GoLand单元测试不支持单个单元测试case执行

譬如函数代码

Go 复制代码
func AddInt(a, b int32) int32 {
	return a + b
}

单元测试代码:

Go 复制代码
func TestAddInt(t *testing.T) {
	type args struct {
		a int32
		b int32
	}
	tests := []struct {
		name string
		args args
		want int32
	}{
		{
			name: "add",
			args: args{a: 1, b: 2},
			want: 3},
		{
			name: "add",
			args: args{a: 1, b: -2},
			want: -1,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := AddInt(tt.args.a, tt.args.b); got != tt.want {
				t.Errorf("AddInt() = %v, want %v", got, tt.want)
			}
		})
	}
}

以上单元测试代码在GoLand中截图如图:

第15、19行是有可以运行符号的,此时可以单个case运行调试。

如果在25、26行之间加点逻辑代码,如下:

此时15、19行的运行图标没有了!

原因是在Run外添加处理逻辑,正确的添加逻辑位置如图所示,应该在Run函数内部。

此坑在开始写单元测试时踩了好久,在某天突然悟道此原因,记录之。

相关推荐
妙码生花20 分钟前
PHP 各框架下和 Go 的性能比较
前端·后端·go
geovindu2 小时前
go: Task Scheduler
开发语言·后端·golang
江湖十年3 小时前
Go 还是 Golang?可能你一直都搞错了!
后端·面试·go
FfHUCisI5 小时前
Go 内存分配器概览:从 TCMalloc 到三级缓存架构
缓存·架构·golang
Johnston_man6 小时前
golang 安装 protoc、protoc-gen-go、protoc-gen-go-grpc、protoc-gen-grpc-gateway
后端·golang
ttwuai6 小时前
Go 后台接入单点登录后,JWT、菜单权限和接口 403 怎么验?
开发语言·后端·golang
sanjiaomao33318 小时前
从论文公式到Python实现:用单元测试校验滑动平均算法
python·算法·单元测试
码士集团小青21 小时前
从对标 Java 到对标 Go:Native AOT 的“无痛化“之路,走到哪一站了?
golang
右耳朵猫AI21 小时前
Go周刊2026W36 | Go 1.27.1 发布、TinyGo 0.42、HTTP/2 原生迁入 net/http、quic-go 0.62
redis·http·golang