subtest

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2018 License: Apache-2.0 Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package subtest provides a TestContext to subtests which handles cleanup, and provides a testing.TB, and context.Context.

This package was inspired by github.com/frankban/quicktest.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(t *testing.T, name string, subtest func(t TestContext)) bool

Run a subtest. When subtest exits, every cleanup function added with TestContext.AddCleanup will be run.

Example (TableTest)
package main

import (
	"io"
	"net/http"
	"strings"
	"testing"

	"github.com/gotestyourself/gotestyourself/assert"
	"github.com/gotestyourself/gotestyourself/x/subtest"
)

var t = &testing.T{}

func main() {
	var testcases = []struct {
		data     io.Reader
		expected int
	}{
		{
			data:     strings.NewReader("invalid input"),
			expected: 400,
		},
		{
			data:     strings.NewReader("valid input"),
			expected: 200,
		},
	}

	for _, tc := range testcases {
		subtest.Run(t, "test-service-call", func(t subtest.TestContext) {
			// startFakeService can shutdown using t.AddCleanup
			url := startFakeService(t)

			req, err := http.NewRequest("POST", url, tc.data)
			assert.NilError(t, err)
			req = req.WithContext(t.Ctx())

			client := newClient(t)
			resp, err := client.Do(req)
			assert.NilError(t, err)
			assert.Equal(t, resp.StatusCode, tc.expected)
		})
	}
}

func startFakeService(t subtest.TestContext) string {

	return "url"
}

func newClient(T subtest.TestContext) *http.Client {
	return &http.Client{}
}
Example (TestSuite)
package main

import (
	"testing"

	"github.com/gotestyourself/gotestyourself/assert"
	"github.com/gotestyourself/gotestyourself/x/subtest"
)

var t = &testing.T{}

func main() {
	// do suite setup before subtests

	subtest.Run(t, "test-one", func(t subtest.TestContext) {
		assert.Equal(t, 1, 1)
	})
	subtest.Run(t, "test-two", func(t subtest.TestContext) {
		assert.Equal(t, 2, 2)
	})

	// do suite teardown after subtests
}

Types

type TestContext

type TestContext interface {
	testing.TB
	// AddCleanup function which will be run when before Run returns.
	AddCleanup(f func())
	// Ctx returns a context for the test case. Multiple calls from the same subtest
	// will return the same context. The context is cancelled when Run
	// returns.
	Ctx() context.Context
	// Parallel calls t.Parallel on the testing.TB. Panics if testing.TB does
	// not implement Parallel.
	Parallel()
}

TestContext provides a testing.TB and a context.Context for a test case.

Jump to

Keyboard shortcuts

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