Skip to content

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

Closed
torfsen opened this issue Oct 1, 2019 · 8 comments
Closed

Add support for using the same fixture twice in a function #5896

torfsen opened this issue Oct 1, 2019 · 8 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@torfsen
Copy link

torfsen commented Oct 1, 2019

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:

tmp_path2 = tmp_path

def test_foo(tmp_path, tmp_path2):
    assert something

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:

def test_foo(input_path=tmp_path, output_path=tmp_path):
    assert something

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.

@RonnyPfannschmidt
Copy link
Member

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

@asottile
Copy link
Member

asottile commented Oct 1, 2019

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 asottile added the type: question general question, might be closed after 2 weeks of inactivity label Oct 1, 2019
@RonnyPfannschmidt
Copy link
Member

@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

@asottile
Copy link
Member

asottile commented Oct 1, 2019

well for one, keyword arguments wouldn't work -- where would the tmp_path default value come from (and that would be a break in and of itself for existing things using default values)? what would the scope of such fixtures be (it couldn't be function as then they'd be identical again)

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

@RonnyPfannschmidt
Copy link
Member

the required complexity will automatically come from multi-scope fixtures and invocation/usage scope fixtures

@asottile
Copy link
Member

asottile commented Oct 1, 2019

are there plans for that? I don't even know what that means

@RonnyPfannschmidt
Copy link
Member

multi scope is being able to have fixtures availiable at different scopes using the same name
usage scope is having the fixture that depends on you determine/enforce the scope

invocation scope is having one fixture instance per request of a fixture

@Zac-HD
Copy link
Member

Zac-HD commented Nov 27, 2019

Closing as duplicate of #2703, which has some more detail on the implementation challenges and interim workarounds (i.e. fixtures providing factories).

@Zac-HD Zac-HD closed this as completed Nov 27, 2019
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

4 participants