Skip to content

How to write a TestCase

xushiwei edited this page Jul 10, 2020 · 8 revisions

Suppose we have a function named foo to test:

func foo(arg1 Type1, arg2 Type2, ..., argN TypeN) (ret1 RetType1, ret2 RetType2, ..., retM RetTypeM)

Normally we test it like this:

func TestXXX(t *testing.T) {
    ret1, ret2, ..., retM := foo(arg1, arg2, ..., argN)
    if ret1 != expect1 {
        t.Fatal("TestXXX failed: ret1 != expect1 -", ret1, expect1)
    }
    if ret2 != expect2 {
        t.Fatal("TestXXX failed: ret2 != expect2 -", ret2, expect2)
    }
    ...
    if retM != expectM {
        t.Fatal("TestXXX failed: retM != expectM -", retM, expectM)
    }
}

By using github.com/qiniu/x/ts, we can test it like this:

import "github.com/qiniu/x/ts"

func TestXXX(test *testing.T) {
    t := ts.New(test)
    t.Call(foo, arg1, arg2, ..., argN).Equal(expect1).Next().Equal(expect2)...Next().Equal(expectM)
}
Clone this wiki locally