In pytest
we can define a session
-scoped fixture, so the following
@pytest.fixture(scope="session", autouse=True)
def some_fixt():
... some setup procedure ...
yield
... some cleanup procedure ...
will automagically set things up before testing session begins, and clean things up afterwards.
Is there something similar in unittest
? The best I can find is setUpModule
+tearDownModule
, but that's just per-module, necessitating copy-paste among different testing modules.
EDIT: This is my current workaround:
For each and every Test Module:
from contextlib import ExitStack
...
ModuleResources = ExitStack()
def setUpModule():
# For funcs/objects that can act as a context_manager
ModuleResources.enter_context(
... something that can act as a context_manager ...
)
# For funcs/objects that cannot act as a context manager,
# but has a "cleanup" method such as "stop" or "close"
... invoke setup ...
ModuleResources.callback(thing.cleanup)
def tearDownModule():
ModuleResources.close()
The problem is, of course, I have to paste all those code for each and every test module. So I'm looking for something simpler here.