ztest

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2020 License: MIT Imports: 13 Imported by: 0

README

Build Status codecov GoDoc

Various small helper functions that are useful in tests.

Documentation

Overview

Package ztest contains helper functions that are useful for writing tests.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultHost        = "example.com"
	DefaultContentType = "application/json"
)

Default values for NewRequest()

Functions

func Body

func Body(a interface{}) *bytes.Reader

Body returns the JSON representation of the passed in argument as an io.Reader. This is useful for creating a request body. For example:

NewRequest("POST", "/", ztest.Body(someStruct{
    Foo: "bar",
}))

func Code

func Code(t *testing.T, recorder *httptest.ResponseRecorder, want int)

Code checks if the error code in the recoder matches the desired one, and will stop the test with t.Fatal() if it doesn't.

func Diff

func Diff(out, want string) string

Diff 2 strings and format as a unified diff.

func ErrorContains

func ErrorContains(out error, want string) bool

ErrorContains checks if the error message in out contains the text in want.

This is safe when out is nil. Use an empty string for want if you want to test that err is nil.

func HTTP

HTTP sets up a HTTP test. A GET request will be made for you if req is nil.

For example:

rr := test.HTTP(t, nil, MyHandler)

Or for a POST request:

req, err := http.NewRequest("POST", "/v1/email", b)
if err != nil {
	t.Fatalf("cannot make request: %v", err)
}
req.Header.Set("Content-Type", ct)
rr := test.HTTP(t, req, MyHandler)

func I64P

func I64P(i int64) *int64

I64P makes a new Int64 Pointer.

func MultipartForm

func MultipartForm(params ...map[string]string) (b *bytes.Buffer, contentType string, err error)

MultipartForm writes the keys and values from params to a multipart form.

The first input parameter is used for "multipart/form-data" key/value strings, the optional second parameter is used creating file parts.

Don't forget to set the Content-Type from the return value:

req.Header.Set("Content-Type", contentType)

func NewMatcherWithJunk added in v1.0.3

func NewMatcherWithJunk(a, b []string, autoJunk bool,
	isJunk func(string) bool) *sequenceMatcher

func NewRequest

func NewRequest(method, target string, body io.Reader) *http.Request

NewRequest returns a new incoming server Request, suitable for passing to an echo.HandlerFunc for testing.

func NormalizeIndent

func NormalizeIndent(in string) string

NormalizeIndent removes tab indentation from every line.

This is useful for "inline" multiline strings:

  cases := []struct {
      string in
  }{
      `
	 	    Hello,
	 	    world!
      `,
  }

This is nice and readable, but the downside is that every line will now have two extra tabs. This will remove those two tabs from every line.

The amount of tabs to remove is based only on the first line, any further tabs will be preserved.

func R

func R(t *testing.T)

R recovers a panic and cals t.Fatal().

This is useful especially in subtests when you want to run a top-level defer. Subtests are run in their own goroutine, so those aren't called on regular panics. For example:

func TestX(t *testing.T) {
    clean := someSetup()
    defer clean()

    t.Run("sub", func(t *testing.T) {
        panic("oh noes")
    })
}

The defer is never called here. To fix it, call this function in all subtests:

t.Run("sub", func(t *testing.T) {
    defer test.R(t)
    panic("oh noes")
})

See: https://github.com/golang/go/issues/20394

func Read

func Read(t *testing.T, paths ...string) []byte

Read data from a file.

func ReplaceStdStreams added in v1.0.1

func ReplaceStdStreams() (*os.File, func())

ReplaceStdStreams replaces os.Stdout and os.Stderr with a buffer.

out, reset := ztest.ReplaceStdStreams()
defer reset()

func SP

func SP(s string) *string

SP makes a new String Pointer.

func TempFile

func TempFile(t *testing.T, data string) (string, func())

TempFile creates a new temporary file and returns the path and a clean function to remove it.

f, clean := TempFile("some\ndata")
defer clean()

Types

This section is empty.

Directories

Path Synopsis
diff module
Package fakeconn provides a "fake" net.Conn implementation.
Package fakeconn provides a "fake" net.Conn implementation.
Package image contains helpers for testing image-related code.
Package image contains helpers for testing image-related code.

Jump to

Keyboard shortcuts

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