登录
首页 >  Golang >  Go问答

编写用于测试在Golang中插入、获取、删除和更新数据的测试案例

来源:stackoverflow

时间:2024-03-10 11:30:26 320浏览 收藏

今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《编写用于测试在Golang中插入、获取、删除和更新数据的测试案例》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!

问题内容

我必须编写插入、获取、删除和更新数据的测试用例。在互联网上搜索时,我找到了一个代码并且它可以工作,但我不知道它到底是如何工作的。下面给出了我的代码,任何人都可以简单地告诉我如何理解该代码。

package models

import(
    "testing"
    "gopkg.in/mgo.v2/bson"
    "fmt"
)

func TestAddBlog(t *testing.T) {
    type args struct{
        query interface{}
    }
    tests := []struct{
        name string
        args args
        want bool
    }{
        {
            "first",
            args{
               bson.M{
                   "_id" : 4,
                   "title" : "Life",
                   "type" : "Motivation",
                   "description" : "If you skip anything then you will fail in the race of the life....",
                   "profile_image" : "/image1",
                   "date_time" : 1536062976,
                   "author" : "Charliee",
                   "status" : 1,
                   "slug" : "abc",
                   "comment_count" : 100,
                   "comment_status" : "q",
                },
            },
            true,
        },
        {
           "second",
           args{
               bson.M{
                   "_id" : 5,
                   "title" : "Life",
                   "type" : "Motivation",
                   "description" : "If you skip anything then you will fail in the race of the life....",
                   "profile_image" : "/image1",
                   "date_time" : 1536062976,
                   "author" : "Charliee",
                   "status" : 1,
                   "slug" : "abc",
                   "comment_count" : 100,
                   "comment_status" : "q",
                },
            },
            false,
        },
    }
    for _, k := range tests {
        t.Run(k.name, func (t *testing.T) {
            err := AddBlog(k.args.query)
            fmt.Println(err)
        })
    }
}

解决方案


下面我提供了称为表驱动测试的测试用例形式

type args struct {
}
tests := []struct {
    name string
    args args
    want bool
}{
    {
        "first",
        args{

        },
        true,
    },
    {
        "second",
        args{

        },
        false,
    },
}
for _, tt := range tests {
    t.run(tt.name, func(t *testing.t) {
    })
}

在下面的代码中我们所做的是:

*声明具有三个参数的 struct([]struct) 切片

1.name:- 它将用于命名 t.run 中的测试。

2.args:- 在这里我们指定我们要测试的函数所需的参数。

3.want:- 这是布尔表达式,将用于与结果的输出进行比较。

现在,在您的代码中,您已经在数据库中添加了一些内容,因此您需要调用一个将获取记录的函数。

如果 addblog 函数的 err 等于 nil。

之后,您可以通过比较来比较是否保存所有值并将结果保存为布尔值,我们可以使用它与我们想要的布尔表达式进行比较。

类似的事情会发生:

err:=  AddBlog(k.args.query)
 if err==nil{
 got,err:=fetchBlog(k.args.query)
 if val:=err==nil && got.id==id;val!=k.want{
   t.Fail()
  }
 }

注意: 这里我比较了 id 属性,因为它是唯一的。

您需要首先在您的参数中声明这一点。

理论要掌握,实操不能落!以上关于《编写用于测试在Golang中插入、获取、删除和更新数据的测试案例》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>