-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Description
Hi,
We are currently using the TestMain function to setup and teardown test-containers in our integration tests. This allows us to prepare the environment once for the entire test suite and save time.
We also monitor our test CI step by using gotestsum which practically parses the output from go test -json. This lets us keep the CI snappy.
One issue we are having is that there is no way to monitor TestMain's timing or outcome. In gotestsum there is some code to add a fake test case in the case the failure occurred in TestMain (see comment by @dnephin ). But, there is still no way to monitor how long did the logic before and after the tests ran.
There are several possible ways to tackle this IMO:
Declaring setup and teardown functions
Introduce new functions for setup and teardown:
func TestMain(m *testing.M) {
m.Setup(setupFunc)
m.Teardown(teardownFunc)
}This can work just like testing.T.Cleanup does. I think this is beneficial for other use cases where there is a need to better organize testing code.
Emitted JSONs could look like:
{"Time":"2022-12-10T14:13:37.4857284-02:00","Action":"pass","Package":"example.com/cool/app","Function":"setupFunc","Elapsed":0.0321}Monitor TestMain timing without m.Run time
Another option is to time the entire TestMain but stop the timer on m.Run and resume when it ends.
Hypothetical code:
func (m *testing.M) Run() int {
m.timer.Stop()
defer m.timer.Continue()
# ... run tests
}Emitted JSONs could look like:
{"Time":"2022-12-10T14:13:37.4857284-02:00","Action":"pass","Package":"example.com/cool/app","Function":"TestMain","Elapsed":10.3853}Metadata
Metadata
Assignees
Labels
Type
Projects
Status