Documentation ¶
Overview ¶
Package tester implements functions for testing and benchmarking Gondola applications.
To set up a test, create a _test.go file, like in Go standard tests, and create a standard Go test function. Then, use New to create a Tester and use it to generate requests and check what they return.
See Tester or this package's tests for a few examples of complete tests. For benchmark, use Request.Bench. See its documentation for details.
Additionaly, tests might be run against a remote server by using the -H command line flag. e.g.
go test -v -H example.com // or https://example.com
Will run your tests against the server at example.com, rather than using the compiled code. This is pretty useful to make sure your tests pass on production after deploying your application.
App Engine apps with a correctly set up app.yaml might also use the -R flag to automatically test against http://<your-app-id>.appspot.com.
goapp test -v -R
The -L flag is also supported on GAE apps, for testing against http://localhost:8080. Note that you must manually start your app with goapp serve.
goapp test -v -L
Index ¶
- type Contains
- type Match
- type Reporter
- type Request
- func (r *Request) AddHeader(key string, value interface{}) *Request
- func (r *Request) Bench(b *testing.B)
- func (r *Request) Contains(what string) *Request
- func (r *Request) ContainsHeader(name string, what string) *Request
- func (r *Request) Err() error
- func (r *Request) Expect(what interface{}) *Request
- func (r *Request) ExpectHeader(name string, what interface{}) *Request
- func (r *Request) Match(what string) *Request
- func (r *Request) MatchHeader(name string, what string) *Request
- type Tester
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Contains ¶
type Contains string
Contains is an alias for string, to indicate Request.Expect to check that a response contains the given value (rather than being equal). Users should use the shorthand methods Response.Contains and Response.ContainsHeader.
type Match ¶
type Match string
Match is an alias for string, to indicate Request.Expect to compile the given value as a regular expression and then match it against the response body. Users should use the shorthand methods Response.Match and Response.MatchHeader.
type Reporter ¶
type Reporter interface { Log(args ...interface{}) Error(args ...interface{}) Fatal(args ...interface{}) }
Reporter is the interface used to log and report errors when testing Gondola applications. Both testing.T and testing.B implement this interface.
type Request ¶
type Request struct { Reporter Reporter App *app.App Header http.Header Method string Path string Body []byte // contains filtered or unexported fields }
A Request represents a request to be sent to the app. Users won't usually need to construct requests by hand, most of the time the helper type Test and its conveniency methods should be used. Use Request.Expect and its related functions to perform tests on the App response.
func (*Request) AddHeader ¶
AddHeader is a conveniency function which adds a new HTTP header and returns the same *Request, to allow chaining. The value is converted to an string using types.ToString.
func (*Request) Bench ¶
Bench performs a benchmark with this request. Note that Bench does not perform any tests on the response, so you should not use Bench for testing, only strictly for benchmarking. Tipically, Bench is used from a testing benchmark.
BenchmarkSomething(b *testing.B) { te := tester.New(b, MyApp) te.Get("/something", nil).Bench(b) }
Note that bench does not support benchmarking a remote server, so it ignores the -H flag.
func (*Request) Contains ¶
Contains checks that the response body contains the given string. It's a shorthand for r.Expect(tester.Contains(what)).
func (*Request) ContainsHeader ¶
ContainsHeader works like Contains, but checks the requested header rather than the response body.
func (*Request) Expect ¶
Expect checks the response body or the status against the given criteria. Expect accepts the following argument types.
int: Check that the response code matches the given value. nil: Check that the response is empty. string: Check that the response body is equal to the given value. Contains: Check that the response body contains the given value. Match: Compile the given value as a regular expression and match it against the body.
Request send the request to the App lazily, on the first call to Expect. Multiple Expect (and related functions, like Contains or ExpectHeader) can be chained, but processing will stop as soon as an error is generated (either when getting the response from the App or from a failed Expect check). To get the first error, use the Err() method. Note that when using a *testing.T or a *testing.B as the Reporter, Expect will call t.Error or t.Fatal for you. See also the shorthand functions Request.Contains and Request.Match. Additionally, check this package's tests, in tester_test.go, for examples.
func (*Request) ExpectHeader ¶
ExpectHeader works like Expect, but checks the requested header rather than the response body. See Response.Expect to find the accepted types.
type Tester ¶
Tester encapsulates the app.App to be tested with the Reporter to send the logs and errors to. It's usually used in conjuction with the testing package.
func TestMyApp(t *testing.T) { // App was initialized on init. Note that // the app does not need to be listening. te := tester.New(t, App) te.Get("/foo", nil).Expect(200) }
func New ¶
New returns prepares the *app.App and then returns a new Tester. It also disables logging in the App, since the Tester does its own logging.
func (*Tester) Form ¶
Form returns a POST request which sends a form with the given parameters, while also setting the right Content-Type.
func (*Tester) Get ¶
Get returns a GET request with the given path and parameters, which are appended to the URL as a query string.