xtest

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2019 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package xtest implements common functionality for unit tests. This includes support for assertions in multiple goroutines to Goconvey, and temporary test file/folder helpers.

Parallel goconvey blocks cannot contain other goconvey blocks.

Example:

func TestParallel(t *testing.T) {
  Convey("Test parallel goroutines", t, Parallel(func(sc *SC) {
    x := 1
    sc.SoMsg("x", x, ShouldEqual, 1)
  }, func(sc *SC) {
    y := 1
    sc.SoMsg("y", y, ShouldEqual, 1)
  }))
}

Note that inside parallel blocks, Convey methods should be explicitly invoked on the local convey object (e.g., sc).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertError added in v0.4.0

func AssertError(t *testing.T, err error, expectError bool)

AssertError checks that err is not nil if expectError is true and that is it nil otherwise

func AssertErrorsIs added in v0.4.0

func AssertErrorsIs(t *testing.T, actualErr, expectedErr error)

AssertErrorsIs checks that errors.Is(actualErr, expectedErr) returns true, if expectedErr is not nil.

func AssertReadDoesNotReturnBefore added in v0.3.0

func AssertReadDoesNotReturnBefore(t testing.TB, ch <-chan struct{}, timeout time.Duration)

AssertReadDoesNotReturnBefore will call t.Fatalf if the first read from the channel happens before timeout.

func AssertReadReturnsBefore added in v0.3.0

func AssertReadReturnsBefore(t testing.TB, ch <-chan struct{}, timeout time.Duration)

AssertReadReturnsBefore will call t.Fatalf if the first read from the channel doesn't happen before timeout.

func AssertReadReturnsBetween added in v0.3.0

func AssertReadReturnsBetween(t testing.TB, ch <-chan struct{}, x, y time.Duration)

AssertReadReturnsBetween will call t.Fatalf if the first read from the channel doesn't happen between x and y.

func ExpandPath

func ExpandPath(file string) string

ExpandPath returns testdata/file.

func FailOnErr

func FailOnErr(t testing.TB, err error, desc ...string)

FailOnErr causes t to exit with a fatal error if err is non-nil.

func MustMarshalJSONToFile

func MustMarshalJSONToFile(t testing.TB, v interface{}, baseName string)

MustMarshalJSONToFile marshals v and writes the result to file testdata/baseName. If the file exists, it is truncated; if it doesn't exist, it is created. On errors, t.Fatal() is called.

func MustParseAS

func MustParseAS(s string) addr.AS

MustParseAS parses s and returns the corresponding addr.AS object. It panics if s is not valid AS representation.

func MustParseHexString added in v0.2.0

func MustParseHexString(s string) common.RawBytes

MustParseHexString parses s and returns the corresponding byte slice. It panics if the decoding fails.

func MustParseIA

func MustParseIA(s string) addr.IA

MustParseIA parses s and returns the corresponding addr.IA object. It panics if s is not a valid ISD-AS representation.

func MustReadFromFile

func MustReadFromFile(t testing.TB, baseName string) []byte

MustReadFromFile reads testdata/baseName and returns the raw content. On errors, t.Fatal() is called.

func MustTempDir

func MustTempDir(dir, prefix string) (string, func())

MustTempDir creates a new temporary directory under dir with the specified prefix. If the function encounters an error it panics. The second return value is a clean-up function that can be called to recursively delete the entire directory.

func MustTempFileName

func MustTempFileName(dir, prefix string) string

MustTempFileName is a wrapper around TempFileName. The function panics if an error occurs. It is intended for tests where error handling is not necessary, and for chaining functions.

func MustWriteToFile

func MustWriteToFile(t testing.TB, b []byte, baseName string)

MustWriteToFile writes b to file testdata/baseName. If the file exists, it is truncated; if it doesn't exist, it is created. On errors, t.Fatal() is called.

func Parallel

func Parallel(f, g func(sc *SC)) func(c C)

func SoMsgError

func SoMsgError(msg string, err error, shouldBeError bool)

SoMsgError wraps nil/non-nil error Goconvey assertions into a single yes/no error check. The assertions pass if err is nil and shouldBeError is false, or if err is non-nil and shouldBeError is true. In the latter case, no equality check is performed.

func SoMsgErrorStr added in v0.4.0

func SoMsgErrorStr(msg string, err error, str string)

SoMsgErrorStr checks whether the top error in err matches string str. If str is empty, then the assertion succeeds if err is nil.

func TempFileName

func TempFileName(dir, prefix string) (string, error)

TempFileName creates a temporary file in dir with the specified prefix, and then closes and deletes the file and returns its name. It is useful for testing packages that care about a unique path without being able to overwrite it (e.g., UNIX domain socket addresses or databases).

Types

type Callback added in v0.4.0

type Callback interface {
	Call()
}

Callback defines an interfaces that provides a callback function that is mockable. A mock implementation implementing this interface can be found in sub-package mock_xtest.

type PanickingReporter added in v0.3.0

type PanickingReporter struct {
	*testing.T
}

PanickingReporter is a wrapper around the *testing.T implementation of gomock.TestReporter which panics and logs on FatalF instead of calling runtime.Goexit(). This avoids deadlocks when a child goroutine fails a mocking constraint when using gomock.

For more information, see https://github.com/golang/mock/issues/139.

func (*PanickingReporter) Fatalf added in v0.3.0

func (reporter *PanickingReporter) Fatalf(format string, args ...interface{})

type SC

type SC struct {
	C
	sync.WaitGroup
	sync.Mutex
}

func (*SC) Convey

func (c *SC) Convey(items ...interface{})

func (*SC) Recover

func (c *SC) Recover()

func (*SC) So

func (c *SC) So(actual interface{},
	assert func(actual interface{}, expected ...interface{}) string, expected ...interface{})

func (*SC) SoMsg

func (c *SC) SoMsg(msg string, actual interface{},
	assert func(actual interface{}, expected ...interface{}) string, expected ...interface{})

type Waiter added in v0.4.0

type Waiter struct {
	sync.WaitGroup
}

Waiter wraps the waitgroup and allows waiting with timeouts.

func (*Waiter) WaitWithTimeout added in v0.4.0

func (w *Waiter) WaitWithTimeout(timeout time.Duration) bool

WaitWithTimeout returns immediately after the waitgroup is done, or the timeout has passed. The return value indicates whether the call timed out.

Directories

Path Synopsis
Code generated by graphupdater tool, DO NOT EDIT.
Code generated by graphupdater tool, DO NOT EDIT.
Package loopback defines a net.PacketConn implementation where sent messages are echoed back on the same connection.
Package loopback defines a net.PacketConn implementation where sent messages are echoed back on the same connection.
Package mock_xtest is a generated GoMock package.
Package mock_xtest is a generated GoMock package.
Package p2p (point to point) provides a net.PacketConn wrapper around net.Pipe().
Package p2p (point to point) provides a net.PacketConn wrapper around net.Pipe().

Jump to

Keyboard shortcuts

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