gunit

package
v0.0.0-...-c3af2b6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2014 License: BSD-2-Clause Imports: 14 Imported by: 0

README

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!")
    })
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunTest

func RunTest(unit interface{}, t *testing.T)

Types

type Batch

type Batch struct {
	// contains filtered or unexported fields
}

func (*Batch) AssertEquals

func (self *Batch) AssertEquals(lhs interface{}) *Batch

func (*Batch) AssertFalse

func (self *Batch) AssertFalse() *Batch

func (*Batch) AssertNil

func (self *Batch) AssertNil() *Batch

func (*Batch) AssertNotNil

func (self *Batch) AssertNotNil() *Batch

func (*Batch) AssertTrue

func (self *Batch) AssertTrue() *Batch

func (*Batch) ExpectEquals

func (self *Batch) ExpectEquals(lhs interface{}) *Batch

func (*Batch) ExpectFalse

func (self *Batch) ExpectFalse() *Batch

func (*Batch) ExpectNil

func (self *Batch) ExpectNil() *Batch

func (*Batch) ExpectNotNil

func (self *Batch) ExpectNotNil() *Batch

func (*Batch) ExpectTrue

func (self *Batch) ExpectTrue() *Batch

func (*Batch) Skip

func (self *Batch) Skip(offset int) *Batch

type Case

type Case struct {
	T *testing.T
	// contains filtered or unexported fields
}

func (*Case) AssertEquals

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

func (*Case) AssertFalse

func (self *Case) AssertFalse(rhs bool)

func (*Case) AssertNil

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

func (*Case) AssertNotNil

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

func (*Case) AssertPanic

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

func (*Case) AssertPanicCall

func (self *Case) AssertPanicCall(lhs, call interface{}, args ...interface{})

func (*Case) AssertTrue

func (self *Case) AssertTrue(rhs bool)

func (*Case) Batch

func (self *Case) Batch(returnVals ...interface{}) *Batch

Accept all func return values for check.

func (*Case) ExpectEquals

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

func (*Case) ExpectFalse

func (self *Case) ExpectFalse(rhs bool)

func (*Case) ExpectNil

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

func (*Case) ExpectNotNil

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

func (*Case) ExpectPanic

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

Call the func MUST to be in defer statement.

func (*Case) ExpectTrue

func (self *Case) ExpectTrue(rhs bool)

func (*Case) Fail

func (self *Case) Fail()

func (*Case) Run

func (self *Case) Run(fn func(c *Case)) (fail bool)

func (*Case) String

func (self *Case) String() string

func (*Case) What

func (self *Case) What(format string, args ...interface{}) *Case

type Test

type Test struct {
	// contains filtered or unexported fields
}

func (*Test) Run

func (self *Test) Run()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL