Documentation ¶
Overview ¶
Package testutil implements generally useful test utilities.
Index ¶
- func FileExists(fileName string) bool
- func JSON(js string) interface{}
- func ReadFile(fileName string) string
- func Round(x float64, places int) float64
- func RoundFixed(x float64, digits int) float64
- func RoundFixedSlice(s []float64, places int) []float64
- func RoundSlice(s []float64, places int) []float64
- func WriteFile(fileName, text string) error
- type TestServer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FileExists ¶ added in v0.2.0
FileExists returns true if a file with the given path exists, and it is indeed a file (not a directory).
func JSON ¶
func JSON(js string) interface{}
JSON parses js as a JSON string into the default encoding/json data structures: maps, strings, numbers, etc. This is useful e.g. for custom JSON readers.
func ReadFile ¶ added in v0.2.0
ReadFile returns the contents of the file as a string. If the file doesn't exist, or it is not a file, this method panics. It is a good practice to test that FileExists() is true, then call this method.
func Round ¶
Round x to the given number of significant decimal digits, for approximate comparisons in tests.
func RoundFixed ¶
RoundFixed rounds x to the fixed number of decimal places. This is useful for rounding around zero, since it has no well-defined number of significant digits.
func RoundFixedSlice ¶
RoundFixedSlice rounds the elements of the slice to the given number of decimal places, for approximate comparisons in tests. This is useful when numbers may be near zero, and therefore have no well-defined number of significant digits.
func RoundSlice ¶
RoundSlice rounds the elements of the slice to the given number of significant decimal digits, for approximate comparisons in tests.
Types ¶
type TestServer ¶
type TestServer struct { ResponseStatusMap map[string][]int // URL path -> status code sequence ResponseBodyMap map[string][]string // URL path -> response body sequence ResponseStatus []int // default status codes sequence ResponseBody []string // default response body sequnece RequestPath string // path in the request URL RequestQuery url.Values // query received by the server in the request Flushed bool // whether the body write was flushed BodyWriteBytes int // number of body bytes written BodyWriteError error // error value from writing body Server *httptest.Server }
TestServer is the handle for a test server. The server returns the status code and response body from the matching sequence, and then repeats the last one in the sequence.
Note, that this creates a real HTTP server running locally, and the client fetches real URLs from the network. Always use the base URL provided by URL() method to avoid calling random servers on the Internet in your tests.
Tip: pay attention to the status codes when setting a response body: some response codes do not allow a response body. Always check for BodyWriteError in your tests to catch this problem.
func NewTestServer ¶
func NewTestServer() *TestServer
NewTestServer creates and starts a new test server.
func (*TestServer) Client ¶
func (h *TestServer) Client() *http.Client
Client is the test server's HTTP client to be used in tests.