Documentation ¶
Overview ¶
Package test contains helper functions that are useful for writing tests.
Index ¶
- Variables
- func Body(a interface{}) *bytes.Reader
- func Code(t *testing.T, recorder *httptest.ResponseRecorder, want int)
- func ErrorContains(out error, want string) bool
- func HTTP(t *testing.T, req *http.Request, h http.Handler) *httptest.ResponseRecorder
- func I64P(i int64) *int64
- func MultipartForm(params ...map[string]string) (b *bytes.Buffer, contentType string, err error)
- func NewRequest(method, target string, body io.Reader) *http.Request
- func NormalizeIndent(in string) string
- func R(t *testing.T)
- func Read(t *testing.T, paths ...string) []byte
- func SP(s string) *string
- func TempFile(t *testing.T, data string) (string, func())
Constants ¶
This section is empty.
Variables ¶
var ( DefaultHost = "test.teamwork.dev" DefaultContentType = "application/json" )
Default values for NewRequest()
Functions ¶
func Body ¶
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", "/", echotest.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 ErrorContains ¶
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 MultipartForm ¶
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 NewRequest ¶
NewRequest returns a new incoming server Request, suitable for passing to an echo.HandlerFunc for testing.
func NormalizeIndent ¶
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 ¶
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") })
Types ¶
This section is empty.
Directories ¶
Path | Synopsis |
---|---|
Package diff has some helpers for text diffs.
|
Package diff has some helpers for text diffs. |
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. |