Back to godoc.org
zgo.at/ztest

package ztest

v1.0.3
Latest Go to latest
Published: Mar 16, 2020 | License: MIT | Module: zgo.at/ztest

Overview

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

Index

Variables

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

Default values for NewRequest()

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

func HTTP(t *testing.T, req *http.Request, h http.Handler) *httptest.ResponseRecorder

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

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

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()
Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier