testig

package module
v0.0.0-...-f88fca9 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2016 License: BSD-3-Clause Imports: 7 Imported by: 0

README

Testig -- Go Test Helpers

GoDoc Build Status Coverage Status

What?

Helpers for Go testing. May they help you thoroughly test your Go code!

The name testig is a pun in two languages, as well as an experiment in misspelling.

Why?

I find myself with shared testing code across projects, which I do not wish to have scattered about like that. Also, you may find it useful in your own projects.

Note that some of this stuff may eventually be merged into other packages (see below); or may be marked deprecated as dumb if I find something much better.

Who?

(c) 2016 Kevin Frost; BSD license (cf. the LICENSE file).

This package leans heavily on the assert package by Mat Ryer and Tyler Bunnel (et al.), without which I probably wouldn't have even stuck with Go long enough to care about this. Thanks guys!

Documentation

Overview

Package testig provides helpers for testing Go programs, including itself. The spelling of the package name is intentional.

LIMITATIONS

There is currently no way for a TestTester to terminate a test function under test. At the moment it doesn't seem worth the extra complexity of running tests in a separate goroutine, as the workaround also happens to be a useful practice: helper functions must not have any ability to continue after a Fail or Skip.

Example
package main

import (
	"fmt"

	"github.com/biztos/testig"
)

var DeterministicFailure = true

func main() {

	// This will of course also accept a *testing.T as its argument.
	AwesomeTestingHelper := func(t testig.TT) {
		if DeterministicFailure {
			t.Fatal("uh-oh spaghettio!")
		} else {
			t.Log("Things are looking up!")
		}
	}

	tt := testig.NewTestTester()
	AwesomeTestingHelper(tt)

	if tt.Stopped {
		fmt.Println("Failed:", tt.Failed())
		fmt.Println("Skipped:", tt.Skipped())
		fmt.Println(tt.Logs)
	}
}
Output:

Failed: true
Skipped: false
[uh-oh spaghettio!]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertPanicsRegexp

func AssertPanicsRegexp(t TT, f func(), exp string, msgAndArgs ...interface{})

AssertPanicsRegexp fails with msgAndArgs and stops test execution unless the function f panics with a string matching the regular expression exp, which must compile. It is safe to omit msgAndArgs.

func AssertPanicsWith

func AssertPanicsWith(t TT, f func(), exp string, msgAndArgs ...interface{})

AssertPanicsWith fails with msgAndArgs and stops test execution unless the function f panics with string exp. It is safe to omit msgAndArgs.

Example
package main

import (
	"fmt"

	"github.com/biztos/testig"
)

func main() {

	panickyFunc := func() { panic("oh no") }

	tt := testig.NewTestTester()

	testig.AssertPanicsWith(tt, panickyFunc, "oh no", "got a scary panic")
	fmt.Println("Failed:", tt.Failed())

	testig.AssertPanicsWith(tt, panickyFunc, "other panic", "got another")
	fmt.Println("Failed:", tt.Failed())

	// Also catches non-panics as you would expect.
	dontPanic := func() { return }
	tt = testig.NewTestTester()
	fmt.Println("Failed:", tt.Failed())

	testig.AssertPanicsWith(tt, dontPanic, "uh oh", "and another")
	fmt.Println("Failed:", tt.Failed())

}
Output:

Failed: false
Failed: true
Failed: false
Failed: true

Types

type OutputRecorder

type OutputRecorder struct {
	Stdout   *bufio.Writer
	Stderr   *bufio.Writer
	Exited   bool
	ExitCode int
	ExitTime time.Time
	// contains filtered or unexported fields
}

OutputRecorder allows recording and inspection of output to the normal channels Stdout and Stderr, as well as capture of the exit code.

func NewOutputRecorder

func NewOutputRecorder() *OutputRecorder

NewOutputRecorder returns an initialied OutputRecorder ready for use.

func (*OutputRecorder) Exit

