You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am looking to parametrize a class which has more than 1 tests in it. I want a setup/teardown method called for each instance of the class.
BTW, I am not sure if class level fixture works the way I think it (I am assuming for each class level parameter I get new class instance). I would appreciate if someone can educate me if my understanding is wrong.
@pytest.fixture(scope="class", params=[2,4,8])defn_iter(request):
returnrequest.param@pytest.mark.usefixtures("n_iter")classTest_Class1:
@classmethoddefsetup_class(cls):
''' This gets called only once not per parameter of class : no issues '''cls.buffer= []
pdb.set_trace()
log.info("setup class")
defsetup(self):
''' I want this to be called only once and all the test below gets executed one after other :instead this gets called for every test method '''self.buffer.append(len(self.buffer))
log.info("setup instance of class")
defteardown(self):
''' I want this to be called only once after all the tests are completed for a class level param :instead this gets called after every test method '''self.buffer= [] # clear@pytest.mark.parametrize('newVal', [2,4])deftest_clsMethod1(self, newVal, n_iter):
assertn_iter==newValdeftest_clsMethod2(self, n_iter):
assertn_iter>0@pytest.mark.parametrize('newVal', [2,4])deftest_clsMethod3(self, newVal):
assertnewVal>0
The text was updated successfully, but these errors were encountered:
GitMate.io thinks possibly related issues are #3359 (Allow methods to access a class fixture. ), #2938 (fixture with scope “class” running for every method), #2270 (Question: writing a plugin with a fixture that can access its plugin instance properties), #674 (Fixtures from methods wrongly get rebound to request.instance), and #484 (Order of class fixtures).
#484 solution is kind of a good idea for my use case. I am still just curious if pytest generates multiple test class objects for each class level parametrize!
I thought #484 worked but no; I am not able to set a class variable in a fixture and access it from the test. the objects self and request.cls are different for some reason. I was expecting them to be same.
classTest_class:
defconfCls(self, request):
self.tmpVar="ABC"# self object and request.cls objects are different for some reasondeftest_class1(self):
printself.tmpVar# buffer not defined error
I am looking to parametrize a class which has more than 1 tests in it. I want a setup/teardown method called for each instance of the class.
BTW, I am not sure if class level fixture works the way I think it (I am assuming for each class level parameter I get new class instance). I would appreciate if someone can educate me if my understanding is wrong.
The text was updated successfully, but these errors were encountered: