Skip to content

Provide FuncPC for sub-tests #4

@metafates

Description

@metafates

Feature

Package testoreflect provides FuncPC uintptr field with program counter of a function which defines either regular test or parametrized test.

With it, we can obtain file and line of such tests

pc := testo.Reflect(t).Test.(testoreflect.RegularTestInfo).FuncPC

f := runtime.FuncForPC(pc)

fmt.Println(f.FileLine(pc))

However, it is only available for top-level tests and is equal to 0 for tests started with testo.Run (sub-tests).

See runner.go file for definition of testo.Run.

Usage example (won't work without this feature):

func (Suite) Test(t T) {
	testo.Run(t, "my subtest", func(t T) {
		pc := testo.Reflect(t).Test.(testoreflect.RegularTestInfo).FuncPC

		f := runtime.FuncForPC(pc)

		fmt.Println(f.FileLine(pc))
	})

	testo.Run(t, "predefined", someFunc)
}

func someFunc(t T) {
	pc := testo.Reflect(t).Test.(testoreflect.RegularTestInfo).FuncPC

	f := runtime.FuncForPC(pc)

	fmt.Println(f.FileLine(pc))
}

Metadata

Metadata

Assignees

Labels

No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions