testing

package
v0.0.0-...-e758773 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2011 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package testing provides support for automated testing of Go packages. It is intended to be used in concert with the “gotest” utility, which automates execution of any function of the form

func TestXxx(*testing.T)

where Xxx can be any alphanumeric string (but the first letter must not be in [a-z]) and serves to identify the test routine. These TestXxx routines should be declared within the package they are testing.

Functions of the form

func BenchmarkXxx(*testing.B)

are considered benchmarks, and are executed by gotest when the -test.bench flag is provided.

A sample benchmark function looks like this:

func BenchmarkHello(b *testing.B) {
    for i := 0; i < b.N; i++ {
        fmt.Sprintf("hello")
    }
}

The benchmark package will vary b.N until the benchmark function lasts long enough to be timed reliably. The output

testing.BenchmarkHello	500000	      4076 ns/op

means that the loop ran 500000 times at a speed of 4076 ns per loop.

If a benchmark needs some expensive setup before running, the timer may be stopped:

func BenchmarkBigLen(b *testing.B) {
    b.StopTimer()
    big := NewBig()
    b.StartTimer()
    for i := 0; i < b.N; i++ {
        big.Len()
    }
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Main

func Main(matchString func(pat, str string) (bool, os.Error), tests []InternalTest, benchmarks []InternalBenchmark)

An internal function but exported because it is cross-package; part of the implementation of gotest.

func RunBenchmarks

func RunBenchmarks(matchString func(pat, str string) (bool, os.Error), benchmarks []InternalBenchmark)

An internal function but exported because it is cross-package; part of the implementation of gotest.

func RunTests

func RunTests(matchString func(pat, str string) (bool, os.Error), tests []InternalTest)

func Short

func Short() bool

Short reports whether the -test.short flag is set.

Types

type B

type B struct {
	N int
	// contains filtered or unexported fields
}

B is a type passed to Benchmark functions to manage benchmark timing and to specify the number of iterations to run.

func (*B) ResetTimer

func (b *B) ResetTimer()

ResetTimer stops the timer and sets the elapsed benchmark time to zero.

func (*B) SetBytes

func (b *B) SetBytes(n int64)

SetBytes records the number of bytes processed in a single operation. If this is called, the benchmark will report ns/op and MB/s.

func (*B) StartTimer

func (b *B) StartTimer()

StartTimer starts timing a test. This function is called automatically before a benchmark starts, but it can also used to resume timing after a call to StopTimer.

func (*B) StopTimer

func (b *B) StopTimer()

StopTimer stops timing a test. This can be used to pause the timer while performing complex initialization that you don't want to measure.

type BenchmarkResult

type BenchmarkResult struct {
	N     int   // The number of iterations.
	Ns    int64 // The total time taken.
	Bytes int64 // Bytes processed in one iteration.
}

The results of a benchmark run.

func Benchmark

func Benchmark(f func(b *B)) BenchmarkResult

Benchmark benchmarks a single function. Useful for creating custom benchmarks that do not use gotest.

func (BenchmarkResult) NsPerOp

func (r BenchmarkResult) NsPerOp() int64

func (BenchmarkResult) String

func (r BenchmarkResult) String() string

type InternalBenchmark

type InternalBenchmark struct {
	Name string
	F    func(b *B)
}

An internal type but exported because it is cross-package; part of the implementation of gotest.

type InternalTest

type InternalTest struct {
	Name string
	F    func(*T)
}

An internal type but exported because it is cross-package; part of the implementation of gotest.

type T

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

T is a type passed to Test functions to manage test state and support formatted test logs. Logs are accumulated during execution and dumped to standard error when done.

func (*T) Error

func (t *T) Error(args ...interface{})

Error is equivalent to Log() followed by Fail().

func (*T) Errorf

func (t *T) Errorf(format string, args ...interface{})

Errorf is equivalent to Logf() followed by Fail().

func (*T) Fail

func (t *T) Fail()

Fail marks the Test function as having failed but continues execution.

func (*T) FailNow

func (t *T) FailNow()

FailNow marks the Test function as having failed and stops its execution. Execution will continue at the next Test.

func (*T) Failed

func (t *T) Failed() bool

Failed returns whether the Test function has failed.

func (*T) Fatal

func (t *T) Fatal(args ...interface{})

Fatal is equivalent to Log() followed by FailNow().

func (*T) Fatalf

func (t *T) Fatalf(format string, args ...interface{})

Fatalf is equivalent to Logf() followed by FailNow().

func (*T) Log

func (t *T) Log(args ...interface{})

Log formats its arguments using default formatting, analogous to Print(), and records the text in the error log.

func (*T) Logf

func (t *T) Logf(format string, args ...interface{})

Logf formats its arguments according to the format, analogous to Printf(), and records the text in the error log.

Directories

Path Synopsis
Package iotest implements Readers and Writers useful only for testing.
Package iotest implements Readers and Writers useful only for testing.
Package quick implements utility functions to help with black box testing.
Package quick implements utility functions to help with black box testing.
Package script aids in the testing of code that uses channels.
Package script aids in the testing of code that uses channels.

Jump to

Keyboard shortcuts

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