Skip to content
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

Enable fixture dynamic scoping during test run #6101

Open
rnetser opened this issue Oct 29, 2019 · 6 comments
Open

Enable fixture dynamic scoping during test run #6101

rnetser opened this issue Oct 29, 2019 · 6 comments
Labels
topic: fixtures anything involving fixtures directly or indirectly type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@rnetser
Copy link

rnetser commented Oct 29, 2019

pytest version 5.2.2

Tried to use dynamic scopes for fixtures. However, the scope definition is executed only once - during the fixture definition.
We would like to have option to control fixture scope during test run.
This will allow code re-use in tests which need the code in different scope.
For example: set up a docker - one test needs it in function level and a separate test needs it in module level.

@pytest.fixture()
def some_fixture():
    pass

# test_method.py
@pytest.mark.scope('some_fixture', 'module')
def test_method(some_fixture):
    pass

# test_function.py
@pytest.mark.scope('some_fixture', 'function')
def test_method(some_fixture):
    pass
@Zac-HD Zac-HD added topic: fixtures anything involving fixtures directly or indirectly type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature labels Oct 30, 2019
@aklajnert
Copy link
Contributor

How about extracting fixture body into a separate function, and define two fixtures with different scopes that will call the function?

def fixture_body():
    pass


@pytest.fixture(scope="function")
def some_fixture_module():
    fixture_body()


@pytest.fixture()
def some_fixture_function():
    fixture_body()


# test_method.py
def test_method(some_fixture_module):
    pass


# test_function.py
def test_method(some_fixture_function):
    pass

@RonnyPfannschmidt
Copy link
Member

@rnetser we do have plans to enable fixtures to be available at multiple scopes (i don't have the issue number ready),
we don't yet have a plan to allow the test to do the detailed picking as its not yet clear how the concepts integrate

@aklajnert what you outlined is the current workaround, thanks for taking the time to put it together

@rnetser
Copy link
Author

rnetser commented Nov 12, 2019

@aklajnert thanks, this is what we have implemented as a workaround.

@RonnyPfannschmidt is there a timeline for this?

@RonnyPfannschmidt
Copy link
Member

@rnetser unfortunately there isn't, when we tried to add the feature for pytest 3.0 in the pytest sprint in Freiburg, we learned that the current conceptual layout of the fixture system makes this feature error prone (@nicoddemus didnt spare efforts to make it work then, however one unexpected bug after another turned up, as such we had to stop and rethink)

so far we haven't found the time to find a conceptual layout that makes this feature solid

@victormaricato
Copy link

@rnetser @nicoddemus now with pytest 6.2.2, do you think this feature is somehow more tangible? Can you share your thoughts on the implementation?

@RonnyPfannschmidt
Copy link
Member

@maricatovictor we didnt touch the relevant fixture internals as of now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: fixtures anything involving fixtures directly or indirectly type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

5 participants