qtsuite

package
v1.14.6 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2023 License: MIT Imports: 5 Imported by: 2

Documentation

Overview

Package qtsuite allows quicktest to run test suites.

A test suite is a value with one or more test methods. For example, the following code defines a suite of test functions that starts an HTTP server before running each test, and tears it down afterwards:

type suite struct {
	url string
}

func (s *suite) Init(c *qt.C) {
	hnd := func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "%s %s", req.Method, req.URL.Path)
	}
	srv := httptest.NewServer(http.HandlerFunc(hnd))
	c.Cleanup(srv.Close)
	s.url = srv.URL
}

func (s *suite) TestGet(c *qt.C) {
	c.Parallel()
	resp, err := http.Get(s.url)
	c.Assert(err, qt.Equals, nil)
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	c.Assert(err, qt.Equals, nil)
	c.Assert(string(b), qt.Equals, "GET /")
}

func (s *suite) TestHead(c *qt.C) {
	c.Parallel()
	resp, err := http.Head(s.url + "/path")
	c.Assert(err, qt.Equals, nil)
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	c.Assert(err, qt.Equals, nil)
	c.Assert(string(b), qt.Equals, "")
	c.Assert(resp.ContentLength, qt.Equals, int64(10))
}

The above code could be invoked from a test function like this:

func TestHTTPMethods(t *testing.T) {
	qtsuite.Run(qt.New(t), &suite{"http://example.com"})
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(c *qt.C, suite interface{})

Run runs each test method defined on the given value as a separate subtest. A test is a method of the form

func (T) TestXxx(*quicktest.C)

where Xxx does not start with a lowercase letter.

If suite is a pointer, the value pointed to is copied before any methods are invoked on it; a new copy is made for each test. This means that it is OK for tests to modify fields in suite concurrently if desired - it's OK to call c.Parallel().

If suite has a method of the form

func (T) Init(*quicktest.C)

this method will be invoked before each test run.

Types

This section is empty.

Jump to

Keyboard shortcuts

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