gounit

package module
v0.0.0-...-103f89b Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2015 License: MIT Imports: 7 Imported by: 0

README

gounit

Package gounit implements xunit for Go (along with some other goodies).

http://en.wikipedia.org/wiki/XUnit

(No attempt has yet been made to produce XUnit-style XML output.)

Installation:

go get -u "github.com/mdwhatcott/gounit"

That command will take some time because gounit depends on github.com/smartystreets/goconvey/convey/assertions, a really handy set of assertions. That assertions package is part of the goconvey testing project, which has a lot more going on and is somewhat large. Have no fear though, because you only need import the package in your *_test.go files your finished binaries will have no trace of any of these testing dependencies.

import . "github.com/mdwhatcott/gounit"

Notice the '.' which brings all exported gounit names into your testing environment. This removes lots of clutter from your test and will allow you to see the important parts of your tests more easily.

Complete Example Test Function:

package examples

import (
    "testing"

    . "github.com/mdwhatcott/gounit"
)

func Test(t *testing.T) {
    var game *Game

    f := NewFixture("Bowling Game Score", t)
    defer f.Run()

    f.Setup(func() {
        game = NewGame()
    })
    f.Test("After rolling all gutter balls", func() {
        game.rollMany(0, 20)
        f.So("No points will be earned", game.Score(), ShouldEqual, 0)
    })
    f.Test("All balls knock down a single pin--score of 20", func() {
        game.rollMany(1, 20)
        f.So("Each roll will score a point", game.Score(), ShouldEqual, 20)
    })
    f.SkipTest("Spare earns bonus", func() {
        game.Roll(3)
        game.Roll(7)
        game.Roll(3)
        game.rollMany(0, 17)
        f.So("The roll after the spare should be counted twice",
            game.Score(), ShouldEqual, 16)
    })
}

Corresponding Output:

$ go test -v
=== RUN Test
--- FAIL: Test (0.00s)
    gounit.go:208: Bowling Game Score
         -> "After rolling all gutter balls"
            + No points will be earned
        
            ************************************************************************
        
            FAILED: "No points will be earned"
        
            Expected: '0'
            Actual:   '1'
            (Should be equal)
        
        
            /Users/mike/src/github.com/mdwhatcott/gounit/examples/bowling_test.go:20
        
            ************************************************************************
        
         -> "All balls knock down a single pin--score of 20"
            + Each roll will score a point
         -> (skipped) "Spare earns bonus"

FAIL
exit status 1
FAIL    github.com/mdwhatcott/gounit/examples   0.007s

Documentation:

godoc.org/github.com/mdwhatcott/gounit

Documentation

Overview

Package gounit implements xunit for Go (along with some other goodies).

http://en.wikipedia.org/wiki/XUnit

(No attempt has yet been made to produce XUnit-style XML output.)

Index

Constants

This section is empty.

Variables

View Source
var (
	So                   = assertions.So
	ShouldEqual          = assertions.ShouldEqual
	ShouldNotEqual       = assertions.ShouldNotEqual
	ShouldAlmostEqual    = assertions.ShouldAlmostEqual
	ShouldNotAlmostEqual = assertions.ShouldNotAlmostEqual
	ShouldResemble       = assertions.ShouldResemble
	ShouldNotResemble    = assertions.ShouldNotResemble
	ShouldPointTo        = assertions.ShouldPointTo
	ShouldNotPointTo     = assertions.ShouldNotPointTo
	ShouldBeNil          = assertions.ShouldBeNil
	ShouldNotBeNil       = assertions.ShouldNotBeNil
	ShouldBeTrue         = assertions.ShouldBeTrue
	ShouldBeFalse        = assertions.ShouldBeFalse
	ShouldBeZeroValue    = assertions.ShouldBeZeroValue

	ShouldBeGreaterThan          = assertions.ShouldBeGreaterThan
	ShouldBeGreaterThanOrEqualTo = assertions.ShouldBeGreaterThanOrEqualTo
	ShouldBeLessThan             = assertions.ShouldBeLessThan
	ShouldBeLessThanOrEqualTo    = assertions.ShouldBeLessThanOrEqualTo
	ShouldBeBetween              = assertions.ShouldBeBetween
	ShouldNotBeBetween           = assertions.ShouldNotBeBetween
	ShouldBeBetweenOrEqual       = assertions.ShouldBeBetweenOrEqual
	ShouldNotBeBetweenOrEqual    = assertions.ShouldNotBeBetweenOrEqual

	ShouldContain    = assertions.ShouldContain
	ShouldNotContain = assertions.ShouldNotContain
	ShouldBeIn       = assertions.ShouldBeIn
	ShouldNotBeIn    = assertions.ShouldNotBeIn
	ShouldBeEmpty    = assertions.ShouldBeEmpty
	ShouldNotBeEmpty = assertions.ShouldNotBeEmpty

	ShouldStartWith           = assertions.ShouldStartWith
	ShouldNotStartWith        = assertions.ShouldNotStartWith
	ShouldEndWith             = assertions.ShouldEndWith
	ShouldNotEndWith          = assertions.ShouldNotEndWith
	ShouldBeBlank             = assertions.ShouldBeBlank
	ShouldNotBeBlank          = assertions.ShouldNotBeBlank
	ShouldContainSubstring    = assertions.ShouldContainSubstring
	ShouldNotContainSubstring = assertions.ShouldNotContainSubstring

	ShouldPanic        = assertions.ShouldPanic
	ShouldNotPanic     = assertions.ShouldNotPanic
	ShouldPanicWith    = assertions.ShouldPanicWith
	ShouldNotPanicWith = assertions.ShouldNotPanicWith

	ShouldHaveSameTypeAs    = assertions.ShouldHaveSameTypeAs
	ShouldNotHaveSameTypeAs = assertions.ShouldNotHaveSameTypeAs
	ShouldImplement         = assertions.ShouldImplement
	ShouldNotImplement      = assertions.ShouldNotImplement

	ShouldHappenBefore         = assertions.ShouldHappenBefore
	ShouldHappenOnOrBefore     = assertions.ShouldHappenOnOrBefore
	ShouldHappenAfter          = assertions.ShouldHappenAfter
	ShouldHappenOnOrAfter      = assertions.ShouldHappenOnOrAfter
	ShouldHappenBetween        = assertions.ShouldHappenBetween
	ShouldHappenOnOrBetween    = assertions.ShouldHappenOnOrBetween
	ShouldNotHappenOnOrBetween = assertions.ShouldNotHappenOnOrBetween
	ShouldHappenWithin         = assertions.ShouldHappenWithin
	ShouldNotHappenWithin      = assertions.ShouldNotHappenWithin
	ShouldBeChronological      = assertions.ShouldBeChronological
)