func (r *OutputRecorder) Exit(code int)

Exit is a function suitable for overriding os.Exit. If Exit is called more than once it panics: in order for exiting functions to be reasonably testable, they must not assume their exit calls actually terminate the program.

func (*OutputRecorder) ExitString

func (r *OutputRecorder) ExitString() string

ExitString stringifies the exit status.

func (*OutputRecorder) StderrString

func (r *OutputRecorder) StderrString() string

StderrString returns a string of all written to standard error so far.

func (*OutputRecorder) StdoutString

func (r *OutputRecorder) StdoutString() string

StdoutString returns a string of all written to standard output so far.

type TT

type TT interface {
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fail()
	FailNow()
	Failed() bool
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Log(args ...interface{})
	Logf(format string, args ...interface{})
	Skip(args ...interface{})
	SkipNow()
	Skipf(format string, args ...interface{})
	Skipped() bool
}

TT defines an interface implemented by both the TestTester and testing.T (and for that matter testing.B).

In order to be testable within this package, helper functions should accept a TT argument instead of a *testing.T:

func TestHelper(t TT) {
 ...
}

This awful hack is necessary because the Go authors explicitly made it impossible to implement the testing.TB interface.

type TestTester

type TestTester struct {
	Logs    []string
	Stopped bool
	// contains filtered or unexported fields
}

TestTester implements the TT interface in a way that helps us test our test functions. It should be used to run a single test function. NOTE: it does not (yet) actually stop execution!

func NewTestTester

func NewTestTester() *TestTester

NewTestTester returns a new TestTester ready for testing tests.

func (*TestTester) Error

func (tt *TestTester) Error(args ...interface{})

Error mirrors the same-named function in testing.T: it is equivalent to Log followed by Fail.

func (*TestTester) Errorf

func (tt *TestTester) Errorf(format string, args ...interface{})

Errorf mirrors the same-named function in testing.T: it is equivalent to Logf followed by Fail.

func (*TestTester) Fail

func (tt *TestTester) Fail()

Fail mirrors the same-named function in testing.T: it marks the function as having failed but continues execution.

func (*TestTester) FailNow

func (tt *TestTester) FailNow()

FailNow mirrors the same-named function in testing.T: it marks the function as having failed and sets the TestTester's Stopped property to true.

func (*TestTester) Failed

func (tt *TestTester) Failed() bool

Failed mirrors the same-named function in testing.T: it reports whether the function has failed.

func (*TestTester) Fatal

func (tt *TestTester) Fatal(args ...interface{})

Fatal mirrors the same-named function in testing.T: it is equivalent to Log followed by FailNow.

func (*TestTester) Fatalf

func (tt *TestTester) Fatalf(format string, args ...interface{})

Fatalf mirrors the same-named function in testing.T: it is equivalent to Logf followed by FailNow.

func (*TestTester) Log

func (tt *TestTester) Log(args ...interface{})

Log mirrors the same-named function in testing.T: it records a log event a la Println.

func (*TestTester) Logf

func (tt *TestTester) Logf(format string, args ...interface{})

Logf mirrors the same-named function in testing.T: it records a log event a la Printf.

func (*TestTester) Skip

func (tt *TestTester) Skip(args ...interface{})

Skip mirrors the same-named function in testing.T: it is equivalent to Log followed by SkipNow.

func (*TestTester) SkipNow

func (tt *TestTester) SkipNow()

SkipNow mirrors the same-named function in testing.T: it marks the test as having been skipped and sets the TestTester's Stopped property to true.

func (*TestTester) Skipf

func (tt *TestTester) Skipf(format string, args ...interface{})

Skipf mirrors the same-named function in testing.T: it is equivalent to Logf followed by SkipNow.

func (*TestTester) Skipped

func (tt *TestTester) Skipped() bool

Skipped mirrors the same-named function in testing.T: it reports whether the test was skipped.

Jump to

Keyboard shortcuts

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