mt

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2023 License: MIT Imports: 22 Imported by: 4

Documentation

Index

Constants

View Source
const (
	// ExecuteTestsFirst causes the test runner to execute tests before subgroups.
	ExecuteTestsFirst = iota

	// ExecuteSubgroupsFirst causes the test runner to execute subgroups before tests.
	ExecuteSubgroupsFirst
)

Variables

This section is empty.

Functions

func FPrintResults

func FPrintResults(w io.Writer, results *GroupRunResult)

FPrintResults prints the results of a group run to the given io.Writer.

By default, the output is formatted as a table and colors are used if possible. The behavior can be controlled by setting the MELATONIN_OUTPUT environment variable to "json" to produce JSON output, or "none" to disable output all together.

func PrintJSONResults

func PrintJSONResults(results *GroupRunResult, deep bool) error

PrintJSONResults prints the results of a group run as JSON to the given io.Writer.

func PrintResults

func PrintResults(results *GroupRunResult)

PrintResults prints the results of a group run to stdout.

By default, the output is formatted as a table and colors are used if possible. The behavior can be controlled by setting the MELATONIN_OUTPUT environment variable to "json" to produce JSON output, or "none" to disable output all together.

Types

type GroupRunResult

type GroupRunResult struct {
	// Group is a reference to the test group that was run.
	Group *TestGroup `json:"-"`

	// Results is a list of test run results for each test in the group that was run.
	TestResults []TestRunResult `json:"test_results,omitempty"`

	// GroupRunResults is a list of group run results for each subgroup that was run.
	SubgroupResults []*GroupRunResult `json:"group_results,omitempty"`

	// Passed is the number of tests that passed.
	Passed int `json:"passed"`

	// Failed is the number of tests that failed.
	Failed int `json:"failed"`

	// Skipped is the number of tests that were skipped.
	Skipped int `json:"skipped"`

	// Total is the total number of tests in the test group.
	Total int `json:"total"`

	// Duration is the total duration of all tests in the test group.
	Duration time.Duration `json:"duration"`
}

A GroupRunResult contains information about a completed set of test cases run by a test runner.

func RunTestGroup

func RunTestGroup(group *TestGroup) *GroupRunResult

RunTestGroup runs a test group using the default test runner.

func RunTestGroupT

func RunTestGroupT(t *testing.T, group *TestGroup) *GroupRunResult

RunTestGroupT runs a test group within the context of a Go test using the default test runner.

func RunTestGroups added in v0.0.10

func RunTestGroups(groups ...*TestGroup) *GroupRunResult

RunTestGroups runs a set of test groups using the default test runner.

func RunTestGroupsT added in v0.0.10

func RunTestGroupsT(t *testing.T, groups ...*TestGroup) *GroupRunResult

RunTestGroupsT runs a set of test groups within the context of a Go test using the default test runner.

func RunTests

func RunTests(tests ...TestCase) *GroupRunResult

RunTests runs a set of tests using the default test runner.

func RunTestsT

func RunTestsT(t *testing.T, tests ...TestCase) *GroupRunResult

RunTestsT runs a set of tests within a Go test context using the default test runner.

type HTTPTestCase

type HTTPTestCase struct {
	// After is an optional function that is run after the test is run.
	// It can be used to perform any cleanup actions after the test,
	// such as adding or removing objects in a database. Any error
	// returned by After is treated as a test failure.
	AfterFunc func() error

	// Before is an optional function that is run before the test is run.
	// It can be used to perform any prerequisites actions for the test,
	// such as adding or removing objects in a database. Any error
	// returned by Before is treated as a test failure.
	BeforeFunc func() error

	// Desc is a description of the test case.
	Desc string

	// Expectations is a set of values to compare the response against.
	Expectations expectatons `json:"expectations"`

	// GoldenFilePath is a path to a golden file defining expectations for the test case.
	//
	// If set, any WantStatus, WantHeaders, or WantBody values are overridden with
	// values from the golden file.
	GoldenFilePath string
	// contains filtered or unexported fields
}

An HTTPTestCase tests a single call to an HTTP endpoint.

An optional setup function can be provided to perform any necessary setup before the test is run, such as adding or removing objects in a database.

All fields in the WantBody map are expected to be present in the response body.

func DELETE

func DELETE(url string, description ...string) *HTTPTestCase

DELETE is a shortcut for DefaultContext().NewTestCase(http.MethodDelete, path).

func DO

func DO(request *http.Request, description ...string) *HTTPTestCase

DO creates a test case from a custom HTTP request.

func GET

func GET(url string, description ...string) *HTTPTestCase

GET is a shortcut for NewTestCase(http.MethodGet, path, description...).

func HEAD(url string, description ...string) *HTTPTestCase

HEAD is a shortcut for NewTestCase(http.MethodHead, path, description...).

func OPTIONS

func OPTIONS(url string, description ...string) *HTTPTestCase

OPTIONS is a shortcut for NewTestCase(http.MethodOptions, path, description...).

func PATCH

func PATCH(url string, description ...string) *HTTPTestCase

PATCH is a shortcut for NewTestCase(http.MethodPatch, path, description...).

func POST

func POST(url string, description ...string) *HTTPTestCase

POST is a shortcut for NewTestCase(http.MethodPost, path, description...).

func PUT

func PUT(url string, description ...string) *HTTPTestCase

PUT is a shortcut for NewTestCase(http.MethodPut, path, description...).

func (*HTTPTestCase) Action

func (tc *HTTPTestCase) Action() string

Action returns a short, uppercase verb describing the action performed by the test case.

func (*HTTPTestCase) After

func (tc *HTTPTestCase) After(after func() error) *HTTPTestCase

After registers a function to be run after the test case.

func (*HTTPTestCase) Before

func (tc *HTTPTestCase) Before(before func() error) *HTTPTestCase

Before registers a function to be run before the test case.

func (*HTTPTestCase) Describe

func (tc *HTTPTestCase) Describe(description string) *HTTPTestCase

Describe sets a description for the test case.

func (*HTTPTestCase) Description

func (tc *HTTPTestCase) Description() string

Description returns a string describing the test case.

func (*HTTPTestCase) Execute

func (tc *HTTPTestCase) Execute() TestResult

Execute runs the test case.

func (*HTTPTestCase) ExpectBody

func (tc *HTTPTestCase) ExpectBody(body any) *HTTPTestCase

ExpectBody sets the expected HTTP response body for the test case.

func (*HTTPTestCase) ExpectExactBody

func (tc *HTTPTestCase) ExpectExactBody(body any) *HTTPTestCase

ExpectExactBody sets the expected HTTP response body for the test case.

Unlike ExpectBody, ExpectExactBody willl cause the test case to fail if the expected response body is a JSON object or array and contains any additional fields or values not present in the expected JSON content.

For non-JSON values, ExpectExactBody behaves identically to ExpectBody.

func (*HTTPTestCase) ExpectExactHeaders

func (tc *HTTPTestCase) ExpectExactHeaders(headers http.Header) *HTTPTestCase

ExpectExactHeaders sets the expected HTTP response headers for the test case.

Unlike ExpectHeaders, ExpectExactHeaders willl cause the test case to fail if any unexpected headers are present in the response.

func (*HTTPTestCase) ExpectGolden

func (tc *HTTPTestCase) ExpectGolden(path string) *HTTPTestCase

ExpectGolden causes the test case to load its HTTP response expectations from a golden file.

func (*HTTPTestCase) ExpectHeader

func (tc *HTTPTestCase) ExpectHeader(key, value string) *HTTPTestCase

ExpectHeader adds an expected HTTP response header for the test case.

func (*HTTPTestCase) ExpectHeaders

func (tc *HTTPTestCase) ExpectHeaders(headers http.Header) *HTTPTestCase

ExpectHeaders sets the expected HTTP response headers for the test case.

Unlike ExpectExactHeaders, ExpectHeaders only verifies that the expected headers are present in the response, and ignores any additional headers.

func (*HTTPTestCase) ExpectStatus

func (tc *HTTPTestCase) ExpectStatus(status int) *HTTPTestCase

ExpectStatus sets the expected HTTP status code for the test case.

func (HTTPTestCase) MarshalJSON

func (tc HTTPTestCase) MarshalJSON() ([]byte, error)

MarshalJSON customizes the JSON representaton of the test case.

func (*HTTPTestCase) Target

func (tc *HTTPTestCase) Target() string

Target returns a string representing the target of the action performed by the test case.

func (*HTTPTestCase) Validate

func (tc *HTTPTestCase) Validate() error

Validate ensures that the test case is valid can can be run.

func (*HTTPTestCase) WithBody

func (tc *HTTPTestCase) WithBody(body any) *HTTPTestCase

WithBody sets the request body for the test case.

func (*HTTPTestCase) WithHeader

func (tc *HTTPTestCase) WithHeader(key, value string) *HTTPTestCase

WithHeader adds a request header to the test case.

func (*HTTPTestCase) WithHeaders

func (tc *HTTPTestCase) WithHeaders(headers http.Header) *HTTPTestCase

WithHeaders sets the request headers for the test case.

func (*HTTPTestCase) WithPathParam added in v0.0.3

func (tc *HTTPTestCase) WithPathParam(key string, value any) *HTTPTestCase

WithPathParam adds a request path parameter to the test case.

func (*HTTPTestCase) WithPathParams added in v0.0.3

func (tc *HTTPTestCase) WithPathParams(params map[string]any) *HTTPTestCase

WithPathParams sets the request path parameters for the test case.

func (*HTTPTestCase) WithQueryParam

func (tc *HTTPTestCase) WithQueryParam(key string, value any) *HTTPTestCase

WithQueryParam adds a request query parameter to the test case.

func (*HTTPTestCase) WithQueryParams

func (tc *HTTPTestCase) WithQueryParams(params map[string]any) *HTTPTestCase

WithQueryParams sets the request query parameters for the test case.

func (*HTTPTestCase) WithTimeout

func (tc *HTTPTestCase) WithTimeout(timeout time.Duration) *HTTPTestCase

WithTimeout sets a timeout for the test case.

type HTTPTestCaseResult

type HTTPTestCaseResult struct {
	// Status is the HTTP status code returned in the response.
	Status int `json:"status"`

	// Headers is the HTTP response headers.
	Headers http.Header `json:"headers"`

	// Body is the HTTP response body.
	Body []byte `json:"body"`
	// contains filtered or unexported fields
}

HTTPTestCaseResult represents the result of running a single test case.

func (*HTTPTestCaseResult) Failures

func (r *HTTPTestCaseResult) Failures() []error

Failures returns a list of test case failures.

func (*HTTPTestCaseResult) TestCase

func (r *HTTPTestCaseResult) TestCase() TestCase

TestCase returns a reference to the test case that generated the result.

type HTTPTestContext

type HTTPTestContext struct {
	BaseURL string
	Client  *http.Client
	Handler http.Handler
}

An HTTPTestContext is used to create HTTP test cases that target either a specific base URL or a Go HTTP handler.

func DefaultContext

func DefaultContext() *HTTPTestContext

DefaultContext returns an HTTPTestContext using the default HTTP client.

func NewHandlerContext

func NewHandlerContext(handler http.Handler) *HTTPTestContext

NewHandlerContext creates a new HTTPTestContext for creating tests that target the specified HTTP handler.

func NewURLContext

func NewURLContext(baseURL string) *HTTPTestContext

NewURLContext creates a new HTTPTestContext for creating tests that target the specified base URL.

func (*HTTPTestContext) DELETE

func (c *HTTPTestContext) DELETE(path string, description ...string) *HTTPTestCase

DELETE is a shortcut for NewTestCase(http.MethodDelete, path).

func (*HTTPTestContext) DO

func (c *HTTPTestContext) DO(request *http.Request, description ...string) *HTTPTestCase

DO creates a test case from a custom HTTP request.

func (*HTTPTestContext) GET

func (c *HTTPTestContext) GET(path string, description ...string) *HTTPTestCase

GET is a shortcut for NewTestCase(http.MethodGet, path, description...).

func (*HTTPTestContext) HEAD

func (c *HTTPTestContext) HEAD(path string, description ...string) *HTTPTestCase

HEAD is a shortcut for NewTestCase(http.MethodHead, path, description...).

func (*HTTPTestContext) OPTIONS

func (c *HTTPTestContext) OPTIONS(path string, description ...string) *HTTPTestCase

OPTIONS is a shortcut for NewTestCase(http.MethodOptions, path, description...).

func (*HTTPTestContext) PATCH

func (c *HTTPTestContext) PATCH(path string, description ...string) *HTTPTestCase

PATCH is a shortcut for NewTestCase(http.MethodPatch, path, description...).

func (*HTTPTestContext) POST

func (c *HTTPTestContext) POST(path string, description ...string) *HTTPTestCase

POST is a shortcut for NewTestCase(http.MethodPost, path, description...).

func (*HTTPTestContext) PUT

func (c *HTTPTestContext) PUT(path string, description ...string) *HTTPTestCase

PUT is a shortcut for NewTestCase(http.MethodPut, path, description...).

func (*HTTPTestContext) WithHTTPClient

func (c *HTTPTestContext) WithHTTPClient(client *http.Client) *HTTPTestContext

WithHTTPClient sets the HTTP client used for HTTP requests and returns the context.

type TestCase

type TestCase interface {
	Action() string
	Target() string
	Description() string
	Execute() TestResult
}

A TestCase is anything that can be Execute()'d to produce a TestResult. Additionally, it must provide an Action, Target, and Description for reporting purposes.

type TestGroup

type TestGroup struct {
	Name       string
	BeforeFunc func()
	AfterFunc  func()
	Tests      []TestCase
	Subgroups  []*TestGroup
}

A TestGroup is a set of Tests with associated metadata.

Test groups are nestable, and can be used to create a hierarchy of tests.

func NewTestGroup

func NewTestGroup(name string) *TestGroup

NewTestGroup creates a new TestGroup with the given name.

func (*TestGroup) AddGroups

func (g *TestGroup) AddGroups(groups ...*TestGroup) *TestGroup

AddGroups adds one or more TestGroups to the TestGroup.

func (*TestGroup) AddTests

func (g *TestGroup) AddTests(tc ...TestCase) *TestGroup

AddTests adds one or more Tests to the TestGroup.

func (*TestGroup) After added in v0.0.11

func (g *TestGroup) After(fn func()) *TestGroup

After adds a function to be called after all tests in the group have been run.

func (*TestGroup) Before added in v0.0.11

func (g *TestGroup) Before(fn func()) *TestGroup

Before adds a function to be called before any tests in the group are run.

type TestResult

type TestResult interface {
	TestCase() TestCase
	Failures() []error
}

A TestResult is anything that produces a set of failures. Additionally, it must reference the TestCase that produced it.

type TestRunResult

type TestRunResult struct {
	TestCase   TestCase      `json:"test"`
	TestResult TestResult    `json:"result"`
	StartedAt  time.Time     `json:"started_at"`
	EndedAt    time.Time     `json:"finished_at"`
	Duration   time.Duration `json:"duration"`
}

A TestRunResult contains information about a completed test case run.

type TestRunner

type TestRunner struct {
	// ContinueOnFailure indicates whether the test runner should continue
	// executing further tests after a test encounters a failure.
	//
	// Default is false.
	ContinueOnFailure bool

	// GroupExecutionPriority indicates whether the test runner should execute
	// tests before or after subgroups.
	GroupExecutionPriority int

	// TestTimeout the the amount of time to wait for any single test to complete.
	//
	// Default is 10 seconds.
	TestTimeout time.Duration
}

A TestRunner runs a set of tests.

func NewTestRunner

func NewTestRunner() *TestRunner

NewTestRunner creates a new TestRunner with default configuration.

func (*TestRunner) RunTestGroup

func (r *TestRunner) RunTestGroup(group *TestGroup) *GroupRunResult

RunTestGroup runs a test group.

To run a test group within a Go test context, use RunTestGroupT().

func (*TestRunner) RunTestGroupT

func (r *TestRunner) RunTestGroupT(t *testing.T, group *TestGroup) *GroupRunResult

RunTestGroupT runs a test group within the context of a Go test.

To run tests as a standalone binary without a testing context, use RunTests().

func (*TestRunner) RunTestGroups added in v0.0.10

func (r *TestRunner) RunTestGroups(groups ...*TestGroup) *GroupRunResult

RunTestGroups runs a set of test groups using the default test runner.

func (*TestRunner) RunTestGroupsT added in v0.0.10

func (r *TestRunner) RunTestGroupsT(t *testing.T, groups ...*TestGroup) *GroupRunResult

RunTestGroupsT runs a set of test groups within the context of a Go test using the default test runner.

func (*TestRunner) RunTests

func (r *TestRunner) RunTests(tests ...TestCase) *GroupRunResult

RunTests runs a set of tests.

To run tests within a Go test context, use RunTestsT().

func (*TestRunner) RunTestsT

func (r *TestRunner) RunTestsT(t *testing.T, tests ...TestCase) *GroupRunResult

RunTestsT runs a set of tests within a Go test context.

To run tests standalone to print or examine results, use RunTests().

func (*TestRunner) WithContinueOnFailure

func (r *TestRunner) WithContinueOnFailure(continueOnFailure bool) *TestRunner

WithContinueOnFailure sets the ContinueOnFailure field of the TestRunner and returns the TestRunner.

func (*TestRunner) WithRequestTimeout

func (r *TestRunner) WithRequestTimeout(timeout time.Duration) *TestRunner

WithRequestTimeout sets the RequestTimeout field of the TestRunner and returns the TestRunner.

Jump to

Keyboard shortcuts

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