Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
192 lines (143 sloc) 3.39 KB

Gunit - Go Unit Test

@(Gunit Document)[go|design]

Gunit add rich Assert and Expect checker for go test.

Rich Output

Install

go get github.com/emptyland/akino/gunit

Add a Test Suite

package foo

import(
    "testing"

    "github.com/emptyland/akino/gunit"
)

// Your suite struct
type MySuite struct {
    a int
    b int
    t string
}

// Suite entry
func Test(t *testing.T) {
    gunit.RunTest(&MySuite{}, t)
}

// Optional: before suite
func (self *MySuite) Before() {
}

// Optional: after suite
func (self *MySuite) After() {
}

This is a simple test suite for gunit.

Test source files like go test file, it must be named xxxx_test.go

How to run it?

Run all test:

go test

Run one test suite:

go test -gunit.test=<suiteName>

Run specified test case:

go test -gunit.test=<suiteName.caseName>

The order of suite running:

-- suite scope
suite.Before()

    -- test 1 scope
    suite.SetUp()
    suite.TestXXX1
    suite.TearDown()
    -- test 1 end

    -- test 2 scope
    suite.SetUp()
    suite.TestXXX2
    suite.TearDown()
    -- test 2 end

suite.After()
-- suite end

In a suite: Before()/After() run only once.

You should add case to your suite for testing.

Add Test Cases

// Optional: test case setup
func (self *MySuite) SetUp(c *gunit.Case) {
    self.a = 314
    self.b = 216
    self.t = "hello"
}

// Optional: test case teardown
func (self *MySuite) TearDown(c *gunit.Case) {
    *self = MySuite{} // clear the suite
}

// Test case must be prefix with Test.
func (self *MySuite) TestSanity(c *gunit.Case) {
    c.AssertEquals(314, self.a)
    c.AssertEquals(216, self.b)
}

The test case must named TestXXX. case function has only one argument: *gunit.Case.

The SetUp and TearDown function are both optional.

Value Check

Check equals:

func (self *Case)AssertEquals(lhs, rhs interface{})

func (self *Case)ExpectEquals(lhs, rhs interface{})

Check true or false:

func (self *Case)AssertTrue(rhs bool)
func (self *Case)AssertFalse(rhs bool)

func (self *Case)ExpectTrue(rhs bool)
func (self *Case)ExpectFalse(rhs bool)

Check nil or NOT nil:

func (self *Case)AssertNil(rhs interface{})
func (self *Case)AssertNotNil(rhs interface{})

func (self *Case)ExpectNil(rhs interface{})
func (self *Case)ExpectNotNil(rhs interface{})

Assert can break test case, but Expect not.

How to check multi-return function?

Your can use the Batch checker:

func (self *MySuite) TestBatchCheck(c *gunit.Case) {
    c.Batch(foo()).
        AssertEquals(1).    // check first value
        Skip(1).            // skip second value
        AssertEquals("ok"). // check third value
        AssertNil()         // and last one
}

func foo() (int, int, string, error) {
    return 1, -1, "ok", nil
}

Panic Test

Expect panic will be happen:

func (self *Case) ExpectPanic(lhs interface{})

Assert panic:

func (self *Case) AssertPanic(lhs, fn func())

Example:

func (self *MySuite) TestExpectPanic(c *gunit.Case) {
    defer c.ExpectPanic("panic!") // must be in `defer` statement

    panic("panic!")
}

func (self *MySuite) TestScopedPanic(c *gunit.Case) {
    c.AssertPanic("error!", func () {
        c.T.Log("panic incoming!")
        panic("error!")
    })
}
You can’t perform that action at this time.