mocktesting

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2021 License: MIT Imports: 9 Imported by: 0

README

go-mocktesting

Mock *testing.T for the purpose of testing test helpers.

Go Reference GitHub tag (latest SemVer) Actions Status Coverage GitHub issues GitHub pull requests License Status

Import

import "github.com/jimeh/go-mocktesting"

Usage

func ExampleT_Error() {
	assertTrue := func(t testing.TB, v bool) {
		if v != true {
			t.Error("expected false to be true")
		}
	}

	mt := mocktesting.NewT("TestMyBoolean1")
	assertTrue(mt, true)
	fmt.Printf("Name: %s\n", mt.Name())
	fmt.Printf("Failed: %+v\n", mt.Failed())
	fmt.Printf("Aborted: %+v\n", mt.Aborted())

	mt = mocktesting.NewT("TestMyBoolean2")
	assertTrue(mt, false)
	fmt.Printf("Name: %s\n", mt.Name())
	fmt.Printf("Failed: %+v\n", mt.Failed())
	fmt.Printf("Aborted: %+v\n", mt.Aborted())
	fmt.Printf("Output: %s\n", strings.Join(mt.Output(), ""))

	// Output:
	// Name: TestMyBoolean1
	// Failed: false
	// Aborted: false
	// Name: TestMyBoolean2
	// Failed: true
	// Aborted: false
	// Output: expected false to be true
}
func ExampleT_Fatal() {
	requireTrue := func(t testing.TB, v bool) {
		if v != true {
			t.Fatal("expected false to be true")
		}
	}

	mt := mocktesting.NewT("TestMyBoolean1")
	fmt.Printf("Name: %s\n", mt.Name())
	requireTrue(mt, true)
	fmt.Printf("Failed: %+v\n", mt.Failed())
	fmt.Printf("Aborted: %+v\n", mt.Aborted())

	mt = mocktesting.NewT("TestMyBoolean2")
	fmt.Printf("Name: %s\n", mt.Name())
	mocktesting.Go(func() {
		requireTrue(mt, false)
		fmt.Println("This is never executed.")
	})
	fmt.Printf("Failed: %+v\n", mt.Failed())
	fmt.Printf("Aborted: %+v\n", mt.Aborted())
	fmt.Printf("Output:\n  - %s\n", strings.Join(mt.Output(), "\n  - "))

	// Output:
	// Name: TestMyBoolean1
	// Failed: false
	// Aborted: false
	// Name: TestMyBoolean2
	// Failed: true
	// Aborted: true
	// Output:
	//   - expected false to be true
}
func ExampleT_Run() {
	requireTrue := func(t testing.TB, v bool) {
		if v != true {
			t.Fatal("expected false to be true")
		}
	}

	mt := mocktesting.NewT("TestMyBoolean")
	fmt.Printf("Name: %s\n", mt.Name())
	mt.Run("true", func(t testing.TB) {
		requireTrue(t, true)
	})
	fmt.Printf("Failed: %+v\n", mt.Failed())
	fmt.Printf("Sub1-Name: %s\n", mt.Subtests()[0].Name())
	fmt.Printf("Sub1-Failed: %+v\n", mt.Subtests()[0].Failed())
	fmt.Printf("Sub1-Aborted: %+v\n", mt.Subtests()[0].Aborted())

	mt.Run("false", func(t testing.TB) {
		requireTrue(t, false)
		fmt.Println("This is never executed.")
	})
	fmt.Printf("Failed: %+v\n", mt.Failed())
	fmt.Printf("Sub2-Name: %s\n", mt.Subtests()[1].Name())
	fmt.Printf("Sub2-Failed: %+v\n", mt.Subtests()[1].Failed())
	fmt.Printf("Sub2-Aborted: %+v\n", mt.Subtests()[1].Aborted())
	fmt.Printf("Sub2-Output:\n  - %s\n",
		strings.Join(mt.Subtests()[1].Output(), "\n  - "),
	)

	// Output:
	// Name: TestMyBoolean
	// Failed: false
	// Sub1-Name: TestMyBoolean/true
	// Sub1-Failed: false
	// Sub1-Aborted: false
	// Failed: true
	// Sub2-Name: TestMyBoolean/false
	// Sub2-Failed: true
	// Sub2-Aborted: true
	// Sub2-Output:
	//   - expected false to be true
}

Documentation

Please see the Go Reference for documentation and examples.

License

MIT

Documentation

Overview

Package mocktesting provides a mock of *testing.T for the purpose of testing test helpers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go

func Go(f func())

Go runs the provided function in a new goroutine, and blocks until the goroutine has exited.

This is essentially a helper function to avoid aborting the current goroutine when a *T instance aborts the goroutine that any of FailNow(), Fatal(), Fatalf(), SkipNow(), Skip(), or Skipf() are called from.

Types

type Option

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

func WithBaseTempdir

func WithBaseTempdir(dir string) Option

WithBaseTempdir sets the base directory that TempDir() creates temporary directories within.

If this option is not used, the default base directory used is os.TempDir().

func WithDeadline

func WithDeadline(d time.Time) Option

WithDeadline specifies a custom timeout for the mock test, but setting the deadline to an exact value, rather than setting it based on the offset from now of a time.Duration. It effectively determines the return values of Deadline().

When given a empty time.Time{}, Deadline() will act as if no timeout has been set.

If this option is not used, the default timeout value is set to 10 minutes.

func WithNoAbort

func WithNoAbort() Option

WithNoAbort disables aborting the current goroutine with runtime.Goexit() when SkipNow or FailNow is called. This should be used with care, as it causes behavior to diverge from normal *tesing.T, as code after calling t.Fatal() will be executed.

func WithTestingT

func WithTestingT(testingT TestingT) Option

WithTestingT accepts a *testing.T instance which is used to report internal errors within *mocktesting.T itself. For example if the TempDir() function fails to create a temporary directory on disk, it will call Fatal() on the *testing.T instance provided here.

If this option is not used, internal errors will instead cause a panic.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout specifies a custom timeout for the mock test. It effectively determines the return values of Deadline().

When given a zero-value time.Duration, Deadline() will act as if no timeout has been set.

If this option is not used, the default timeout value is set to 10 minutes.

type T

type T struct {

	// Embed *testing.T to implement the testing.TB interface, which has a
	// private method to prevent it from being implemented. However that means
	// it's very difficult to test testing helpers.
	*testing.T
	// contains filtered or unexported fields
}

T is a fake/mock implementation of *testing.T. All methods available on *testing.T are available on *T with the exception of Run(), which has a slightly different func type.

All method calls against *T are recorded, so they can be inspected and asserted later. To be able to pass in *testing.T or *mocktesting.T, functions will need to use an interface instead of *testing.T explicitly.

For basic use cases, the testing.TB interface should suffice. For more advanced use cases, create a custom interface that exactly specifies the methods of *testing.T which are needed, and then freely pass *testing.T or *mocktesting.T.

func NewT

func NewT(name string, options ...Option) *T

func (*T) Aborted

func (t *T) Aborted() bool

Aborted returns true if the *T instance aborted the current goroutine via runtime.Goexit(), which is called by FailNow() and SkipNow().

This returns true even if *T was initialized using the WithNoAbort() option. Because the test was still instructed to abort, which is a separate matter than that *T was specifically set to not abort the current goroutine.

func (*T) Cleanup

func (t *T) Cleanup(f func())

Cleanup registers a cleanup function. *T does not run cleanup functions, it simply records them for the purpose of later inspection via CleanupFuncs() or CleanupNames().

Example
cleanup1 := func() {
	fmt.Println("running cleanup1")
}
cleanup2 := func() {
	fmt.Println("running cleanup2")
}

mt := mocktesting.NewT("TestMyCleanup")
mt.Cleanup(cleanup1)
mt.Cleanup(cleanup2)
fmt.Printf("Name: %s\n", mt.Name())
fmt.Printf(
	"CleanupNames:\n  - %s\n", strings.Join(mt.CleanupNames(), "\n  - "),
)
mt.CleanupFuncs()[1]()
mt.CleanupFuncs()[0]()
Output:

Name: TestMyCleanup
CleanupNames:
  - github.com/jimeh/go-mocktesting_test.ExampleT_Cleanup.func1
  - github.com/jimeh/go-mocktesting_test.ExampleT_Cleanup.func2
running cleanup2
running cleanup1

func (*T) CleanupFuncs

func (t *T) CleanupFuncs() []func()

CleanupFuncs returns a slice of functions given to Cleanup().

func (*T) CleanupNames

func (t *T) CleanupNames() []string

CleanupNames returns a string slice of function names given to Cleanup(). The names are resolved using runtime.FuncForPC(), meaning they include the absolute Go package path to the function, along with the function name itself.

func (*T) Deadline

func (t *T) Deadline() (time.Time, bool)

Name returns the time at which the *T instance is set to timeout. If no timeout is set, the bool return value is false, otherwise it is true.

func (*T) Error

func (t *T) Error(args ...interface{})

Error logs the given args with Log(), and then calls Fail() to mark the *T instance as failed.

Example
assertTrue := func(t testing.TB, v bool) {
	if v != true {
		t.Error("expected false to be true")
	}
}

mt := mocktesting.NewT("TestMyBoolean1")
assertTrue(mt, true)
fmt.Printf("Name: %s\n", mt.Name())
fmt.Printf("Failed: %+v\n", mt.Failed())
fmt.Printf("Aborted: %+v\n", mt.Aborted())

mt = mocktesting.NewT("TestMyBoolean2")
assertTrue(mt, false)
fmt.Printf("Name: %s\n", mt.Name())
fmt.Printf("Failed: %+v\n", mt.Failed())
fmt.Printf("Aborted: %+v\n", mt.Aborted())
fmt.Printf("Output: %s\n", strings.Join(mt.Output(), ""))

// Output:
// Name: TestMyBoolean1
// Failed: false
// Aborted: false
// Name: TestMyBoolean2
// Failed: true
// Aborted: false
Output:

Name: TestMyBoolean1
Failed: false
Aborted: false
Name: TestMyBoolean2
Failed: true
Aborted: false
Output: expected false to be true

func (*T) Errorf

func (t *T) Errorf(format string, args ...interface{})

Errorf logs the given format and args with Logf(), and then calls Fail() to mark the *T instance as failed.

Example
assertGreaterThan := func(t testing.TB, got int, min int) {
	if got <= min {
		t.Errorf("expected %d to be greater than %d", got, min)
	}
}

mt := mocktesting.NewT("TestMyBoolean1")
assertGreaterThan(mt, 6, 5)
fmt.Printf("Name: %s\n", mt.Name())
fmt.Printf("Failed: %+v\n", mt.Failed())
fmt.Printf("Aborted: %+v\n", mt.Aborted())

mt = mocktesting.NewT("TestMyBoolean2")
assertGreaterThan(mt, 4, 5)
fmt.Printf("Name: %s\n", mt.Name())
fmt.Printf("Failed: %+v\n", mt.Failed())
fmt.Printf("Aborted: %+v\n", mt.Aborted())
fmt.Printf("Output:\n  - %s\n", strings.Join(mt.Output(), "\n  - "))

// Output:
// Name: TestMyBoolean1
// Failed: false
// Aborted: false
// Name: TestMyBoolean2
// Failed: true
// Aborted: false
Output:

Name: TestMyBoolean1
Failed: false
Aborted: false
Name: TestMyBoolean2
Failed: true
Aborted: false
Output:
  - expected 4 to be greater than 5

func (*T) Fail

func (t *T) Fail()

Fail marks the *T instance as having failed. You can check if the *T instance has been failed with Failed(), or how many times it has been failed with FailedCount().

Example
assertTrue := func(t testing.TB, v bool) {
	if v != true {
		t.Fail()
	}
}

mt := mocktesting.NewT("TestMyBoolean1")
fmt.Printf("Name: %s\n", mt.Name())
assertTrue(mt, false)
fmt.Printf("Failed: %+v\n", mt.Failed())
Output:

Name: TestMyBoolean1
Failed: true

func (*T) FailNow

func (t *T) FailNow()

FailNow marks the *T instance as having failed, and also aborts the current goroutine with runtime.Goexit(). If the WithNoAbort() option was used when initializing the *T instance, runtime.Goexit() will not be called.

Example
requireTrue := func(t testing.TB, v bool) {
	if v != true {
		t.FailNow()
	}
}

mt := mocktesting.NewT("TestMyBoolean1")
fmt.Printf("Name: %s\n", mt.Name())
halted := true
mocktesting.Go(func() {
	requireTrue(mt, false)
	halted = false
})
fmt.Printf("Failed: %+v\n", mt.Failed())
fmt.Printf("Halted: %+v\n", halted)
Output:

Name: TestMyBoolean1
Failed: true
Halted: true

func (*T) Failed

func (t *T) Failed() bool

Failed returns true if the *T instance has been marked as failed.

func (*T) FailedCount

func (t *T) FailedCount() int

FailedCount returns the number of times the *T instance has been marked as failed.

func (*T) Fatal

func (t *T) Fatal(args ...interface{})

Fatal logs the given args with Log(), and then calls FailNow() to fail the *T instance and abort the current goroutine.

See FailNow() and WithNoAbort() for details about how abort works.

Example
requireTrue := func(t testing.TB, v bool) {
	if v != true {
		t.Fatal("expected false to be true")
	}
}

mt := mocktesting.NewT("TestMyBoolean1")
fmt.Printf("Name: %s\n", mt.Name())
requireTrue(mt, true)
fmt.Printf("Failed: %+v\n", mt.Failed())
fmt.Printf("Aborted: %+v\n", mt.Aborted())

mt = mocktesting.NewT("TestMyBoolean2")
fmt.Printf("Name: %s\n", mt.Name())
mocktesting.Go(func() {
	requireTrue(mt, false)
	fmt.Println("This is never executed.")
})
fmt.Printf("Failed: %+v\n", mt.Failed())
fmt.Printf("Aborted: %+v\n", mt.Aborted())
fmt.Printf("Output:\n  - %s\n", strings.Join(mt.Output(), "\n  - "))

// Output:
// Name: TestMyBoolean1
// Failed: false
// Aborted: false
// Name: TestMyBoolean2
// Failed: true
// Aborted: true
Output:

Name: TestMyBoolean1
Failed: false
Aborted: false
Name: TestMyBoolean2
Failed: true
Aborted: true
Output:
  - expected false to be true

func (*T) Fatalf

func (t *T) Fatalf(format string, args ...interface{})

Fatalf logs the given format and args with Logf(), and then calls FailNow() to fail the *T instance and abort the current goroutine.

See FailNow() and WithNoAbort() for details about how abort works.

Example
requireGreaterThan := func(t testing.TB, got int, min int) {
	if got <= min {
		t.Fatalf("expected %d to be greater than %d", got, min)
	}
}

mt := mocktesting.NewT("TestMyGT1")
fmt.Printf("Name: %s\n", mt.Name())
halted := true
mocktesting.Go(func() {
	requireGreaterThan(mt, 6, 5)
	halted = false
})
fmt.Printf("Failed: %+v\n", mt.Failed())
fmt.Printf("Halted: %+v\n", halted)

mt = mocktesting.NewT("TestMyGT2")
fmt.Printf("Name: %s\n", mt.Name())
halted = true
mocktesting.Go(func() {
	requireGreaterThan(mt, 4, 5)
	halted = false
})
fmt.Printf("Failed: %+v\n", mt.Failed())
fmt.Printf("Halted: %+v\n", halted)
fmt.Printf("Output:\n  - %s\n", strings.Join(mt.Output(), "\n  - "))

// Output:
// Name: TestMyGT1
// Failed: false
// Halted: false
// Name: TestMyGT2
// Failed: true
// Halted: true
Output:

Name: TestMyGT1
Failed: false
Halted: false
Name: TestMyGT2
Failed: true
Halted: true
Output:
  - expected 4 to be greater than 5

func (*T) Getenv

func (t *T) Getenv() map[string]string

Getenv returns a map[string]string of keys/values given to Setenv().

func (*T) Helper

func (t *T) Helper()

Helper marks the function that is calling Helper() as a helper function. Within *T it simply stores a reference to the function.

The list of functions which have called Helper() can be inspected with HelperNames(). The names are resolved using runtime.FuncForPC(), meaning they include the absolute Go package path to the function, along with the function name itself.

Example
assertTrue := func(t testing.TB, v bool) {
	t.Helper()

	if v != true {
		t.Error("expected false to be true")
	}
}

mt := mocktesting.NewT("TestMyBoolean1")
assertTrue(mt, true)
assertTrue(mt, true)
fmt.Printf("Name: %s\n", mt.Name())
fmt.Printf("Helpers:\n  - %s\n", strings.Join(mt.HelperNames(), "\n  - "))
Output:

Name: TestMyBoolean1
Helpers:
  - github.com/jimeh/go-mocktesting_test.ExampleT_Helper.func1
  - github.com/jimeh/go-mocktesting_test.ExampleT_Helper.func1

func (*T) HelperNames

func (t *T) HelperNames() []string

HelperNames returns a list of function names which called Helper(). The names are resolved using runtime.FuncForPC(), meaning they include the absolute Go package path to the function, along with the function name itself.

func (*T) Log

func (t *T) Log(args ...interface{})

Log renders given args to a string with fmt.Sprintln() and stores the result in a string slice which can be accessed with Output().

Example
logHello := func(t testing.TB) {
	t.Log("hello world")
}

mt := mocktesting.NewT("TestMyLog")
fmt.Printf("Name: %s\n", mt.Name())
logHello(mt)
fmt.Printf("Output:\n  - %s\n", strings.Join(mt.Output(), "\n  - "))

// Output:
// Name: TestMyLog
Output:

Name: TestMyLog
Output:
  - hello world

func (*T) Logf

func (t *T) Logf(format string, args ...interface{})

Logf renders given format and args to a string with fmt.Sprintf() and stores the result in a string slice which can be accessed with Output().

Example
logHello := func(t testing.TB, name string) {
	t.Logf("hello, %s", name)
}

mt := mocktesting.NewT("TestMyLogf")
fmt.Printf("Name: %s\n", mt.Name())
logHello(mt, "Abel")
fmt.Printf("Output:\n  - %s\n", strings.Join(mt.Output(), "\n  - "))

// Output:
// Name: TestMyLogf
Output:

Name: TestMyLogf
Output:
  - hello, Abel

func (*T) Name

func (t *T) Name() string

Name returns the name given to the *T instance.

func (*T) Output

func (t *T) Output() []string

Output returns a string slice of all output produced by calls to Log() and Logf().

func (*T) Parallel

func (t *T) Parallel()

Parallel marks the *T instance to indicate Parallel() has been called. Use Paralleled() to check if Parallel() has been called.

Example
logHello := func(t testing.TB, name string) {
	mt, _ := t.(*mocktesting.T)
	mt.Parallel()
	mt.Logf("hello, %s", name)
}

mt := mocktesting.NewT("TestMyLogf")
fmt.Printf("Name: %s\n", mt.Name())
fmt.Printf("Parallel (before): %+v\n", mt.Paralleled())
logHello(mt, "Abel")
fmt.Printf("Parallel (after): %+v\n", mt.Paralleled())
Output:

Name: TestMyLogf
Parallel (before): false
Parallel (after): true

func (*T) Paralleled

func (t *T) Paralleled() bool

Paralleled returns true if Parallel() has been called.

func (*T) Run

func (t *T) Run(name string, f func(testing.TB)) bool

Run allows running sub-tests just very much like *testing.T. The one difference is that the function argument accepts a testing.TB instead of *testing.T type. This is to allow passing a *mocktesting.T to the sub-test function instead of a *testing.T.

Sub-test functions are executed in a separate blocking goroutine, so calls to SkipNow() and FailNow() abort the new goroutine that the sub-test is running in, rather than the gorouting which is executing Run().

The sub-test function will receive a new instance of *T which is a sub-test, which name and other attributes set accordingly.

If any sub-test *T is marked as failed, the parent *T instance will also be marked as failed.

The list of sub-test *T instances can be accessed with Subtests().

Example
requireTrue := func(t testing.TB, v bool) {
	if v != true {
		t.Fatal("expected false to be true")
	}
}

mt := mocktesting.NewT("TestMyBoolean")
fmt.Printf("Name: %s\n", mt.Name())
mt.Run("true", func(t testing.TB) {
	requireTrue(t, true)
})
fmt.Printf("Failed: %+v\n", mt.Failed())
fmt.Printf("Sub1-Name: %s\n", mt.Subtests()[0].Name())
fmt.Printf("Sub1-Failed: %+v\n", mt.Subtests()[0].Failed())
fmt.Printf("Sub1-Aborted: %+v\n", mt.Subtests()[0].Aborted())

mt.Run("false", func(t testing.TB) {
	requireTrue(t, false)
	fmt.Println("This is never executed.")
})
fmt.Printf("Failed: %+v\n", mt.Failed())
fmt.Printf("Sub2-Name: %s\n", mt.Subtests()[1].Name())
fmt.Printf("Sub2-Failed: %+v\n", mt.Subtests()[1].Failed())
fmt.Printf("Sub2-Aborted: %+v\n", mt.Subtests()[1].Aborted())
fmt.Printf("Sub2-Output:\n  - %s\n",
	strings.Join(mt.Subtests()[1].Output(), "\n  - "),
)
Output:

Name: TestMyBoolean
Failed: false
Sub1-Name: TestMyBoolean/true
Sub1-Failed: false
Sub1-Aborted: false
Failed: true
Sub2-Name: TestMyBoolean/false
Sub2-Failed: true
Sub2-Aborted: true
Sub2-Output:
  - expected false to be true
Example (Nested)
assertGreaterThan := func(t testing.TB, got int, min int) {
	if got <= min {
		t.Errorf("expected %d to be greater than %d", got, min)
	}
}

mt := mocktesting.NewT("TestMyBoolean")
mt.Run("positive", func(t testing.TB) {
	subMT, _ := t.(*mocktesting.T)

	subMT.Run("greater than", func(t testing.TB) {
		assertGreaterThan(t, 5, 4)
	})
	subMT.Run("equal", func(t testing.TB) {
		assertGreaterThan(t, 5, 5)
	})
	subMT.Run("less than", func(t testing.TB) {
		assertGreaterThan(t, 4, 5)
	})
})
fmt.Printf("Name: %s\n", mt.Name())
fmt.Printf("Failed: %+v\n", mt.Failed())
fmt.Printf("Sub1-Name: %s\n", mt.Subtests()[0].Name())
fmt.Printf("Sub1-Failed: %+v\n", mt.Subtests()[0].Failed())
fmt.Printf("Sub1-Aborted: %+v\n", mt.Subtests()[0].Aborted())
fmt.Printf("Sub1-Sub1-Name: %s\n", mt.Subtests()[0].Subtests()[0].Name())
fmt.Printf(
	"Sub1-Sub1-Failed: %+v\n", mt.Subtests()[0].Subtests()[0].Failed(),
)
fmt.Printf(
	"Sub1-Sub1-Aborted: %+v\n", mt.Subtests()[0].Subtests()[0].Aborted(),
)
fmt.Printf("Sub1-Sub1-Name: %s\n", mt.Subtests()[0].Subtests()[1].Name())
fmt.Printf(
	"Sub1-Sub2-Failed: %+v\n", mt.Subtests()[0].Subtests()[1].Failed(),
)
fmt.Printf(
	"Sub1-Sub2-Aborted: %+v\n", mt.Subtests()[0].Subtests()[1].Aborted(),
)
fmt.Printf(
	"Sub1-Sub3-Output:\n  - %s\n", strings.TrimSpace(
		strings.Join(mt.Subtests()[0].Subtests()[1].Output(), "\n  - "),
	),
)
fmt.Printf("Sub1-Sub1-Name: %s\n", mt.Subtests()[0].Subtests()[2].Name())
fmt.Printf(
	"Sub1-Sub3-Failed: %+v\n", mt.Subtests()[0].Subtests()[2].Failed(),
)
fmt.Printf(
	"Sub1-Sub3-Aborted: %+v\n", mt.Subtests()[0].Subtests()[2].Aborted(),
)
fmt.Printf(
	"Sub1-Sub3-Output:\n  - %s\n", strings.TrimSpace(
		strings.Join(mt.Subtests()[0].Subtests()[2].Output(), "\n  - "),
	),
)
Output:

Name: TestMyBoolean
Failed: true
Sub1-Name: TestMyBoolean/positive
Sub1-Failed: true
Sub1-Aborted: false
Sub1-Sub1-Name: TestMyBoolean/positive/greater_than
Sub1-Sub1-Failed: false
Sub1-Sub1-Aborted: false
Sub1-Sub1-Name: TestMyBoolean/positive/equal
Sub1-Sub2-Failed: true
Sub1-Sub2-Aborted: false
Sub1-Sub3-Output:
  - expected 5 to be greater than 5
Sub1-Sub1-Name: TestMyBoolean/positive/less_than
Sub1-Sub3-Failed: true
Sub1-Sub3-Aborted: false
Sub1-Sub3-Output:
  - expected 4 to be greater than 5

func (*T) Setenv

func (t *T) Setenv(key string, value string)

func (*T) Skip

func (t *T) Skip(args ...interface{})

Skip logs the given args with Log(), and then uses SkipNow() to mark the *T instance as skipped and aborts the current goroutine.

See SkipNow() for more details about aborting the current goroutine.

Example
logHello := func(t testing.TB, name string) {
	if name == "" {
		t.Skip("no name given to say hello to")
	}
	t.Logf("hello, %s", name)
}

mt := mocktesting.NewT("TestMyLog1")
fmt.Printf("Name: %s\n", mt.Name())
halted := true
mocktesting.Go(func() {
	logHello(mt, "Abel")
	halted = false
})
fmt.Printf("Skipped: %+v\n", mt.Skipped())
fmt.Printf("Halted: %+v\n", halted)

mt = mocktesting.NewT("TestMyLog2")
fmt.Printf("Name: %s\n", mt.Name())
halted = true
mocktesting.Go(func() {
	logHello(mt, "")
	halted = false
})
fmt.Printf("Skipped: %+v\n", mt.Skipped())
fmt.Printf("Halted: %+v\n", halted)
fmt.Printf("Output:\n  - %s\n", strings.Join(mt.Output(), "\n  - "))

// Output:
// Name: TestMyLog1
// Skipped: false
// Halted: false
// Name: TestMyLog2
// Skipped: true
// Halted: true
Output:

Name: TestMyLog1
Skipped: false
Halted: false
Name: TestMyLog2
Skipped: true
Halted: true
Output:
  - no name given to say hello to

func (*T) SkipNow

func (t *T) SkipNow()

SkipNow marks the *T instance as skipped, and then aborts the current goroutine with runtime.Goexit(). If the WithNoAbort() option was used when initializing the *T instance, runtime.Goexit() will not be called.

Example
logHello := func(t testing.TB, name string) {
	if name == "" {
		t.SkipNow()
	}
}

mt := mocktesting.NewT("TestMyLog1")
fmt.Printf("Name: %s\n", mt.Name())
halted := true
mocktesting.Go(func() {
	logHello(mt, "Abel")
	halted = false
})
fmt.Printf("Skipped: %+v\n", mt.Skipped())
fmt.Printf("Halted: %+v\n", halted)

mt = mocktesting.NewT("TestMyLog2")
fmt.Printf("Name: %s\n", mt.Name())
halted = true
mocktesting.Go(func() {
	logHello(mt, "")
	halted = false
})
fmt.Printf("Skipped: %+v\n", mt.Skipped())
fmt.Printf("Halted: %+v\n", halted)
Output:

Name: TestMyLog1
Skipped: false
Halted: false
Name: TestMyLog2
Skipped: true
Halted: true

func (*T) Skipf

func (t *T) Skipf(format string, args ...interface{})

Skipf logs the given format and args with Logf(), and then uses SkipNow() to mark the *T instance as skipped and aborts the current goroutine.

See SkipNow() for more details about aborting the current goroutine.

Example
logHello := func(t testing.TB, name string) {
	if name != "Jane" {
		t.Skipf("I only say hello to Jane, you are %s", name)
	}
}

mt := mocktesting.NewT("TestMyLog1")
fmt.Printf("Name: %s\n", mt.Name())
halted := true
mocktesting.Go(func() {
	logHello(mt, "Jane")
	halted = false
})
fmt.Printf("Skipped: %+v\n", mt.Skipped())
fmt.Printf("Halted: %+v\n", halted)

mt = mocktesting.NewT("TestMyLog2")
fmt.Printf("Name: %s\n", mt.Name())
halted = true
mocktesting.Go(func() {
	logHello(mt, "John")
	halted = false
})
fmt.Printf("Skipped: %+v\n", mt.Skipped())
fmt.Printf("Halted: %+v\n", halted)
fmt.Printf("Output:\n  - %s\n", strings.Join(mt.Output(), "\n  - "))

// Output:
// Name: TestMyLog1
// Skipped: false
// Halted: false
// Name: TestMyLog2
// Skipped: true
// Halted: true
Output:

Name: TestMyLog1
Skipped: false
Halted: false
Name: TestMyLog2
Skipped: true
Halted: true
Output:
  - I only say hello to Jane, you are John

func (*T) Skipped

func (t *T) Skipped() bool

Skipped returns true if the *T instance has been marked as skipped, otherwise it returns false.

func (*T) Subtests

func (t *T) Subtests() []*T

Subtests returns a slice of *T instances created for any subtests executed via Run().

func (*T) TempDir

func (t *T) TempDir() string

TempDir creates an actual temporary directory on the system using ioutil.TempDir(). This actually does perform a action, rather than just recording the fact the method was called list most other *T methods.

This is because returning a string that is not the path to a real directory, would most likely be useless. Hence it does create a real temporary directory.

It is important to note that the temporary directory is not cleaned up by mocktesting. But it is created via ioutil.TempDir(), so the operating system should eventually clean it up.

A string slice of temporary directory paths created by calls to TempDir() can be accessed with TempDirs().

func (*T) TempDirs

func (t *T) TempDirs() []string

TempDirs returns a string slice of temporary directories created by TempDir().

type TestingT

type TestingT interface {
	Fatal(args ...interface{})
}

TestingT is an interface covering *mocktesting.T's internal use of *testing.T. See WithTestingT() for more details.

Jump to

Keyboard shortcuts

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