-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add support for using the same fixture twice in a function #5896
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
Comments
its currently very far away, i strongly suggest to simply make 2 fixtures that each create a different folder within the test tmpdir for the time being |
I don't think we could ever enable something like this, there's too many test suites depending on fixtures being the same instance across tests / fixtures. Note also that the doubling of fixtures suggested on SO doesn't really work, they're exactly the same fixture: import pytest
@pytest.fixture
def tmpdir2(tmpdir):
yield tmpdir
def test(tmpdir, tmpdir2):
assert tmpdir is tmpdir2 $ pytest t.py
============================= test session starts ==============================
platform linux -- Python 3.6.8, pytest-5.2.0, py-1.8.0, pluggy-0.13.0
rootdir: /tmp/x
collected 1 item
t.py . [100%]
============================== 1 passed in 0.01s =============================== The suggestion is to make a fixture or set of fixtures which represent your individual sub-directories: @pytest.fixture
def input_dir(tmpdir):
return tmpdir.join('input').ensure_dir()
@pytest.fixture
def output_dir(tmpdir):
return tmpdir.join('output').ensure_dir()
def test(input_dir, output_dir):
... |
@asottile i dont consider the concept itself unthinkable/bad - but it first needs a sound underpinning its unattainable before the implementation of fixtures is refactored |
well for one, keyword arguments wouldn't work -- where would the I don't think this is worth complicating fixtures and I don't think the implementation would be easy (nor the complexity worth it) -- plus it would break all testsuites assuming shared fixtures which has been the behaviour since the beginning afaict |
the required complexity will automatically come from multi-scope fixtures and invocation/usage scope fixtures |
are there plans for that? I don't even know what that means |
multi scope is being able to have fixtures availiable at different scopes using the same name invocation scope is having one fixture instance per request of a fixture |
Closing as duplicate of #2703, which has some more detail on the implementation challenges and interim workarounds (i.e. fixtures providing factories). |
I often end up using the same fixture twice (or even more times) in the same function, for example to get two temporary directories using
tmp_path
. As far as a I know, this is currently only possible by manually creating a copy of the fixture:I would love to see native support for this, and I think keyword arguments would be a nice way to achieve this from the user's perspective:
This would give the user two (or more) independent instances of the fixture.
I don't know enough about the pytest internals to judge how hard this would be to implement, though.
The text was updated successfully, but these errors were encountered: