gotestingmock

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2025 License: MIT Imports: 3 Imported by: 0

README

gotestingmock Go ReferenceGo Report Card

gotestingmock mocking utilities for unit test in Go.

Usage

func Test(t *testing.T) {
	t.Parallel()

	// gotestingmock.Run simulates a test function on a goroutine.
	got := gotestingmock.Run(func(tb *gotestingmock.TB) {
		// The test helper can use *gotestingmock.TB as testing.TB
		// which is implemented  by testing.T, testing.B and testing.F.
		MyTestHelper(tb, "arg1")
	})

	// Check if the test helper failed with t.Error, t.Fatal or similar methods.
	if !got.Failed {
		t.Error("expected failed did not occur")
	}

	// Check that the test helper has panicked.
	if got.PanicValue != nil {
		t.Error("unexpected panic:", got.PanicValue)
	}
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Record

type Record struct {
	Failed     bool
	Skipped    bool
	Goexit     bool
	PanicValue any
}

Record records the result of Run.

func Run

func Run(f func(*TB)) *Record

Run runs the given mocking test function with testing.TB. The f can be described in the same way as the test function of the go test. Run call the f on new goroutine. The return value records whether the test function failed (e.g. t.Error), was skipped (e.g. t.Skip), failed and exited its goroutine (e.g. t.Fatal) or panic occured.

type TB

type TB struct {

	// mock funcs
	CleanupFunc func(func())
	ErrorFunc   func(args ...any)
	ErrorfFunc  func(format string, args ...any)
	FailFunc    func()
	FailNowFunc func()
	FailedFunc  func() bool
	FatalFunc   func(args ...any)
	FatalfFunc  func(format string, args ...any)
	HelperFunc  func()
	LogFunc     func(args ...any)
	LogfFunc    func(format string, args ...any)
	NameFunc    func() string
	SetenvFunc  func(key, value string)
	SkipFunc    func(args ...any)
	SkipNowFunc func()
	SkipfFunc   func(format string, args ...any)
	SkippedFunc func() bool
	TempDirFunc func() string

	testing.TB // for default behavior andd private method
	// contains filtered or unexported fields
}

TB is mock for testing.TB. XxxFunc is a mock function for the method Xxx of testing.TB. If you confirm more usage please see Run.

func (*TB) Cleanup

func (tb *TB) Cleanup(f func())

func (*TB) Error

func (tb *TB) Error(args ...any)

func (*TB) Errorf

func (tb *TB) Errorf(format string, args ...any)

func (*TB) Fail

func (tb *TB) Fail()

func (*TB) FailNow

func (tb *TB) FailNow()

func (*TB) Failed

func (tb *TB) Failed() bool

func (*TB) Fatal

func (tb *TB) Fatal(args ...any)

func (*TB) Fatalf

func (tb *TB) Fatalf(format string, args ...any)

func (*TB) Helper

func (tb *TB) Helper()

func (*TB) Log

func (tb *TB) Log(args ...any)

func (*TB) Logf

func (tb *TB) Logf(format string, args ...any)

func (*TB) Name

func (tb *TB) Name() string

func (*TB) Setenv

func (tb *TB) Setenv(key, value string)

func (*TB) Skip

func (tb *TB) Skip(args ...any)

func (*TB) SkipNow

func (tb *TB) SkipNow()

func (*TB) Skipf

func (tb *TB) Skipf(format string, args ...any)

func (*TB) Skipped

func (tb *TB) Skipped() bool

func (*TB) TempDir

func (tb *TB) TempDir() string

Jump to

Keyboard shortcuts

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