标签:tps 记录 安装 receive host veh dde path update
单元测试相当的重要,这几天实习由于单元测试没写好所以被骂了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": "asd471331055@vip.qq.com",
"username": "ch"
},
"committer": {
"name": "miracle",
"email": "asd471331055@vip.qq.com",
"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": "asd471331055@vip.qq.com",
"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": "miracle@localhost: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": "asd471331055@vip.qq.com",
"avatar_url": "https://secure.gravatar.com/avatar/bdafa8c50b079c7bf7a647de881e3b0b?d=identicon"
},
"sender": {
"id": 1,
"username": "ch",
"login": "ch",
"full_name": "",
"email": "asd471331055@vip.qq.com",
"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下面添加测试用的数据就可以了。
另外测试数据一定要包含正确的数据以及错误的数据,这样的测试才有意义。
标签:tps 记录 安装 receive host veh dde path update
原文地址:https://www.cnblogs.com/Miracle-boy/p/10426167.html