testkit

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2020 License: BSD-2-Clause Imports: 18 Imported by: 0

README

Test Kit

Package testkit provides all sorts of test helpers. See GoDoc for the documentation and examples.

Go Report Card PkgGoDev

License

BSD-2-Clause

Documentation

Overview

Package testkit provides all sorts of test helpers.

Index

Constants

This section is empty.

Variables

View Source
var ErrTestError = errors.New("testkit test error")

ErrTestError is general error used in tests,it is returned by some helpers.

Functions

func AbsPath

func AbsPath(t T, pth string) string

AbsPath is a wrapper around filepath.Abs() which calls t.Fatal() on error.

func AssertErrPrefix

func AssertErrPrefix(t T, err error, prefix string)

AssertErrPrefix asserts error message has prefix.

func CloneHTTPRequest

func CloneHTTPRequest(t T, req *http.Request) *http.Request

CloneHTTPRequest clones HTTP request with body, URL.Host and URL.Scheme. Calls t.Fatal() on error.

func CreateDir

func CreateDir(t T, name string) string

CreateDir creates directory with name returns passed name so it can be used in place.

MyFunction(CreateDir(t, "name"))

Calls t.Fatal() on error.

func CreateFile

func CreateFile(t T, name string) *os.File

CreateFile is a wrapper around os.Create() which calls t.Fatal() on error.

func ErrReader

func ErrReader(r io.Reader, n int, err error) io.Reader

ErrReader wraps reader r. The returned reader reads at most n bytes and returns error err. If err is set to nil ErrTestError will be used. Error other then err will be returned if underlying reader returns an error before n bytes are read.

func ErrWriter

func ErrWriter(w io.Writer, n int, err error) io.Writer

ErrWriter wraps writer w in a writer which writes at most n bytes and returns error err. If err is set to nil then ErrTestError will be used. Error other then err will be returned if underlying writer returns an error before n bytes are written.

func FileSize

func FileSize(t T, fil *os.File) int64

FileSize returns file size. Calls t.Fatal() on error.

func FromJSON

func FromJSON(t T, data []byte, v interface{})

FromJSON is a wrapper around json.Unmarshal() which calls t.Fatal() on error.

func FromJSONReader

func FromJSONReader(t T, r io.Reader, v interface{})

FromJSONReader reads JSON from r and unmarshalls them to v. Calls t.Fatal() on error.

func Getwd

func Getwd(t T) string

Getwd is a wrapper around os.Getwd() which calls t.Fatal() on error..

func HasNetConn

func HasNetConn() bool

HasNetConn returns true if the Internet connection is present.

func ModTime

func ModTime(t T, name string) time.Time

ModTime calls os.Stat() and returns modification time. Calls t.Fatal() on error.

func NoNetworkSkip

func NoNetworkSkip(t T)

NoNetworkSkip skips test if there is no Internet connection.

func OpenFile

func OpenFile(t T, name string) *os.File

OpenFile is a wrapper around os.Open() which calls t.Fatal() on error.

func RandFileName

func RandFileName(dir, prefix, ext string) string

RandFileName returns random file name. If prefix is empty string it will be set to "file-". If ext is empty string it will be set to ".txt".

func RandStr

func RandStr(n int) string

RandStr returns random string of length n.

func ReadAll

func ReadAll(t T, r io.Reader) []byte

ReadAll is a wrapper around ioutil.ReadAll which calls t.Fatal() on error.

func ReadAllFromStart

func ReadAllFromStart(t T, rs io.ReadSeeker) []byte

ReadAllFromStart seeks to the beginning of rs and reads it till gets io.EOF or any other error. Then seeks back to the position where rs was before the call. Calls t.Fatal() on error.

func ReadAllString

func ReadAllString(t T, r io.Reader) string

ReadAllString is a wrapper around ioutil.ReadAll which returns string instead of byte slice. Calls t.Fatal() on error.

func ReadFile

func ReadFile(t T, filename string) []byte

ReadFile is a wrapper around ioutil.ReadFile() which calls t.Fatal() on error.

func Readdirnames

func Readdirnames(t T, fil *os.File) []string

Readdirnames returns slice returned by Readdirnames(0) called on fil instance. Calls t.Fatal() on error.

func ReplaceAllInFile

func ReplaceAllInFile(t T, filename, old, new string)

ReplaceAllInFile replaces all occurrences of old sting with new in the filename. Calls t.Fatal() on error.

func TempDir

func TempDir(t T, dir string, prefix string) string

TempDir is a wrapper around ioutil.TempDir() which calls t.Fatal() on error.

func TempFile

func TempFile(t T, dir, pattern string) *os.File

TempFile is a wrapper around ioutil.TempFile() which calls t.Fatal() on error.

func ToJSON

func ToJSON(t T, v interface{}) []byte

ToJSON is a wrapper around json.MarshalIndent() which calls t.Fatal() on error.

func ToJSONReader

func ToJSONReader(t T, v interface{}) io.Reader

ToJSONReader marshals v to JSON and returns io.Reader for it. Calls t.Fatal() on error.

func Wait

func Wait(max time.Duration, fn func() bool)

Wait waits till fn returns true but no longer then timeout max.

func WaitTh

func WaitTh(max, th time.Duration, fn func() bool)

WaitTh waits for fn to return true but no longer then timeout max. The calls to fn are throttled with th.

func WriteTempFile

func WriteTempFile(t T, dir string, r io.Reader) string

WriteTempFile creates and writes to temporary file from reader r. Returns path to created file. It registers cleanup function with t removing the created file. Calls t.Fatal() on error.

Types

type HTTPServer

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

HTTPServer represents very simple HTTP server recording all the requests it receives, responding with responses added with Rsp() method.

The server instance provides methods to analyse the received requests.

func NewHTTPServer

func NewHTTPServer(t T) *HTTPServer

NewHTTPServer returns new instance of HTTPServer.

func (*HTTPServer) Body

func (tst *HTTPServer) Body(n int) []byte

Body returns body of the nth received request. Calls t.Fatal() if n is greater then number or received requests.

func (*HTTPServer) Close

func (tst *HTTPServer) Close() error

Close stops the test server and does cleanup. May be called multiple times.

func (*HTTPServer) Headers

func (tst *HTTPServer) Headers(n int) http.Header

Headers returns headers for given request index. Calls t.Fatal() if n is greater then number or received requests.

func (*HTTPServer) ReqCount

func (tst *HTTPServer) ReqCount() int

ReqCount returns number of requests recorded by test server.

func (*HTTPServer) Request

func (tst *HTTPServer) Request(n int) *http.Request

Request returns clone of the nth received request. Calls t.Fatal() if n is greater then number or received requests.

func (*HTTPServer) Rsp

func (tst *HTTPServer) Rsp(status int, rsp []byte) *HTTPServer

Rsp adds response with status and body to the list of responses to return.

Every time server receives a request it returns predefined response. The responses are returned in the order they were added. If there is no more responses the t.Fatal() will be called.

func (*HTTPServer) URL

func (tst *HTTPServer) URL() string

URL returns URL for the test server.

func (*HTTPServer) Values

func (tst *HTTPServer) Values(n int) url.Values

Values returns URL query values of the nth received request. Calls t.Fatal() if n is greater then number or received requests.

type T

type T interface {
	// Cleanup registers a function to be called when the test and all its
	// subtests complete. Cleanup functions will be called in last added,
	// first called order.
	Cleanup(func())

	// Error is equivalent to Log followed by Fail.
	Error(args ...interface{})

	// Errorf is equivalent to Logf followed by Fail.
	Errorf(format string, args ...interface{})

	// Fatal is equivalent to Log followed by FailNow.
	Fatal(args ...interface{})

	// Fatalf is equivalent to Logf followed by FailNow.
	Fatalf(format string, args ...interface{})

	// Helper marks the calling function as a test helper function.
	// When printing file and line information, that function will be skipped.
	// Helper may be called simultaneously from multiple goroutines.
	Helper()

	// Skip is equivalent to Log followed by SkipNow.
	Skip(args ...interface{})
}

T is a subset of testing.TB interface. It's primarily used to test the testkit package but can be used to implement custom actions to be taken on errors.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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