Pytest 2.3 Adding Teardowns Within The Class
I'm researching new version of pytest (2.3) and getting very excited about the new functionality where you 'can precisely control teardown by registering one or multiple teardo
Solution 1:
Ok, I got it working by having a 'session'-wide funcarg finalizer
:
@pytest.fixture(scope = "session")deffinalizer():
return Finalizer()
classFinalizer(object):
def__init__(self):
self.fin_funcs = []
defadd_fin_func(self, func):
self.fin_funcs.append(func)
defremove_fin_func(self, func):
try:
self.fin_funcs.remove(func)
except:
passdefexecute(self):
for func inreversed(self.fin_funcs):
func()
self.fin_funcs = []
classTestSomething(object):
@classmethod @pytest.fixture(scope = "class", autouse = True)defsetup(self, request, finalizer):
self.finalizer = finalizer
request.addfinalizer(self.finalizer.execute)
self.finalizer.add_fin_func(lambda: some_teardown())
deftest_with_teardown(self):
#some test
self.finalizer.add_fin_func(self.additional_teardown)
defadditional_teardown(self):
#additional teardown
Thanks @hpk42 for answering e-mails and helping me get the final version.
NOTE: together with xfailing the rest of the steps and improved scenarios this now makes a pretty good Test-Step structure
Solution 2:
Indeed, there are no good examples for teardown yet. The request object has a addfinalizer method. Here is an example usage:
@pytest.setup(scope=...)defmysetup(request):
...
request.addfinalizer(finalizerfunction)
...
The finalizerfunction will be called when all tests withing the scope finished execution.
Post a Comment for "Pytest 2.3 Adding Teardowns Within The Class"