dtja73027 2018-09-06 11:29
浏览 49
已采纳

如何在golang中编写关于插入,获取,删除和更新数据的测试用例

I have to write the test cases for the inserting, fetching, deletion and updating the data. While searching on the internet I found a code and it works but I don't know how it works exactly. The code I have is given below can anybody please tell me in easy way that how I will understand the code.

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)
        })
    }
} 
  • 写回答

1条回答 默认 最新

  • drvfqr5609 2018-09-06 12:02
    关注

    Below I Have Provided With The Form Of Test Case Known as Table Driven Tests

    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) {
        })
    }
    

    In The Following Code What we Do is:

    *Declare A Slice of Struct([]struct) With Three Parameters

    1.Name:- It Would Be used For naming the Test in t.Run.

    2.Args:- Here We Specify the arguments That Are needed By The Function We are Going To Test.

    3.Want:- This is the bool expression which will be used For Comparison with The output of our Result.

    Now as in Your Code You have Added Something in the Database so You Need To call a Function Which Will Fetch the record.

    If the err is equal to nil by the addblog Function.

    After That You can Compare if all of the values are saved by comparing and saving the result as a bool which we can use for comparison with our want bool expression.

    Something Like this Will happen:

     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()
      }
     }
    

    Note: Here I have Compared The Id Attribute as it Will be Unique .

    You Need To Declare that in Your args First.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分