Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test_Class: how to write a setup/fixture that runs once for a class instance(not per class/method)? #3718

Closed
Sathisha-Movvar-Bose opened this issue Jul 26, 2018 · 2 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@Sathisha-Movvar-Bose
Copy link

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])
def n_iter(request):
    return request.param

@pytest.mark.usefixtures("n_iter")
class Test_Class1:
	@classmethod
	def setup_class(cls):
		''' This gets called only once not per parameter of class
			: no issues
		'''
		cls.buffer = []
		pdb.set_trace()
		log.info("setup class")
		
	def setup(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")
		
	def teardown(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])
	def test_clsMethod1(self, newVal, n_iter):
		assert n_iter == newVal

	def test_clsMethod2(self, n_iter):
		assert n_iter > 0

	@pytest.mark.parametrize('newVal', [2,4])
	def test_clsMethod3(self, newVal):
		assert newVal > 0
@pytestbot
Copy link
Contributor

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).

@Sathisha-Movvar-Bose
Copy link
Author

Sathisha-Movvar-Bose commented Jul 27, 2018

#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.

class Test_class:
    def confCls(self, request):
          self.tmpVar= "ABC"
          # self object and request.cls objects are different for some reason
    def test_class1(self):
          print self.tmpVar # buffer not defined error

@Zac-HD Zac-HD added the type: question general question, might be closed after 2 weeks of inactivity label Oct 20, 2018
@Zac-HD Zac-HD closed this as completed Oct 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants