No description
Find a file
2021-11-22 03:23:33 +00:00
.github/workflows ci(github): setup GitHub Actions to run CI tasks (lint, text, etc) 2021-11-21 23:49:46 +00:00
.gitignore feat(mocktesting): initial implementation 2021-11-21 23:49:45 +00:00
.golangci.yml ci(github): setup GitHub Actions to run CI tasks (lint, text, etc) 2021-11-21 23:49:46 +00:00
CHANGELOG.md chore(release): 0.1.0 2021-11-22 03:23:33 +00:00
go.mod feat(mocktesting): initial implementation 2021-11-21 23:49:45 +00:00
go.sum feat(mocktesting): initial implementation 2021-11-21 23:49:45 +00:00
LICENSE chore: add license file 2021-11-21 23:49:46 +00:00
Makefile feat(mocktesting): initial implementation 2021-11-21 23:49:45 +00:00
mocktesting.go feat(mocktesting): initial implementation 2021-11-21 23:49:45 +00:00
mocktesting_test.go feat(mocktesting): initial implementation 2021-11-21 23:49:45 +00:00
README.md docs(readme): add release badge 2021-11-22 03:22:41 +00:00
t.go docs(godoc): improve godoc comments for all *T methods 2021-11-22 03:09:16 +00:00
t_example_test.go docs(examples): update and flesh out example functions 2021-11-22 03:09:16 +00:00
t_go116.go feat(mocktesting): initial implementation 2021-11-21 23:49:45 +00:00
t_go116_test.go feat(mocktesting): initial implementation 2021-11-21 23:49:45 +00:00
t_test.go chore(test): fix typo in test function name and var/field names 2021-11-22 02:03:49 +00:00

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