Retrieve fixture directly #10223
-
I have a somewhat odd use case in that I'm working in a test suite that already has its own parameterization (it was written back in the nose days). Basically you write code like: class MySuite(APITests):
def validate_X(self, objmaker, params):
myobj = objmaker()
# write tests based on params The Annoyingly, this makes it impossible to use pytest fixtures in this way: class MySuite(APITests):
def validate_X(self, objmaker, params, tmp_path):
myobj = objmaker()
# write tests based on params Is it possible to retrieve an active fixture by name, e.g.: @pytest.mark.usefixtures('tmp_path')
class MySuite(APITests):
def validate_X(self, objmaker, params):
tmp_path = pytest.fetch_fixture('tmp_path')
myobj = objmaker()
# write tests based on params An even better approach, IMO, would be to use it as a context manager that does setup and teardown internal to the function: class MySuite(APITests):
def validate_X(self, objmaker, params):
with pytest.fetch_fixture('tmp_path') as tmp_path:
myobj = objmaker()
# write tests based on params (I'm using Apologies if this is a duplicate. The closest I could find was #3834. Having something like the latter would probably resolve #2703 as well. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
What you suggest is intentionally unsupported The only way to add it would be global state, which is a unacceptable footgun |
Beta Was this translation helpful? Give feedback.
-
Well, I'd hoped there would be some way, but thanks for the prompt reply. |
Beta Was this translation helpful? Give feedback.
-
Couldn't you just have an autoused fixture which stores the fixture objects in the instance of the test object? Something like: class MySuite(APITests):
@pytest.fixture(autouse=True)
def setup(self, tmp_path):
self.tmp_path = tmp_path
def validate_X(self, objmaker, params):
tmp_path = self.tmp_path
myobj = objmaker()
# write tests based on params |
Beta Was this translation helpful? Give feedback.
Couldn't you just have an autoused fixture which stores the fixture objects in the instance of the test object? Something like: