stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
22.48k stars 1.56k forks source link

Feature request: add BeforeSetupTest and AfterTearDownTest #1604

Open sirzooro opened 1 month ago

sirzooro commented 1 month ago

Description

Methods of existing BeforeTest and AfterTest interfaces are called after SetupTest and before TearDownTest ones. I need ones which are called before and after them, i.e. "wrap" whole test with its Setup and TestDown parts. Please add new interfaces BeforeSetupTest and AfterTearDownTest for this.

Use case

I have custom Suite type which wraps suite.Suite. I want to do extra things before and after each test, like printing custom start/end messages and checking for goroutine leaks. For subtests I was able to provide my own Run method which internally calls one from suite.Suite. For tests there is no easy way to do this.

hendrywiranto commented 3 weeks ago

hi @sirzooro

I'm no expert for the suite interfaces, but can you use SetupSuite and TearDownSuite?

I looked for more doc of the ordering but I still can't find the complete docs. might do some testing when I have the time

sirzooro commented 3 weeks ago

SetupSuite and TearDownSuite are called before 1st test and after last one, respectively. They are useful to perform some costly global initialization and cleanup for whole test suite, e.g. setup test DB before all tests and shut it down after all tests are completed. So no, they do not satisfy my needs.

And yes, docs does not specify ordering, one has to check this in the code. It would be good to add this to the docs. I always check source code of suite.Run when I need to check it: https://github.com/stretchr/testify/blob/master/suite/suite.go#L121

hendrywiranto commented 2 weeks ago

I see, thank you for explaining.

I was thinking of just adding your operations that you want to be before SetupTest just on the start of the SetupTest itself, so no need to add another wrapper for this since both SetupTest and your suggested feature is executed in order on each tests. does that make sense to you?