Why am I unable to set a self.param value in a pytest fixture?
class TestClass:
@pytest.fixture(scope='class', autouse=True)
def setup_class(self, test_env):
self.param = 2
def teardown_method(self):
remove_setting(self.param)
def test_one(self):
assert self.param == 2
def test_two(self):
assert len("hello") == 5
This results in
scratch.py::TestClass::test_one FAILED
def test_one(self):
> assert self.param == 2
E AttributeError: 'TestClass' object has no attribute 'param'
def teardown_method(self):
> remove_setting(self.param)
E AttributeError: 'TestClass' object has no attribute 'param'
I want to set this attribute during the setup because I will eventually execute a method-level teardown with that parameter (NOT a class-level teardown, so I didn't use yield). In this example, my tests can't see the self.param value and neither can my teardown function. It is cumbersome to move self.param = 2 into all of my tests. What's a better way to do this?