gotest

package module
v0.0.0-...-5c28cf3 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2017 License: MIT Imports: 5 Imported by: 0

README

GoTest

Build Status Issue Count

Package gotest provides rich assertions for use within and beyond Go's testing package.

GoTest plays well with "vernacular" Go testing tests, providing a rich set of assertions to test HTTP Responses, JSON and JSON:API data, general equality, numeric comparison, collections, strings, panics, types, and time.

Look at these GoDocs for the latest documentation.

Quickstart

Grab the package and import it:

go get github.com/kindrid/gotest import "github.com/kindrid/gotest" import "github.com/kindrid/gotest/should"

Code normal testing-style tests, but use gotest.Assert and assertions found in should like this:

gotest.Assert(t, actualJson, should.HaveFields, "name", reflect.String, "children", reflect.Map, "hobbies", reflect.Slice)

Assertions are just functions that accept interfaces and return a non-empty string if there's an error. Look at gotest.should.Assertion for the details. Look at should/doc.go and should/assertion.go ofr more details.

Overview

GoTest plays well with "vernacular" Go testing tests, providing a rich set of assertions to test HTTP Responses, JSON and JSON:API data, general equality, numeric comparison, collections, strings, panics, types, and time.

Most of these rich assertions are provided by SmartyStreet's excellent assertion library (https://github.com/smartystreets/assertions) which builds off Aaron Jacobs' Oglematchers (https://github.com/jacobsa/oglematchers).

In addition, any SmartyTreets-style assertion can be used as is (see https://github.com/smartystreets/goconvey/wiki/Custom-Assertions).

Why

We like Go's stdlib testing because it's simple, fast, familiar to most Go coders, has good tooling support, benchmark support, and coverage support.

In earlier versions of Go, we missed subtests for test organization and a more BDD approach. We looked at GinkGo (github.com/onsi/ginkgo) and GoConvey (github.com/smartystreets/goconvey/convey)--both with benefits--and chose GoConvey because of the simple and consistent approach it took to writing custom assertions. See the documenataion for "gotest/should" for more details on that. It also had a pretty test runner.

But around the release of Go 1.7 we ran into some problems: the growth of parameterized (table-driven) tests in our code didn't play well with GoConvey. GoConvey's approach to building test suites made it hard to focus specific subtests.

We also ran into an opportunity: testing now supported subtests. See https://godoc.org/testing#hdr-Subtests_and_Sub_benchmarks. We were able to drop a lot of fancy suite construction code and gain easier focussing on particular tests. But we had come to appreciate GoConvey's excellent assertion pattern. We considered moving to github.com/stretchr/testify and used it's pattern for the custom assertion wrapper (see Assert below). It also had a simple custom-assertion pattern, but GoConvey's seemed simpler and more useful. We took a sideways glance at GUnit (github.com/smartystreets/gunit) and Labix's GoCheck (gopkg.in/check.v1 ). Very cool packages, but we wanted to stay closer to testing with its new versatility.

Documentation

Overview

Package gotest provides rich assertions for use within and beyond Go's `testing` package.

Quickstart

Grab the package and import it:

go get github.com/kindrid/gotest
import "github.com/kindrid/gotest"
import "github.com/kindrid/gotest/should"

Code normal `testing`-style tests, but use `gotest.Assert` and assertions found in `should` like this:

gotest.Assert(t, actualJson, should.HaveFields,
  "name", reflect.String,
  "children", reflect.Map,
  "hobbies", reflect.Slice)

Assertions are just functions that accept interfaces and return a non-empty string if there's an error. Look at `gotest.should.Assertion` for the details. Look at `should/doc.go` and `should/assertion.go` ofr more details.

Gotest adds some command-line options to your tests. You can see them via `go test . -args -help`. Note that gotest has its own verbosity flag which controls different aspects than `-test.v`.

Overview

GoTest plays well with "vernacular" Go `testing` tests, providing a rich set of assertions to test HTTP Responses, JSON and JSON:API data, general equality, numeric comparison, collections, strings, panics, types, and time.

Most of these rich assertions are provided by SmartyStreet's excellent assertion library (https://github.com/smartystreets/assertions) which builds off Aaron Jacobs' Oglematchers (https://github.com/jacobsa/oglematchers).

In addition, any SmartyTreets-style assertion can be used as is (see https://github.com/smartystreets/goconvey/wiki/Custom-Assertions).

Why

We like Go's stdlib `testing` because it's simple, fast, familiar to most Go coders, has good tooling support, benchmark support, and coverage support.

In earlier versions of Go, we missed subtests for test organization and a more BDD approach. We looked at GinkGo (`github.com/onsi/ginkgo`) and GoConvey (github.com/smartystreets/goconvey/convey)--both with benefits--and chose GoConvey because of the simple and consistent approach it took to writing custom assertions. See the documenataion for "gotest/should" for more details on that. It also had a pretty test runner.

But around the release of Go 1.7 we ran into some problems: the growth of parameterized (table-driven) tests in our code didn't play well with GoConvey. GoConvey's approach to building test suites made it hard to focus specific subtests.

We also ran into an opportunity: `testing` now supported subtests. See https://godoc.org/testing#hdr-Subtests_and_Sub_benchmarks. We were able to drop a lot of fancy suite construction code and gain easier focussing on particular tests. But we had come to appreciate GoConvey's excellent assertion pattern. We considered moving to `github.com/stretchr/testify` and used it's pattern for the custom assertion wrapper (see `Assert` below). It also had a simple custom-assertion pattern, but GoConvey's seemed simpler and more useful. We took a sideways glance at GUnit (`github.com/smartystreets/gunit`) and Labix's GoCheck (`gopkg.in/check.v1 `). Very cool packages, but we wanted to stay closer to `testing` with its new versatility.

Index

Constants

View Source
const (
	Silent    = iota - 1 // (hopefully) no output except panics
	Short                // Shows the first line of the failure string (up to \n) and info to re-run the particular tests
	Long                 // Add the next level of failure string (up to \n\n)
	Actuals              // Add the entire failure string and a (possibly shortened) representation of the actual value
	Expecteds            // Add  (possibly shortened) representation(s) of  expected values.
	Debug                // Adds granular information to successes as well as failures. This level may inject flags into the tested item to make it more verbose.
	Insane               // Adds information to test meta concerns, such as logic within assertions.
)

// Verbosity Levels: these are conventions only. Assertions and test functions can interpret these however they want. Stack traces, however should be controlled by the StackDepth variable.

Variables

View Source
var FailFast bool

FailFast halts testing with testing.FailNow() upon the first error. This will usually result in less output and quicker failures, but read the `testing.FailNow() to understand its limitations

View Source
var StackDepth int

StackDepth sets the maximum stack depth reported with errors. 0 disables. It is puporsefully public so tests using this library can manipulate it and check it.

View Source
var StackLevel int

StackLevel sets the number of stack frames to ignore before printing out stack traces. This can help remove some extraneous levels from within test runners and utility functions.

View Source
var Verbosity int

Verbosity sets a level of "chattiness" for the tests. It is puporsefully public so tests using this library can manipulate it and check it.

View Source
var (
	Version = "1.1.0"
)

Functions

func Assert

func Assert(t T, actual interface{}, assertion should.Assertion, expected ...interface{})

Assert wraps any standard Assertion for use with Go's std.testing library.

func Deny

func Deny(t T, actual interface{}, assertion should.Assertion, expected ...interface{})

Deny negates any standard Assertion for use with Go's std.testing library. You may also want to use should.Not()--it will give more accurate reporting.

func Inspectv

func Inspectv(minLevel int, label string, inspected ...interface{}) (result string)

Inspectv returns a detailed introspection of objects if Verbosity >= minLevel.

func Later

func Later(t T, desc string, ignored ...interface{})

Later describes pending tests.

func RegisterFlags

func RegisterFlags(prefix string)

RegisterFlags adds CLI flags to tailor these testing parameters. Call this function within your test code's init(). Use them with gotest -args. For example `gotest . -args -gotest-depth 5`.

func Sprintv

func Sprintv(minLevel int, format string, args ...interface{}) string

Sprintv formats a string if Verbosity >= minLevel, otherwise returns ""

func Vocal

func Vocal(minLevel int) bool

Vocal makes an easy way to gate operations by verbosity level. It returns true if Verbosity is < minLevel.

Types

type T

type T interface {
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fail()
	FailNow() // exit the current test immediately
	Logf(format string, args ...interface{})
	Name() string // need Go 1.8 for this.
}

T describes the interface provided by Go's std.testing.T. If only they had made that an interface!

Directories

Path Synopsis
Package rest adds capabilities for testing restful services.
Package rest adds capabilities for testing restful services.
describers
Package describers holds different sources of restful scenarios
Package describers holds different sources of restful scenarios
Package should gathers helpful tests (a.k.a.
Package should gathers helpful tests (a.k.a.

Jump to

Keyboard shortcuts

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