Documentation
¶
Overview ¶
Package check contains assertions to assist with unit testing.
Index ¶
- Constants
- Variables
- func PrintStack(depth int) string
- func RequireLabel(t TestSkipper, required string)
- func RunTargetted(t TestSkipper, r Runnable)
- func ThatDoesNotPanic(t Tester, f func())
- func ThatPanicsAsExpected(t Tester, assertion PanicAssertion, f func())
- type Assertion
- type Interceptor
- type InterceptorStub
- type MockTester
- type Mutation
- type PanicAssertion
- type Predicate
- type Runnable
- type SingleCapture
- type TestCapture
- type TestSkipper
- type Tester
- type Timesert
Constants ¶
const DefaultWaitCheckInterval = 1 * time.Millisecond
DefaultWaitCheckInterval is the default value of the optional check interval passed to Wait().
const EnvGolabels = "GOLABELS"
EnvGolabels is the name of the environment variable used by RequireLabel.
Variables ¶
var ErrSimulated = errors.New("simulated")
ErrSimulated is a pre-canned error, useful in simulating faults.
Functions ¶
func PrintStack ¶
PrintStack prints the call stack, starting from the given depth.
func RequireLabel ¶
func RequireLabel(t TestSkipper, required string)
RequireLabel ensures that the given label is present in the value of the GOLABELS environment variable, where the latter is a comma-separated list of arbitrary labels, e.g. GOLABELS=prod,test. If the required label is absent, the test will be skipped.
func RunTargetted ¶
func RunTargetted(t TestSkipper, r Runnable)
RunTargetted runs a given function if it the test case was specified using a narrow-matching regular expression; for example, by running 'debug.test -test.run ^...$' or 'go test -run ^...$'. If tests were run without a regex, or with a regex designed to match several tests, the example will be skipped.
The purpose of RunTargetted is to enable the selective running of 'go doc' example snippets. These should not normally be run as part of package tests, CI builds and so on, but may occasionally be run from the IDE.
func ThatDoesNotPanic ¶
func ThatDoesNotPanic(t Tester, f func())
ThatDoesNotPanic ensures that the given function f returns without panicking. This is useful in tests that must perform multiple assertions without terminating the test. (Otherwise, if we let the panic through, the test will not run to completion.)
func ThatPanicsAsExpected ¶
func ThatPanicsAsExpected(t Tester, assertion PanicAssertion, f func())
ThatPanicsAsExpected checks that the given function f panics, where the trapped panic complies with the supplied assertion.
Types ¶
type Assertion ¶
type Assertion func(t Tester)
Assertion is a check that must pass for Timesert.UntilAsserted to return without raising an error.
type Interceptor ¶
type Interceptor interface { Tester }
Interceptor represents a mechanism for transforming the result of invoking Tester.Errorf(). This is useful when you need to modify the output of a Tester, for instance, to enrich it with additional information that is not available to the assertion framework at the point where an assertion fails.
type InterceptorStub ¶
type InterceptorStub struct {
// contains filtered or unexported fields
}
InterceptorStub is an element in a fluid chain.
func Intercept ¶
func Intercept(t Tester) InterceptorStub
Intercept starts a fluid chain for specifying testing mutations.
func (InterceptorStub) Mutate ¶
func (is InterceptorStub) Mutate(m Mutation) Interceptor
Mutate sets up the given mutation that will be triggered on Tester.Errorf(). To chain mutations, use the Then() method.
type MockTester ¶
type MockTester struct {
ErrorFunc func(format string, args ...interface{})
}
MockTester aids in the mocking of the Tester interface.
func (*MockTester) Errorf ¶
func (m *MockTester) Errorf(format string, args ...interface{})
Errorf feeds a formatted error message to the mocked tester.
type Mutation ¶
Mutation represents a transformation of the captured Tester.Errorf() invocation, where the formatted message is fed to a Mutation function, and the output is some transformation of the original.
func Append ¶
func Append(suffix interface{}) Mutation
Append adds a string to the end of the original captured message. The appended suffixed is delimited from the original capture with a single whitespace character.
type PanicAssertion ¶
type PanicAssertion func(t Tester, cause interface{})
PanicAssertion checks a given panic cause. It is used by ThatPanicsAsExpected.
func AnyCause ¶
func AnyCause() PanicAssertion
AnyCause satisfies the panic assertion irrespective of the cause. Use to test that a function panics without caring as to the nature of the panic.
func CauseEqual ¶
func CauseEqual(expected interface{}) PanicAssertion
CauseEqual checks that the panic is equal to the given cause.
func ErrorContaining ¶
func ErrorContaining(substr string) PanicAssertion
ErrorContaining checks that the panic is of the built-in error type, where the result of calling err.Error() contains the given substring.
func ErrorWithValue ¶
func ErrorWithValue(value string) PanicAssertion
ErrorWithValue checks that the panic is of the built-in error type, where the result of calling err.Error() matches the given value.
type Predicate ¶
type Predicate func() bool
Predicate is a condition that must be satisfied for Timesert.Until to return.
type Runnable ¶
type Runnable func()
Runnable in any no-arg function.
Typically, this is a 'go doc' example function, of the form —
func Example_suffix() { ... } func ExampleF_suffix() { ... } func ExampleT_suffix() { ... } func ExampleT_M_suffix() { ... }
type SingleCapture ¶
type SingleCapture interface { Captured() *string CapturedLines() []string NumCapturedLines() int AssertNil(t Tester) AssertNotNil(t Tester) AssertFirstLineEqual(t Tester, expected string) AssertFirstLineContains(t Tester, substr string) AssertContains(t Tester, substr string) }
SingleCapture represents one instance of the invocation of TestCapture.Errorf.
type TestCapture ¶
type TestCapture interface { Tester First() SingleCapture Capture(index int) SingleCapture Captures() []SingleCapture Length() int Reset() }
TestCapture provides a mechanism for capturing the results of tests. This is useful when testing assertion libraries. TestCapture is thread-safe; it may be invoked concurrently from multiple goroutines to capture test results.
func NewTestCapture ¶
func NewTestCapture() TestCapture
NewTestCapture creates a new TestCapture object.
type TestSkipper ¶
type TestSkipper interface {
Skip(args ...interface{})
}
TestSkipper is the API contract for testing.T.Skip().
type Tester ¶
type Tester interface {
Errorf(format string, args ...interface{})
}
Tester is an API-compatible stand-in for *testing.T.