go introduction unit testing framework

Recent projects in the supplementary unit testing, introduced several go in the popular unit testing framework here.

gomock 

gostub

monkey

Convey

Here the main purpose of each frame

The main purpose is to convey the test tissue

gomock mainly used to the interface piling. mockgen may generate a corresponding interface test file.

When used to gostub main variable, function, but to function piling piling process, need to make modifications intrusive

monkey is largely used for variables, functions piling

gostub style by piling principle of reflection, it is required to call stub function passed in the first argument must be a pointer, but does not function pointer concept, and therefore need to make invasive changes to the function.

monkey piling principle is the compilation of statements at run time by overwriting an executable file to be piling implement a function or method to jump to pile implement the principles and Patching similar. But moneky not thread-safe, can not be used in concurrent tests

For more than a few frame, a large God has made a detailed presentation on the book Jane

Here are links

convey

https://www.jianshu.com/p/e3b2b1194830

gomock

https://www.jianshu.com/p/f4e773a1b11f

monkey

 https://www.jianshu.com/p/2f675d5e334e

gostub

https://www.jianshu.com/p/70a93a9ed186

Brief mention here convey

Generally convey + monkey or combination convey + gostub

package tests

import (
    "errors"
    . "github.com/smartystreets/goconvey/convey"
    "testing"
)

func Func(arg string) error {
    if len(arg) > 0 {
        return nil
    } else {
        return errors.New("arg is nil")
    }
}

func TestFunc(t *testing.T) {
    Convey("test Func", t, func() {
        Convey("Func should return nil when arg is not empty", func() {
            arg := "1"
            err := Func(arg)
            So(err, ShouldBeNil)
        })
        Convey("Func should return error when arg is empty", func() {
            arg := ""
            exceptErr := errors.New("arg is nil")
            err := Func(arg)
            So(err, ShouldBeError, exceptErr)
        })
    })
}

The main use of the two functions Convey and So function

convey function can be nested, the first parameter is a description of the test case, the second parameters differ. Convery outer second parameter must testing.T pointer. The third parameter is the function to save the other test cases. The second parameter is the inner convey test execution function

So the function of the function return value is used to make the determination. It provides many types, ShouldBeNil, ShouldBeERRor, ShouldBeEmpty and so on. Basically to cover

 

Guess you like

Origin www.cnblogs.com/lgh344902118/p/11876714.html