Not out of the box, but it shouldn't be hard to rig up yourself. Any function named init
is guaranteed to be run before anything else, and that's true for tests as well.
In your test file:
var listener pkg.EventListener
func init() {
pkg.SetupMyFramework()
listener = pkg.Listener()
}
Then in any test
func TestXxx(t *testing.T) {
listener.Dispatch(pkg.MakeEvent("TestXxx", pkg.TestStarted))
err := DoSomething()
if err != nil {
listener.Dispatch(pkg.MakeEvent("TestXxx", pkg.TestFailed))
t.Fatal("Test failed")
}
listener.Dispatch(pkg.MakeEvent("TestXxx", pkg.TestPassed))
}
You can, of course, extend this however you want (using channels, making wrapper functions around Fatal
to make this less verbose, etc), but that's the gist of how you can integrate it.
Edit: I wrote a really simple wrapper over *testing.T
and an event dispatching framework here: https://github.com/Jragonmiris/testevents
It's totally untested (heh), but it should be relatively close to what you want. It aims to require only one or two extra method calls per test to hook up the dispatching code.