testutil

package
v0.0.0-...-b14404a Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

README

Snapshot Testing

Snapshot testing is a type of software testing that compares the output of a software application to a known good state, or snapshot. This is done by taking a snapshot of the application's output at a specific point in time, and then comparing it to the snapshot at a later point in time. If the two snapshots are not identical, then the application has changed in an unexpected way, and a test failure is reported.

Snapshot testing is a valuable tool for ensuring that software applications do not change in an unexpected way. It can be used to detect changes in the application's output, UI, behavior, and data. Snapshot testing is often used in conjunction with other types of software testing, such as unit testing and integration testing.

HTTP Dump

There is a standard library in golang to dump the HTTP request and response. However, the json output response is not pretty-printed.

package snapshottest

import (
	"net/http"
	"net/http/httputil"
	"strings"
)

func dump(w *http.Response, r *http.Request) (string, error) {
	req, err := httputil.DumpRequest(r, true)
	if err != nil {
		return "", err
	}

	res, err := httputil.DumpResponse(w, true)
	if err != nil {
		return "", err
	}

	output := make([]string, 3)
	output[0] = string(req)
	output[2] = string(res)

	return strings.Join(output, "\n"), nil
}

Patterns

  • dump input/output, e.g.
    • before and after unmarshalling
    • before and after mapping
    • before and after transforming
    • before and after calling a function
  • dump specific format, e.g.
    • httpdump
    • mysqldump
    • grpcdump
    • graphqldump
    • calcdump
    • markdowndump

Reference

https://solidstudio.io/blog/snapshot-testing

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpGRPC

func DumpGRPC(t *testing.T, ctx context.Context, opts ...GRPCOption) context.Context

func DumpHTTP

func DumpHTTP(t *testing.T, w *http.Response, r *http.Request, opts ...HTTPOption)

func DumpHTTPHandler

func DumpHTTPHandler(t *testing.T, r *http.Request, handler http.HandlerFunc, opts ...HTTPOption)

func DumpJSON

func DumpJSON[T any](t *testing.T, v T, opts ...JSONOption)

func DumpMySQL

func DumpMySQL(t *testing.T, dump *SQL, opts ...SQLOption)

func DumpPostgres

func DumpPostgres(t *testing.T, dump *SQL, opts ...SQLOption)

func DumpText

func DumpText(t *testing.T, s string, opts ...TextOption)

func DumpYAML

func DumpYAML[T any](t *testing.T, v T, opts ...YAMLOption)

func IgnoreArgs

func IgnoreArgs(fields ...string) *sqlCmpOption

func IgnoreBodyFields

func IgnoreBodyFields(fields ...string) *httpCmpOption

func IgnoreFields

func IgnoreFields(fields ...string) ignoreFields

func IgnoreHeaders

func IgnoreHeaders(headers ...string) *httpCmpOption

func IgnoreMessageFields

func IgnoreMessageFields(fields ...string) *grpcCmpOption

func IgnoreMetadata

func IgnoreMetadata(headers ...string) *grpcCmpOption

func IgnoreResultFields

func IgnoreResultFields(fields ...string) *sqlCmpOption

func IgnoreTrailers

func IgnoreTrailers(headers ...string) *httpCmpOption

func IgnoreVars

func IgnoreVars(fields ...string) *sqlCmpOption

func InspectGRPC

func InspectGRPC(hook func(snapshot, received *GRPCDump) error) *grpcHookOption

func InspectHTTP

func InspectHTTP(hook func(snapshot, received *HTTPDump) error) *httpHookOption

func InspectSQL

func InspectSQL(hook func(snapshot, received *SQL) error) *sqlHookOption

func InterceptGRPC

func InterceptGRPC(hook func(dump *GRPCDump) (*GRPCDump, error)) *grpcHookOption

func InterceptHTTP

func InterceptHTTP(hook func(dump *HTTPDump) (*HTTPDump, error)) *httpHookOption

func InterceptSQL

func InterceptSQL(hook func(t *SQL) (*SQL, error)) *sqlHookOption

func MaskFields

func MaskFields(fields ...string) maskFields

func MaskMessage

func MaskMessage(fields ...string) *grpcHookOption

func MaskMetadata

func MaskMetadata(headers ...string) *grpcHookOption

func MaskRequestBody

func MaskRequestBody(fields ...string) *httpHookOption

func MaskRequestHeaders

func MaskRequestHeaders(headers ...string) *httpHookOption

func MaskResponseBody

func MaskResponseBody(fields ...string) *httpHookOption

func MaskResponseHeaders

func MaskResponseHeaders(headers ...string) *httpHookOption

Types

type CmpOption

type CmpOption []cmp.Option

func CmpOpts

func CmpOpts(opts ...cmp.Option) CmpOption

type FileName

type FileName string

Those with x is shared.

type GRPCDump

type GRPCDump = testdump.GRPCDump

type GRPCHook

type GRPCHook = testdump.Hook[*GRPCDump]

type GRPCOption

type GRPCOption interface {
	// contains filtered or unexported methods
}

type HTTPDump

type HTTPDump = testdump.HTTPDump

type HTTPHook

type HTTPHook = testdump.HTTPHook

type HTTPOption

type HTTPOption interface {
	// contains filtered or unexported methods
}

type JSONOption

type JSONOption interface {
	// contains filtered or unexported methods
}

func CUESchema

func CUESchema(schema string, opts ...cue.Option) JSONOption

func CUESchemaPath

func CUESchemaPath(schemaPath string, opts ...cue.Option) JSONOption

type Path

type Path = internal.Path

type RoundTripper

type RoundTripper struct {
	// contains filtered or unexported fields
}

func DumpRoundTrip

func DumpRoundTrip(t *testing.T, opts ...HTTPOption) *RoundTripper

func (*RoundTripper) RoundTrip

func (t *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

type SQL

type SQL = testdump.SQL

func NewSQL

func NewSQL(query string, args []any, result any) *SQL

type SQLCall

type SQLCall struct {
	// contains filtered or unexported fields
}

type SQLDumper

type SQLDumper struct {
	// contains filtered or unexported fields
}

SQLDumper logs the query and args.

func DumpSQL

func DumpSQL(t *testing.T, db db, driverName string, opts ...SQLOption) *SQLDumper

DumpSQL ...

func (*SQLDumper) Calls

func (r *SQLDumper) Calls() int

Calls returns the number of calls made to the db.

func (*SQLDumper) Exec

func (r *SQLDumper) Exec(query string, args ...any) (sql.Result, error)

func (*SQLDumper) ExecContext

func (r *SQLDumper) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

func (*SQLDumper) Options

func (r *SQLDumper) Options(i int) []SQLOption

Options returns the options for the i-th call.

func (*SQLDumper) Prepare

func (r *SQLDumper) Prepare(query string) (*sql.Stmt, error)

func (*SQLDumper) PrepareContext

func (r *SQLDumper) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

func (*SQLDumper) Query

func (r *SQLDumper) Query(query string, args ...any) (*sql.Rows, error)

func (*SQLDumper) QueryContext

func (r *SQLDumper) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*SQLDumper) QueryRow

func (r *SQLDumper) QueryRow(query string, args ...any) *sql.Row

func (*SQLDumper) QueryRowContext

func (r *SQLDumper) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

func (*SQLDumper) SetCallOptions

func (r *SQLDumper) SetCallOptions(i int, optsByCall ...SQLOption)

SetCallOptions sets the options for the i-th call.

func (*SQLDumper) SetDB

func (r *SQLDumper) SetDB(db db)

SetDB sets the db.

func (*SQLDumper) SetDriverName

func (r *SQLDumper) SetDriverName(driverName string)

SetDriverName sets the driver name.

func (*SQLDumper) SetResult

func (r *SQLDumper) SetResult(v any)

type SQLOption

type SQLOption interface {
	// contains filtered or unexported methods
}

type TextOption

type TextOption interface {
	// contains filtered or unexported methods
}

type YAMLOption

type YAMLOption interface {
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

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