Functions

This section is empty.

Types

type A

type A func(
	description string,
	actual interface{},
	so func(actual interface{}, expected ...interface{}) string,
	expected ...interface{},
)

A represents an abbreviation of the function signatures implemented by the functions in `github.com/smartystreets/goconvey/convey/assertions`.

type Fixture

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

A simple xunit-style test fixture. Call NewFixture to create one.

func NewFixture

func NewFixture(description string, t T) *Fixture

NewFixture creates a new test fixture. Now you can call the attached methods to register and run test cases and optional setup and teardown functions. Because these methods return their receiver you have the option to chain the method calls if you like that sort of thing (I know I do).

func SkipNewFixture

func SkipNewFixture(description string, t T) *Fixture

func (*Fixture) FocusGoTest

func (self *Fixture) FocusGoTest(description string, action func(func()))

FocusGoTest registers a test to be run instead of any other tests not registered with this function. It is analogous to FocusTest and is meant for concurrent scenarios. A call of this function is meant to aid debugging and development and should be replaced with a call to the Test function as soon as possible.

func (*Fixture) FocusTest

func (self *Fixture) FocusTest(description string, action func())

FocusTest registers a test to be run instead of any other tests not registered with this function. A call of this function is meant to aid debugging and development and should be replaced with a call to the Test function as soon as possible.

func (*Fixture) GoTest

func (self *Fixture) GoTest(description string, action func(func()))

GoTest registers a test case, to be run after any registered setup and before any registered teardown. Use GoTest in favor of the Test function when your action launches another goroutine, thus relenting flow of execution from your code back to this library. To avoid the teardown or additional test cases running away before your code finishes, call the done func() passed into the action as its last instruction. Test cases must have unique descriptions within the context of a Fixture.

func (*Fixture) Log

func (self *Fixture) Log(args ...interface{})

func (*Fixture) Logf

func (self *Fixture) Logf(message string, args ...interface{})

func (*Fixture) Run

func (self *Fixture) Run()

Run iterates all test cases performing the following steps: - If registered, run the setup function. - Run the test case. - If registered, run the teardown function.

func (*Fixture) Setup

func (self *Fixture) Setup(action func())

Setup registers a function to be run before any and all test cases. Subsequent calls to this function overwrite the previously registered setup function.

func (*Fixture) SkipGoTest

func (self *Fixture) SkipGoTest(description string, action func(func()))

SkipGoTest registers a test case to be logged in test output but it will not be executed. It is analogous to SkipTest and is meant for concurrent scenarios. A call of this function is meant to aid debugging and development and should be replaced with a call to the Test function as soon as possible.

func (*Fixture) SkipSo

func (self *Fixture) SkipSo(description string, actual interface{}, so func(actual interface{}, expected ...interface{}) string, expected ...interface{})

func (*Fixture) SkipTest

func (self *Fixture) SkipTest(description string, action func())

SkipTest registers a test case to be logged in test output but it will not be executed. A call of this function is meant to aid debugging and development and should be replaced with a call to the Test function as soon as possible.

func (*Fixture) So

func (self *Fixture) So(description string, actual interface{}, so func(actual interface{}, expected ...interface{}) string, expected ...interface{})

This method stands in as a 'So' call with a required description-- (a-la-`github.com/smartystreets/goconvey/convey/assertions.So`)

func (*Fixture) Teardown

func (self *Fixture) Teardown(action func())

Teardown registers a function to be run after any and all test cases, even when test cases panic. Subsequent calls to this function overwrite the previously registered teardown function.

func (*Fixture) Test

func (self *Fixture) Test(description string, action func())

Test registers a test case, to be run after any registered setup and before any registered teardown. Test cases must have unique descriptions within the context of a Fixture.

type T

type T interface {
	Fail()
	SkipNow()
	Log(...interface{})
}

T contains the methods we use on the testing.T that is passed into the fixture. Using this interface instead of the testing.T directly allows for easier verification of correct behavior via automated testing.

Jump to

Keyboard shortcuts

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