为什么我无法在 pytest 夹具中设置 self.param 值?
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
这导致
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'
我想在设置期间设置此属性,因为我最终将使用该参数执行方法级别的拆卸(不是类级别的拆卸,所以我没有使用 yield)。在这个例子中,我的测试看不到 self.param 值,我的拆解函数也看不到。将 self.param = 2 移动到我的所有测试中很麻烦。有什么更好的方法来做到这一点?