Skip to content

proposal: testing: expose a constructor for *testing.T #28021

Description

@pmallela

Can we expose a constructor that creates a minimal *testing.T? (..or even make T an interface)?

I have some helpers which I use for ease of testing. e.g.

func Assert(t *testing.T, isTrue bool, args ...interface{}) {
	if !isTrue {
		t.Fatal(args...)
	}
}

I was thinking about testing this Assert, by passing in a new *testing.T and checking if t.Failed() but my code panics because T is unable to be initialized properly.

func TestAssertFails(t *testing.T) {
	t.Parallel()
	innerT := &testing.T{}
	testing_utils.Assert(innerT, false, "omg")
	if !innerT.Failed() {
		t.Errorf("Assert did not fail the test")
	}
}
	panic: test executed panic(nil) or runtime.Goexit
	
	goroutine 5 [running]:
	testing.tRunner.func1(0xc42009c0f0)
		/usr/local/Cellar/go/1.10.1/libexec/src/testing/testing.go:742 +0x29d
	runtime.Goexit()
		/usr/local/Cellar/go/1.10.1/libexec/src/runtime/panic.go:377 +0x117
	testing.(*common).FailNow(0xc42009c2d0)
		/usr/local/Cellar/go/1.10.1/libexec/src/testing/testing.go:553 +0x39
	testing.(*common).Fatal(0xc42009c2d0, 0xc420033f88, 0x1, 0x1)
		/usr/local/Cellar/go/1.10.1/libexec/src/testing/testing.go:591 +0x6f

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions