V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
? http://golang.org/
? Go Playground
Go Projects
? Revel Web Framework
yujianwjj
V2EX  ?  Go 编程语言

如何修改 goland 默认生成的单元测试的样式。

  •  
  •   yujianwjj · 33 天前 · 849 次点击
    这是一个创建于 33 天前的主题,其中的信息可能已经有所发展或是发生改变。

    腾讯云最新优惠活动来了:云产品限时1折,云服务器低至88元/年 ,点击这里立即抢购:9i0i.cn/qcloud,更有2860元代金券免费领取,付款直接抵现金用,点击这里立即领取:9i0i.cn/qcloudquan

    (福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

    func check(a, b int) bool {
    	return false
    }
    

    用 goland 给上面这个函数生成的测试方法

    func Test_check(t *testing.T) {
    	type args struct {
    		a int
    		b int
    	}
    	tests := []struct {
    		name string
    		args args
    		want bool
    	}{
    		// TODO: Add test cases.
    	}
    	for _, tt := range tests {
    		t.Run(tt.name, func(t *testing.T) {
    			if got := check(tt.args.a, tt.args.b); got != tt.want {
    				t.Errorf("check() = %v, want %v", got, tt.want)
    			}
    		})
    	}
    }
    

    默认生成的规则是把函数所有的参数放到一个 args 结构体里面。自己不太喜欢这种风格。 我希望是下面这种风格。

    func Test_check(t *testing.T) {
    	tests := []struct {
    		name string
    		a    int
    		b    int
    		want bool
    	}{
    		// TODO: Add test cases.
    	}
    	for _, tt := range tests {
    		t.Run(tt.name, func(t *testing.T) {
    			if got := check(tt.a, ttb); got != tt.want {
    				t.Errorf("check() = %v, want %v", got, tt.want)
    			}
    		})
    	}
    }
    

    请教一下 goland 是否可以改成默认用我这种风格。

    顺便再请教一个问题:

    1. 如果一个函数的参数有一个大的结构体(比如 k8s 的 deployment 那种),每次写单元测试的时候,要构造一个大的结构体,特别麻烦,有没有好的解决方案。
    1 条回复  ?  2024-04-09 13:55:35 +08:00
    xhd2015
        1
    xhd2015  
       33 天前 via iPhone
    这个应该是一个叫做 gotests 的工具生成的,你可以手动运行命令行。参考 https://github.com/cweill/gotests
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   6405 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 02:20 · PVG 10:20 · LAX 19:20 · JFK 22:20
    Developed with CodeLauncher
    ? Do have faith in what you're doing.


    http://www.vxiaotou.com