• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Go:表驱动单元测试

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Go:表驱动单元测试

单元测试相当的重要,这几天实习由于单元测试没写好所以被骂了emmm

痛定思痛,立刻上网学习了一下,总算达到了预期的效果,所以写一篇文章记录一下

首先安装gotests

$go get -u github.com/cweill/gotests/...

然后记得添加到PATH里面

之后在goland里面可以直接自动生成表驱动的单元测试

生成代码大概是这个样子

package test

import (
	"publish_server_receivehook/services"
	"reflect"
	"testing"
)

func TestDealWithData(t *testing.T) {
	type args struct {
		data []byte
	}
	tests := []struct {
		name string
		args args
		want map[string]interface{}
	}{
		// TODO: Add test cases.
		//正常情况
		{"1",args{data:[]byte(`{
  "ref": "refs/heads/master",
  "before": "fc5075afcb02fcf9d356d4b1a23348506a522947",
  "after": "fc5075afcb02fcf9d356d4b1a23348506a522947",
  "compare_url": "",
  "commits": [
    {
      "id": "fc5075afcb02fcf9d356d4b1a23348506a522947",
      "message": "123\n",
      "url": "http://localhost:3000/ch/git_hook_test/commit/fc5075afcb02fcf9d356d4b1a23348506a522947",
      "author": {
        "name": "miracle",
        "email": "[email protected]",
        "username": "ch"
      },
      "committer": {
        "name": "miracle",
        "email": "[email protected]",
        "username": "ch"
      },
      "added": [
        "test.txt"
      ],
      "removed": [],
      "modified": [],
      "timestamp": "0001-01-01T00:00:00Z"
    }
  ],
  "repository": {
    "id": 1,
    "owner": {
      "id": 1,
      "username": "ch",
      "login": "ch",
      "full_name": "",
      "email": "[email protected]",
      "avatar_url": "https://secure.gravatar.com/avatar/bdafa8c50b079c7bf7a647de881e3b0b?d=identicon"
    },
    "name": "git_hook_test",
    "full_name": "ch/git_hook_test",
    "description": "",
    "private": false,
    "fork": false,
    "parent": null,
    "empty": false,
    "mirror": false,
    "size": 12288,
    "html_url": "http://localhost:3000/ch/git_hook_test",
    "ssh_url": "[email protected]:ch/git_hook_test.git",
    "clone_url": "http://localhost:3000/ch/git_hook_test.git",
    "website": "",
    "stars_count": 0,
    "forks_count": 0,
    "watchers_count": 1,
    "open_issues_count": 0,
    "default_branch": "master",
    "created_at": "2019-02-17T16:52:44+08:00",
    "updated_at": "2019-02-17T17:00:23+08:00"
  },
  "pusher": {
    "id": 1,
    "username": "ch",
    "login": "ch",
    "full_name": "",
    "email": "[email protected]",
    "avatar_url": "https://secure.gravatar.com/avatar/bdafa8c50b079c7bf7a647de881e3b0b?d=identicon"
  },
  "sender": {
    "id": 1,
    "username": "ch",
    "login": "ch",
    "full_name": "",
    "email": "[email protected]",
    "avatar_url": "https://secure.gravatar.com/avatar/bdafa8c50b079c7bf7a647de881e3b0b?d=identicon"
  }
}`)}, map[string]interface{}{"repository_name":"ch/git_hook_test","pusher":"ch"}},

		//异常情况
		{"2",args{data:[]byte(`{"test":"test"}`)}, map[string]interface{}{"repository_name":nil,"pusher":nil}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := services.DealWithData(tt.args.data); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("DealWithData() = %v, want %v", got, tt.want)
			}
		})
	}
}

  因为我的测试数据比较多,所以代码看来比较多.

其实都是自动生成的,自己在TODO下面添加测试用的数据就可以了。

另外测试数据一定要包含正确的数据以及错误的数据,这样的测试才有意义。

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
下个10年,Go能取代Python成为开发者的首选语言吗?发布时间:2022-07-10
下一篇:
[Go] go下实现md5加密发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap