I am using TestMain to test multiple implementation of the same interface
func TestMain(m *testing.M) {
setup1()
code = m.Run()
if code != 0 {
os.exit(code)
}
setup2()
code = m.Run()
if code != 0 {
os.exit(code)
}
}
If I then get an error in a test, it is difficult to know which implementation generated the failure
In T subtests, you run like this :
t.run("test name", testfunc)
is there a way to do something to prefix the Main tests
m.Run("name") // intuitively what I should be able to do
EDIT : Adding some background because it looks like people are not seeing what it is about
// MyInterface is implemented multiple time, but we expect the same behavior for any implementation
type IMyInterface interface {
SomeMethod()
SomeOtherMethod()
}
var (
implUnderTest IMyInterface // is referred to in every tests
)
// There are many test files with many tests run for each implementation
func TestMain(m *testing.M) {
for _, impl := range []IMyInterface {&Impl1{}, &Impl2{}, &Impl3{}} {
implUnderTest = impl
code := m.Run()
if code != 0 {
os.exit(code)
}
}
}
Calling m.Run()
multiple times seems like an anti-pattern. Create multiple tests that have different setups and name them accordingly. AFAIK there is no m.Run("name")
I am still looking for a better solution, until then I'm doing something like the following
func TestMain(m *testing.M) {
setup1()
code = m.Run()
if code != 0 {
log.Println("failed on implementation 1")
os.exit(code)
}
setup2()
code = m.Run()
if code != 0 {
log.Println("failed on implementation 2")
os.exit(code)
}
}